在 Android 中处理多个 Runnables()
Handling multiple Runnables() in Android
我正在尝试连续为 4 个图像制作动画,当我收到一条发送 Broadcast Intent 的新 GCM 消息时切换动画。我将展示前 2 个的代码,以及出了什么问题。基本上在两个命令之间(案例 0 与案例 1),我需要能够停止第一个 运行nable 然后启动第二个。我尝试通过设置
来做到这一点
isRunning = false;
但这不起作用。我在想,如果我继续破解,我可能会做对,但我想知道正确的方法是什么。
public void startAnimatedBackground(Integer num) {
isRunning = false;
ImageSwitcher imageSwitcher = null;
aniIn = AnimationUtils.loadAnimation(this,
android.R.anim.fade_in);
aniOut = AnimationUtils.loadAnimation(this,
android.R.anim.fade_out);
aniIn.setDuration(500);
aniOut.setDuration(500);
switch (num) {
case 0:
imageSwitcher = (ImageSwitcher) findViewById(R.id.switcher_accept);
break;
case 1:
imageSwitcher = (ImageSwitcher) findViewById(R.id.switcher_on_way);
break;
default:
break;
}
imageSwitcher.setInAnimation(aniIn);
imageSwitcher.setOutAnimation(aniOut);
imageSwitcher.setFactory(this);
imageSwitcher.setImageResource(images[0]);
isRunning = true;
final Handler handler = new Handler();
final ImageSwitcher finalImageSwitcher = imageSwitcher;
Runnable runnable = new Runnable() {
@Override
public void run() {
if (isRunning) {
System.out.println("Running.."+finalImageSwitcher+index);
index++;
index = index % images.length;
finalImageSwitcher.setImageResource(images[index]);
handler.postDelayed(this, interval);
}
}
};
handler.postDelayed(runnable, interval);
}
这是一开始发生的事情,而且是正确的。我得到一个闪烁的图像。
...app:id/switcher_accept}1
...app:id/switcher_accept}0
...app:id/switcher_accept}1
...app:id/switcher_accept}0
...app:id/switcher_accept}1
在案例 2 运行s 之后,这就是 运行ning 过程的样子。如您所见,两者都是 运行ning,并且 none 的预期行为正在发生。我想要的是 2nd(switcher_on_way) 到 运行 就像之前的第一个一样。我希望我的 isRunning=false;代码会让这种情况发生,但显然不是。
...app:id/switcher_on_way}0
...app:id/switcher_accept}1
...app:id/switcher_on_way}0
...app:id/switcher_accept}1
...app:id/switcher_on_way}0
...app:id/switcher_accept}1
你的问题出在这块:
Runnable runnable = new Runnable() {
@Override
public void run() {
if (isRunning) {
System.out.println("Running.."+finalImageSwitcher+index);
...
handler.postDelayed(this, interval);
}
}
};
handler.postDelayed(runnable, interval);
您试图创建一个循环并通过 isRunning
变量控制它。但是,问题是当 isRunning
为 true
.
时,在 interval
中间检查条件
1: Start _isRunning=false______Check(isRunning)________Check(isRunning)
2: ___Start _isRunning=false___isRunning=true_______isRunning=true
并且您需要其他方法,例如:创建全局 handler
而不是本地,然后在开始新动画时取消 postDelayed
。
参考:cancelling a handler.postdelayed process
或者您可以使用 wait
、synchronized
和 notify
。
我正在尝试连续为 4 个图像制作动画,当我收到一条发送 Broadcast Intent 的新 GCM 消息时切换动画。我将展示前 2 个的代码,以及出了什么问题。基本上在两个命令之间(案例 0 与案例 1),我需要能够停止第一个 运行nable 然后启动第二个。我尝试通过设置
来做到这一点isRunning = false;
但这不起作用。我在想,如果我继续破解,我可能会做对,但我想知道正确的方法是什么。
public void startAnimatedBackground(Integer num) {
isRunning = false;
ImageSwitcher imageSwitcher = null;
aniIn = AnimationUtils.loadAnimation(this,
android.R.anim.fade_in);
aniOut = AnimationUtils.loadAnimation(this,
android.R.anim.fade_out);
aniIn.setDuration(500);
aniOut.setDuration(500);
switch (num) {
case 0:
imageSwitcher = (ImageSwitcher) findViewById(R.id.switcher_accept);
break;
case 1:
imageSwitcher = (ImageSwitcher) findViewById(R.id.switcher_on_way);
break;
default:
break;
}
imageSwitcher.setInAnimation(aniIn);
imageSwitcher.setOutAnimation(aniOut);
imageSwitcher.setFactory(this);
imageSwitcher.setImageResource(images[0]);
isRunning = true;
final Handler handler = new Handler();
final ImageSwitcher finalImageSwitcher = imageSwitcher;
Runnable runnable = new Runnable() {
@Override
public void run() {
if (isRunning) {
System.out.println("Running.."+finalImageSwitcher+index);
index++;
index = index % images.length;
finalImageSwitcher.setImageResource(images[index]);
handler.postDelayed(this, interval);
}
}
};
handler.postDelayed(runnable, interval);
}
这是一开始发生的事情,而且是正确的。我得到一个闪烁的图像。
...app:id/switcher_accept}1 ...app:id/switcher_accept}0 ...app:id/switcher_accept}1 ...app:id/switcher_accept}0 ...app:id/switcher_accept}1
在案例 2 运行s 之后,这就是 运行ning 过程的样子。如您所见,两者都是 运行ning,并且 none 的预期行为正在发生。我想要的是 2nd(switcher_on_way) 到 运行 就像之前的第一个一样。我希望我的 isRunning=false;代码会让这种情况发生,但显然不是。
...app:id/switcher_on_way}0 ...app:id/switcher_accept}1 ...app:id/switcher_on_way}0 ...app:id/switcher_accept}1 ...app:id/switcher_on_way}0 ...app:id/switcher_accept}1
你的问题出在这块:
Runnable runnable = new Runnable() {
@Override
public void run() {
if (isRunning) {
System.out.println("Running.."+finalImageSwitcher+index);
...
handler.postDelayed(this, interval);
}
}
};
handler.postDelayed(runnable, interval);
您试图创建一个循环并通过 isRunning
变量控制它。但是,问题是当 isRunning
为 true
.
interval
中间检查条件
1: Start _isRunning=false______Check(isRunning)________Check(isRunning)
2: ___Start _isRunning=false___isRunning=true_______isRunning=true
并且您需要其他方法,例如:创建全局 handler
而不是本地,然后在开始新动画时取消 postDelayed
。
参考:cancelling a handler.postdelayed process
或者您可以使用 wait
、synchronized
和 notify
。