在循环中创造独特postDelayed的Runnable [英] Creating Unique postDelayed Runnables in a Loop
问题描述
我模仿一个帧动画;我拥有这一切工作空余的一个问题。 我有一个for循环中,在每次迭代,它改变了ImageView的形象延迟后。
I'm emulating a Frame Animation; I have it all working spare one issue. I have a for loop in which, on every iteration, it changes the Image of an ImageView after a delay.
for(int i = 1; i <13; i++){
if (stop== false){
String imgName = "b"+ Integer.toString(i);
id = getResources().getIdentifier(imgName, "drawable", getPackageName());
Handler handlerTimer = new Handler();
handlerTimer.postDelayed(new Runnable(){
public void run() {
view.setImageDrawable((getResources().getDrawable(id)));
}}, 300);
}
}
问题是运行()不能在每次迭代更新;它只能一次。
The issue is that run() doesn't refresh on every iteration; it only works once.
我怎样才能刷新或创建一个新的run()?
How can I refresh or make a new run()?
我接受任何其他的方式来做到这一点。
I'm open to any other way to do this.
任何意见将是AP preciated。
Any advice would be appreciated.
推荐答案
第1步:定义的Runnable
作为活动的数据成员(或是其他地方,这个code所在)
Step #1: Define the Runnable
as a data member of your activity (or wherever this code resides)
步骤#2:转储处理程序
,因为你不需要它 - postDelayed()
是在查看
以及
Step #2: Dump the Handler
, as you don't need it -- postDelayed()
is implemented on View
as well
第三步:创建一个执行 postDelayed()
调用一个辅助方法 - 我指的是方法, FOO()
在这里 - 和呼叫 FOO()
,你有权不叫 postDelayed()
Step #3: Create a helper method that does the postDelayed()
call -- I'll refer to that method as foo()
here -- and call foo()
where you right not call postDelayed()
第四步:在的run()
的的Runnable
,呼叫的foo( )
再重新安排的Runnable
再延迟时间
Step #4: In run()
of the Runnable
, call foo()
again to reschedule the Runnable
for another delay period
这篇关于在循环中创造独特postDelayed的Runnable的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!