How to draw using xml layout in android

I am trying to work with the android example provided on the developers page. He gives two ways to draw on canvas. The first way is to use the CustomDrawableView class, which looks like this:

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);
}
}

Then it is called by the main class:

CustomDrawableView mCustomDrawableView;
protected void onCreate(Bundle savedInstanceState) {    
super.onCreate(savedInstanceState);    
mCustomDrawableView = new CustomDrawableView(this);        
setContentView(mCustomDrawableView);
}

But then the lesson says:

If you want to draw this custom drawing from an XML layout, and not from an Activity, the CustomDrawable class must override the View (Context, AttributeSet) constructor, which is called when creating the View instance through inflation from XML. Then add the CustomDrawable element to the XML, for example:

<com.example.shapedrawable.CustomDrawableView
   android:layout_width="fill_parent"     
android:layout_height="wrap_content"     
/>

I successfully got the first example to run, but I don't know what my code looks like if I want to accept the second option. Presumably, I start with @Override View, but then got stuck.

, , - , , , .

+3
2

, , , . , .

public CustomDrawableView(Context context, AttributeSet attrs) {
    super(context, attrs);
    initDrawable();
}

xml

<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);
}
+4

public CustomDrawableView (Context v, AttributeSet as) {        (V, );       drawShape ();   } public void drawShape (AttributeSet ast) {
      int x = 0;       int y = 0;       int width = 0;       int height = 0;

            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);

} }

MainActivity.java {  CustomDrawableView mCustomDrawableView;

@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);}
       });       

} .. ( , ...)

0

Source: https://habr.com/ru/post/1793887/


All Articles