在第一次循环后停止可绘制动画
Stopping drawable animation after first loop
我的问题是试图弄清楚如何在遍历图像(例如返回到葡萄图像)后停止可绘制动画。我当然不能使用我在 .ACTION.DOWN
中实现的方法,所以任何人都可以帮助我。
public boolean onTouchEvent (MotionEvent event){
Drawable currentFrame, checkFrame;
if (event.getAction() == MotionEvent.ACTION_DOWN) {
if (fruitAnimation.isRunning()) {
fruitAnimation.stop();
//The variable that will guard the frame number
int frameNumber = 0;
//Get the frame of the animation
//Drawable currentFrame, checkFrame;
currentFrame = fruitAnimation.getCurrent();
//checks the position of the frame.
for (int i = 0; i < fruitAnimation.getNumberOfFrames(); i++) {
checkFrame = fruitAnimation.getFrame(i);
if (checkFrame == currentFrame) {
frameNumber = i;
break;
}
}
String fruit = "";
switch (frameNumber) {
case 0:
fruit = "Grapes";
break;
case 1:
fruit = "Lemon";
break;
case 2:
fruit = "Orange";
break;
case 3:
fruit = "Pear";
break;
case 4:
fruit = "Strawberry";
break;
}
Toast.makeText(this, fruit, Toast.LENGTH_SHORT).show();
}else {
fruitAnimation.start();
}
return true;
}
return super.onTouchEvent(event);
}
你很可能做错了。因为有 animation listener 你可以用来监控你的动画,我将动画设置为一次性 运行 然后在我的监听器中,在它的 onAnimationEnd()
我会检查我是否达到我的极限,如果没有,重新开始动画。
我的问题是试图弄清楚如何在遍历图像(例如返回到葡萄图像)后停止可绘制动画。我当然不能使用我在 .ACTION.DOWN
中实现的方法,所以任何人都可以帮助我。
public boolean onTouchEvent (MotionEvent event){
Drawable currentFrame, checkFrame;
if (event.getAction() == MotionEvent.ACTION_DOWN) {
if (fruitAnimation.isRunning()) {
fruitAnimation.stop();
//The variable that will guard the frame number
int frameNumber = 0;
//Get the frame of the animation
//Drawable currentFrame, checkFrame;
currentFrame = fruitAnimation.getCurrent();
//checks the position of the frame.
for (int i = 0; i < fruitAnimation.getNumberOfFrames(); i++) {
checkFrame = fruitAnimation.getFrame(i);
if (checkFrame == currentFrame) {
frameNumber = i;
break;
}
}
String fruit = "";
switch (frameNumber) {
case 0:
fruit = "Grapes";
break;
case 1:
fruit = "Lemon";
break;
case 2:
fruit = "Orange";
break;
case 3:
fruit = "Pear";
break;
case 4:
fruit = "Strawberry";
break;
}
Toast.makeText(this, fruit, Toast.LENGTH_SHORT).show();
}else {
fruitAnimation.start();
}
return true;
}
return super.onTouchEvent(event);
}
你很可能做错了。因为有 animation listener 你可以用来监控你的动画,我将动画设置为一次性 运行 然后在我的监听器中,在它的 onAnimationEnd()
我会检查我是否达到我的极限,如果没有,重新开始动画。