Canvas does not draw in Custom View

前端 未结 2 921
耶瑟儿~
耶瑟儿~ 2021-01-11 12:04

I created a Custom View CircleView like this:

public class CircleView extends LinearLayout {

    Paint paint1;
    public CircleView(Context context) {
             


        
相关标签:
2条回答
  • 2021-01-11 12:29

    Where is your this.draw() method?

    This should work definitively:

    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);         
        canvas.drawCircle(50, 50, 25, paint1);
        //this.draw(canvas);  where is this method?
    }
    
    0 讨论(0)
  • 2021-01-11 12:42

    Your onDraw method is never called, you need to call setWillNotDraw(false) on the constructor of your Custom View in order to get onDraw actually called.

    As stated on Android SDK:

    If this view doesn't do any drawing on its own, set this flag to allow further optimizations. By default, this flag is not set on View, but could be set on some View subclasses such as ViewGroup. Typically, if you override onDraw(android.graphics.Canvas) you should clear this flag.

    0 讨论(0)
提交回复
热议问题