绘制延迟在画布上 - "做出的OnDraw()减慢" [英] Draw on a canvas with delay - "make onDraw() slow down"
问题描述
我使用的功能像画圆和drawPoint在Android的画布上。 这工作得很好。
I use functions for canvas like drawCircle and drawPoint in android. This works fine.
但现在的问题是与延迟绘制这些不同的项目,所以它看起来像一个动画。
But the problem now is to draw these different items with a delay, so it looks like an animation.
我应该用什么样的机制?曾试图与异步,但我不喜欢做的那样。
What kind of mechanism should I use? Have tried with async but I dont like that way of doing it.
我应该使用某种类型的计时器,只是画有间隔或有其他聪明的方法来做到这一点?
Should I use some kind of timer that just draw with an interval or is there other clever ways to do this?
推荐答案
我使用这个策略,首先我声明了一个处理程序和一个Runnable这种方式:
I use this strategy, first I declare a Handler and a Runnable that way:
private final Observable mObservable = new Observable();
private final static int TIME_STEP_MS = 5;
private final Handler mHandler = new Handler();
private final Runnable mTimeManager = new Runnable()
{
public void run()
{
mObservable.notifyObservers(TIME_STEP_MS);
mHandler.postDelayed(mTimeManager, TIME_STEP_MS);
}
};
然后,当我要开始我的时间经理,我只需要调用mTimeManager.run(),它会开始通知我观察
S(previously加)定期。
Then when I want to start my time manager I just call the mTimeManager.run() and it will start to notify my Observer
s (previously added) periodically.
如果你需要某种原因停止计时器,或者你只是做的东西:
If you need for some reason stop the timer or something you just do that:
mHandler.removeCallbacks(mTimeManager);
好了比我们更清楚,首先我做了一个自定义的可观察对象一样,[这是可选]:
Ok than let's make it clearer, first I made a custom Observable object like that [that's optional]:
private final Observable mObservable = new Observable()
{
public void notifyObservers()
{
setChanged();
super.notifyObservers();
};
@Override
public void notifyObservers(Object data)
{
setChanged();
super.notifyObservers(data);
};
};
有关,其原因仅仅是因为我不能叫setChanged()可观察到的类之外 - 这是受保护的,如果它没有改变它并没有通知任何观察者
the reason for that is just because I can't call setChanged() outside Observable class - it's protected, if it's not changed it doesn't notify any observer.
另外声明,保持和以前一样所示,现在我要开始这个 TimeManager
的地方,我的应用程序是一个LiveWallpaper,我让所有的渲染的东西变成一个类,扩展了发
,但你并不需要是必然的,我做了一个名为方法 resumeDrawing()
,这个人是之后 super.start()调用;
在我的 @覆盖
公共无效同步启动()
从发
类,方法看起来就像是:
The other declarations keep the same as shown before, now I need to start this TimeManager
somewhere, my app is a LiveWallpaper and I make all rendering stuff into a class that extends a Thread
but you don't need that necessarily, I made a method called resumeDrawing()
, this one is called right after super.start();
at my @Override
of public synchronized void start()
from Thread
class, the method looks like that:
public void resumeDrawing()
{
if (!mTimeManagerRunning) // just a boolean field in my class
{
System.err.println("Resuming renderer."); // just for debug
mTimeManager.run();
mTimeManagerRunning = true;
}
else
{
System.err.println("Renderer already running."); // just for debug
}
}
和它的双:
public void pauseDrawing()
{
if (mTimeManagerRunning)
{
System.err.println("Pausing renderer.");
mHandler.removeCallbacks(mTimeManager);
mTimeManagerRunning = false;
}
else
{
System.err.println("Renderer already paused.");
}
}
好了,现在我们就可以开始和停止时间的经理,但谁在听?没人!所以让我们add'em:在我的渲染器的构造我添加一些观察
s到我的 mObservable
对象之一这些是渲染器本身,所以我的渲染器扩展发
和农具观察
:
Ok, now we can start and stop the time manager, but who's listening? Nobody! so let's add'em: On the constructor of my Renderer I add some Observer
s to my mObservable
object, one of those is the Renderer itself, so my renderer extends Thread
and implements Observer
:
@Override // from Observer interface
public void update(Observable arg0, Object arg1)
{
mElapsedMsRedraw += (Integer) arg1;
if (mElapsedMsRedraw >= mDrawingMsPerFrame)
{
mElapsedMsRedraw = 0;
drawEm(); // refresh the canvas and stuff
}
}
补充观察者,你根本就 mObservable.addObserver(THE_OBJECT - 实现观察员)
你可以看到,我不,我通知每次都重新渲染我的东西,那是因为我用这个TimeManager其他认为不仅仅是刷新画布
如更新对象的位置,我想提请只是内部。
you can see that I don't re-render my stuff each time I'm notified, that's because I use this TimeManager for other thinks than just refresh the Canvas
like updating the position of the objects I want to draw just internally.
所以,你需要慢下来的图纸是什么方式的转变,你的对象内部改变,而时间的推移,我的意思是你的社交圈和点等,也可以几率使你的时间步长,我建议第一个。
So, what you need to slow down the drawing is to change the way your objects change internally while the time passes, I mean your circles and points etc, or you can chance your time step, I recommend the first one.
是不是更清楚?我希望它能帮助。
Was it clearer? I hope it helps.
这篇关于绘制延迟在画布上 - "做出的OnDraw()减慢"的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!