Don't like ads? PRO users don't see any ads ;-)
Guest

Untitled

By: a guest on Jun 30th, 2012  |  syntax: None  |  size: 2.12 KB  |  hits: 13  |  expires: Never
download  |  raw  |  embed  |  report abuse  |  print
Text below is selected. Please press Ctrl+C to copy to your clipboard. (⌘+C on Mac)
  1. How to draw using an xml layout in android
  2. public class CustomDrawableView extends View {
  3. private ShapeDrawable mDrawable;
  4.  
  5. public CustomDrawableView(Context context) {
  6. super(context);
  7.  
  8. int x = 10;
  9. int y = 10;
  10. int width = 300;
  11. int height = 50;
  12.  
  13. mDrawable = new ShapeDrawable(new OvalShape());
  14. mDrawable.getPaint().setColor(0xff74AC23);
  15. mDrawable.setBounds(x, y, x + width, y + height);
  16. }
  17.  
  18. protected void onDraw(Canvas canvas) {
  19. mDrawable.draw(canvas);
  20. }
  21. }
  22.        
  23. CustomDrawableView mCustomDrawableView;
  24. protected void onCreate(Bundle savedInstanceState) {    
  25. super.onCreate(savedInstanceState);    
  26. mCustomDrawableView = new CustomDrawableView(this);        
  27. setContentView(mCustomDrawableView);
  28. }
  29.        
  30. <com.example.shapedrawable.CustomDrawableView
  31.    android:layout_width="fill_parent"    
  32. android:layout_height="wrap_content"    
  33. />
  34.        
  35. public CustomDrawableView(Context context, AttributeSet attrs) {
  36.     super(context, attrs);
  37.     initDrawable();
  38. }
  39.        
  40. <com.example.shapedrawable.CustomDrawableView
  41.    android:id="+id/custom_drawable_view"
  42.    android:layout_width="fill_parent"    
  43.     android:layout_height="wrap_content"    
  44. />
  45.        
  46. CustomDrawableView mCustomDrawableView;
  47.  
  48. protected void onCreate(Bundle savedInstanceState) {    
  49.     super.onCreate(savedInstanceState);          
  50.     setContentView(R.layout.main); // assuming your layout is named main.xml
  51.     mCustomDrawableView = (CustomDrawableView) findViewById(R.id.custom_drawable_view);
  52. }
  53.        
  54. x=Integer.parseInt(ast.getAttributeValue(null, "x"));      
  55.     y=Integer.parseInt(ast.getAttributeValue(null, "y"));
  56.     width=Integer.parseInt(ast.getAttributeValue(null, "width"));
  57.     height= Integer.parseInt(ast.getAttributeValue(null, "height"));
  58.  
  59.     mDrawable = new ShapeDrawable(new OvalShape());
  60.     mDrawable.getPaint().setColor(0xff74AC23);
  61.     mDrawable.setBounds(x, y, x + width, y + height);
  62.        
  63. @Override
  64. public void onCreate(Bundle savedInstanceState) {
  65.     super.onCreate(savedInstanceState);
  66.     setContentView(R.layout.main);
  67.  }
  68. public View.OnClickListener cc = (new View.OnClickListener() {
  69.     public void onClick(View view) {
  70.                     setContentView(R.id.myid);}
  71.        });