- How to draw using an xml layout in android
- public class CustomDrawableView extends View {
- private ShapeDrawable mDrawable;
- public CustomDrawableView(Context context) {
- super(context);
- int x = 10;
- int y = 10;
- int width = 300;
- int height = 50;
- mDrawable = new ShapeDrawable(new OvalShape());
- mDrawable.getPaint().setColor(0xff74AC23);
- mDrawable.setBounds(x, y, x + width, y + height);
- }
- protected void onDraw(Canvas canvas) {
- mDrawable.draw(canvas);
- }
- }
- CustomDrawableView mCustomDrawableView;
- protected void onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- mCustomDrawableView = new CustomDrawableView(this);
- setContentView(mCustomDrawableView);
- }
- <com.example.shapedrawable.CustomDrawableView
- android:layout_width="fill_parent"
- android:layout_height="wrap_content"
- />
- public CustomDrawableView(Context context, AttributeSet attrs) {
- super(context, attrs);
- initDrawable();
- }
- <com.example.shapedrawable.CustomDrawableView
- android:id="+id/custom_drawable_view"
- android:layout_width="fill_parent"
- android:layout_height="wrap_content"
- />
- CustomDrawableView mCustomDrawableView;
- protected void onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- setContentView(R.layout.main); // assuming your layout is named main.xml
- mCustomDrawableView = (CustomDrawableView) findViewById(R.id.custom_drawable_view);
- }
- x=Integer.parseInt(ast.getAttributeValue(null, "x"));
- y=Integer.parseInt(ast.getAttributeValue(null, "y"));
- width=Integer.parseInt(ast.getAttributeValue(null, "width"));
- height= Integer.parseInt(ast.getAttributeValue(null, "height"));
- mDrawable = new ShapeDrawable(new OvalShape());
- mDrawable.getPaint().setColor(0xff74AC23);
- mDrawable.setBounds(x, y, x + width, y + height);
- @Override
- public void onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- setContentView(R.layout.main);
- }
- public View.OnClickListener cc = (new View.OnClickListener() {
- public void onClick(View view) {
- setContentView(R.id.myid);}
- });