Invalidate doesn't cause OnDraw

空扰寡人 提交于 2019-12-09 04:11:32

Call onDraw every 33ms

You could use Handler to implement this feature.

Every time your DrawCanvas execute the OnDraw method, you could send a message to Handler, when you receive the message in Handler, you could call the DrawCanvas's Invalidate method, this Invalidate method will call OnDraw method. It will keep going.

For example :

class DrawCanvas : View
{
     MyHandler mHandler;
     ...
     private void init(Context ctx)
     {
         mHandler = new MyHandler(this);
         mContext = ctx;
         black = new Paint() { Color = Color.Black,TextSize=56 };
         white = new Paint() { Color = Color.White, TextSize = 56 };
     }
     ...
     protected override void OnDraw(Canvas canvas)
     {
         TestDraw(canvas);
         mHandler.SendEmptyMessageDelayed(1,33);
     }
     ...
     public class MyHandler : Handler
     {
         private DrawCanvas drawCanvas;

         public MyHandler(DrawCanvas drawCanvas)
         {
             this.drawCanvas = drawCanvas;
         }

         public override void HandleMessage(Message msg)
         {
             base.HandleMessage(msg);
             drawCanvas.Invalidate();
         }
     }
}

Effect :

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!