如何在Android中停止动画 要求:
我需要一个接一个地运行多个动画。我正在使用onAnimationEnd()来实现这一点 在设置动画时,如果发生触摸事件,我需要停止动画并在该位置显示新图像 问题:如何在Android中停止动画 要求:,android,animation,imageview,Android,Animation,Imageview,我需要一个接一个地运行多个动画。我正在使用onAnimationEnd()来实现这一点 在设置动画时,如果发生触摸事件,我需要停止动画并在该位置显示新图像 问题: 我在onTouch中使用clearAnimation()。因此,完整的动画被删除。我的意图是停止动画并在触摸部分显示新图像。我怎样才能做到这一点 由于clearAnimation(),onAnimationEnd()被多次调用,我在一个接一个地运行动画时面临问题 是否有任何功能只是停止动画而不是完全清除动画?我使用的是安卓2.1 @覆
@覆盖
公共布尔onTouch(视图v,运动事件){
开关(event.getAction()){
case MotionEvent.ACTION\u UP:
imageArray[g_animCount-1].clearAnimation();
打破
违约:
打破
}
返回true;//指示事件已处理
}
@凌驾
onAnimationEnd上的公共无效(动画){
layout.removeView(imageArray[g_animCount-1]);
如果((g_计数<计数))
{
startNextAnimation();
}
其他的
{
g_animCount=0;
isBegin=false;
}
}
public void startNextAnimation()
{
int j=随机的nextInt(200);
layoutParams.setMargins(j,-20,30,0);
layout.addView(imageArray[g_animCount],layoutParams);
imageArray[g_animCount]。开始动画(movArray[g_animCount]);
g_animCount++;
}
动画只移动屏幕上的像素,而不移动对象的位置。要想让你的生命停留在终点,就要让你的生命停留在终点
animation.setFillAfter(true);
要实际移动对象的位置,请使用下面代码段的修改版本进行查看
MarginLayoutParams marginParams = new MarginLayoutParams(object.getLayoutParams());
marginParams.setMargins(left, (top+hol2), left, 0);
RelativeLayout.LayoutParams layoutParams = new RelativeLayout.LayoutParams(marginParams);
object.setLayoutParams(layoutParams);
关于onAnimationEnd被多次调用,我需要查看一些代码
我所知道的手动停止动画的唯一两种方法是
animation.cancel(); (may not work for 2.1, can't remember)
或
示例代码如下:
upmotionleft = new TranslateAnimation(0, 0, 0, 600);
upmotionleft.setDuration(2000);
upmotionleft.setFillEnabled(true);
upmotionleft.setAnimationListener(new Animation.AnimationListener()
{
@Override
public void onAnimationStart(Animation animation)
{}
@Override
public void onAnimationEnd(Animation animation)
{
//sets to wherever I want the final object to be
MarginLayoutParams marginParams = new MarginLayoutParams(object.getLayoutParams());
marginParams.setMargins(left, top-hol2, left, 0);
RelativeLayout.LayoutParams layoutParams = new RelativeLayout.LayoutParams(marginParams);
object.setLayoutParams(layoutParams);
//starts next animation
object.startAnimation(nextAnimation);
}
@Override
public void onAnimationRepeat(Animation animation)
{}
});
object.startAnimation(upmotionleft);
这段代码是从我的一个项目中复制和粘贴的,但已经做了一些更改,应该仍然运行。@testingtester是否需要clearAnimation确实调用onAnimationEnd()?
object.clearAnimation();
upmotionleft = new TranslateAnimation(0, 0, 0, 600);
upmotionleft.setDuration(2000);
upmotionleft.setFillEnabled(true);
upmotionleft.setAnimationListener(new Animation.AnimationListener()
{
@Override
public void onAnimationStart(Animation animation)
{}
@Override
public void onAnimationEnd(Animation animation)
{
//sets to wherever I want the final object to be
MarginLayoutParams marginParams = new MarginLayoutParams(object.getLayoutParams());
marginParams.setMargins(left, top-hol2, left, 0);
RelativeLayout.LayoutParams layoutParams = new RelativeLayout.LayoutParams(marginParams);
object.setLayoutParams(layoutParams);
//starts next animation
object.startAnimation(nextAnimation);
}
@Override
public void onAnimationRepeat(Animation animation)
{}
});
object.startAnimation(upmotionleft);