Why onDraw is not called after invalidate()?

后端 未结 4 1373
不知归路
不知归路 2020-11-29 01:46

I have found many posts on stackoverflow but I still cannot solve my problem. Here is my code piece:

public class MyView extends RelativeLayout {

Button b1;         


        
相关标签:
4条回答
  • 2020-11-29 02:21

    You need to call setWillNotDraw(false); in order for your onDraw method to be called:

    private void init() {
      setWillNotDraw(false);
      ...
    }
    
    0 讨论(0)
  • 2020-11-29 02:24

    By default all ViewGroup sub-classes do not call their onDraw method, you should enable it by calling setWillNotDraw(false) link

    0 讨论(0)
  • 2020-11-29 02:26

    I was having OnDraw not called in C# using 4.1 Jelly Bean API 16. I switched from compiling with API 16 SDK to compiling with 7.1 Nougat SDK. It solved the problem completely. I still have in manifest min sdk of API 16 and it runs fine in my Jelly Beam avd.

    0 讨论(0)
  • 2020-11-29 02:35

    Borrowing from the link @

    Android Custom Layout - onDraw() never gets called

    Try the below it works

    1.If you're extending a ViewGroup (in your case a RelativeLayout) you should override dispatchDraw() instead of onDraw().

    Discussion on the topic @

    https://groups.google.com/forum/?fromgroups=#!topic/android-developers/oLccWfszuUo

    protected void dispatchDraw (Canvas canvas)

    Called by draw to draw the child views. This may be overridden by derived classes to gain control just before its children are drawn (but after its own view has been drawn).

    Parameters

    canvas the canvas on which to draw the view

    Example

    public class Hello  extends Activity {
    
        private MyView myView;
        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
    
                    super.onCreate(savedInstanceState);
                    Log.e("hello", "hello");
                    this.myView = new MyView(this);
                    setContentView(this.myView);
    
        }
         public class MyView extends RelativeLayout
           {
    
          private Paint myPaint = new Paint();
          private int[] numbers;
          public MyView(Context paramContext)
          {
            super(paramContext);
            Log.e("MyView", "MyView");
            setFocusable(true);
            setBackgroundResource(R.drawable.ic_launcher);
          }
          @Override
          protected void dispatchDraw(Canvas canvas){         
    
                super.dispatchDraw(canvas);     
                Log.i("...............","drawing");   
            }
    
      }
    }
    

    2.If you Override onDraw in the constructor call setWillNotDraw(false) then it should work.

    http://developer.android.com/reference/android/view/View.html#setWillNotDraw(boolean)

    public MyView(Context context) {
    super(context);
    sContext = context;
    init();
    setWillNotDraw(false); 
    }
    
    0 讨论(0)
提交回复
热议问题