无效不会导致OnDraw [英] Invalidate doesn't cause OnDraw
问题描述
我的Android应用程序有问题.我创建了自定义视图( http://syedwasihaider.github.io/blog/2015/07/12/XamarinViews/直到第2步),覆盖了onDraw,然后我绘制了黑屏,并绘制了文本"Draw {i}",其中i
是每次onDraw发生时都会递增的数字.我想以30次/秒(或60次)的速度调用此方法,所以我设置了一个计时器,每33毫秒导致一次Invalidate().
I have problem with my Android app. I've created custom view (http://syedwasihaider.github.io/blog/2015/07/12/XamarinViews/ up to step 2), overriden onDraw and I draw black screen and text "Draw {i}" where i
is number that increments every time onDraw occurs. I want to call this method 30 times/second (or 60), so I've set up timer that causes Invalidate() every 33 ms.
但是Invalidate()根本不会导致onDraw! (这不像是在invalid和ondraw之间的延迟,根本没有调用ondraw).我尝试设置SetWillNotDraw(false)
,但是没有用.这是我的代码:
But Invalidate() doesn't cause onDraw at all! (It's not like its a delay between invalidate and ondraw, ondraw isn't called at all). I've tried to set SetWillNotDraw(false)
, but it didn't work. Here's my code:
class DrawCanvas : View
{
Context mContext;
public DrawCanvas(Context context) : base(context)
{
init(context);
}
public DrawCanvas(Context context, IAttributeSet attrs) : base(context, attrs)
{
init(context);
}
public DrawCanvas(Context context, IAttributeSet attrs, int defStyle) : base(context, attrs, defStyle)
{
init(context);
}
private void init(Context ctx)
{
mContext = ctx;
black = new Paint() { Color = Color.Black };
white = new Paint() { Color = Color.White };
Timer timer = new Timer(33);
timer.Elapsed += Timer_Elapsed;
timer.Start();
}
private void Timer_Elapsed(object sender, ElapsedEventArgs e)
{
Invalidate();
}
int i = 0;
Paint black;
Paint white;
private void TestDraw(Canvas canvas)
{
canvas.DrawRect(0, 0, Width, Height, black);
canvas.DrawText("Draw " + i, 10, 10, white);
i++;
}
protected override void OnDraw(Canvas canvas)
{
TestDraw(canvas);
}
}
我该怎么做:
每33毫秒调用一次onDraw
Call onDraw every 33ms
OR
获取onDraw方法中使用的画布? (试图将画布作为参数保存在onDraw中,并在以后使用,但是有一些奇怪的行为,所以它不起作用.)
Get canvas that is used in onDraw method? (tried to save canvas that comes as parameter in onDraw and use it later, but there was some weird behaviour, so it didn't work).
推荐答案
每33毫秒调用一次onDraw
Call onDraw every 33ms
您可以使用处理程序来实现此功能.
You could use Handler to implement this feature.
每次DrawCanvas
执行OnDraw
方法时,您都可以向Handler
发送一条消息,当您在Handler
中收到消息时,可以调用DrawCanvas
的Invalidate
方法,此Invalidate
方法将调用OnDraw
方法.它将继续下去.
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.
例如:
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();
}
}
}
效果:
这篇关于无效不会导致OnDraw的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!