我用runnable旋转图像。我希望旋转图像例如第4次然后暂停/停止旋转。我写了一些函数
public void rotateImage(final View myView, final int size) {
runnable = new Runnable() {
@Override
public void run() {
count++;
myView.setRotation(myView.getRotation() + size);
if (count ==3) {
myHandler.removeCallbacks(runnable);
}
myHandler.postDelayed(this, 100);
// 1000 means 1 second duration
}
};
myHandler.postDelayed(runnable, 100);
}
我可以旋转图像,但我无法停止/暂停旋转.removeCallbacks暂时不工作 如果有人知道解决方案,我的代码有什么问题请帮帮我
答案 0 :(得分:8)
我的Handler / Runnable上有相同的逻辑。它也没有停止。
我做的是:在我的活动的OnDestroy上,我打电话给
myHandler.removeCallbacksAndMessages(null);
它终于停止了。
希望它有所帮助。
答案 1 :(得分:2)
/** * <p>Removes the specified Runnable from the message queue.</p> * * @param action The Runnable to remove from the message handling queue * * @return true if this view could ask the Handler to remove the Runnable, * false otherwise. When the returned value is true, the Runnable * may or may not have been actually removed from the message queue * (for instance, if the Runnable was not in the queue already.) * * @see #post * @see #postDelayed * @see #postOnAnimation * @see #postOnAnimationDelayed */ public boolean removeCallbacks(Runnable action)
removeCallbacks()仅在runnable在消息队列中进行挂起时才有效。 你要删除的runnable显然正在运行。
你最好自己阻止它。像:
public void rotateImage(final View myView, final int size) {
runnable = new Runnable() {
@Override
public void run() {
count++;
myView.setRotation(myView.getRotation() + size);
if (count ==3) {
//myHandler.removeCallbacks(runnable);
} else {
myHandler.postDelayed(this, 100);
}
// 1000 means 1 second duration
}
};
myHandler.postDelayed(runnable, 100);
}