【发布时间】:2011-05-05 23:37:43
【问题描述】:
我需要停止正在运行的翻译动画。 Animation的.cancel()方法无效;无论如何,动画都会一直播放到最后。
如何取消正在运行的动画?
【问题讨论】:
我需要停止正在运行的翻译动画。 Animation的.cancel()方法无效;无论如何,动画都会一直播放到最后。
如何取消正在运行的动画?
【问题讨论】:
拨打clearAnimation() 拨打您拨打startAnimation() 的任何View。
【讨论】:
setFillAfter() 可能不会像您认为的那样做,无论如何。当触摸事件发生时,清除动画,然后调整布局以影响您寻求的任何永久更改。
您可以尝试做的是在停止动画之前从动画中获取转换矩阵并检查矩阵内容以获取您正在寻找的位置值。
这里是你应该研究的 api
public boolean getTransformation (long currentTime, Transformation outTransformation)
public void getValues (float[] values)
例如(一些伪代码。我没有测试过):
Transformation outTransformation = new Transformation();
myAnimation.getTransformation(currentTime, outTransformation);
Matrix transformationMatrix = outTransformation.getMatrix();
float[] matrixValues = new float[9];
transformationMatrix.getValues(matrixValues);
float transX = matrixValues[Matrix.MTRANS_X];
float transY = matrixValues[Matrix.MTRANS_Y];
【讨论】:
在 Android 4.4.4 上,似乎我可以在 View 上停止 alpha 渐变动画的唯一方法是调用 View.animate().cancel()(即,在 View 的 ViewPropertyAnimator 上调用 .cancel())。
这是我在 ICS 前后用于兼容性的代码:
public void stopAnimation(View v) {
v.clearAnimation();
if (canCancelAnimation()) {
v.animate().cancel();
}
}
...用方法:
/**
* Returns true if the API level supports canceling existing animations via the
* ViewPropertyAnimator, and false if it does not
* @return true if the API level supports canceling existing animations via the
* ViewPropertyAnimator, and false if it does not
*/
public static boolean canCancelAnimation() {
return Build.VERSION.SDK_INT >= Build.VERSION_CODES.ICE_CREAM_SANDWICH;
}
这是我要停止的动画:
v.setAlpha(0f);
v.setVisibility(View.VISIBLE);
// Animate the content view to 100% opacity, and clear any animation listener set on the view.
v.animate()
.alpha(1f)
.setDuration(animationDuration)
.setListener(null);
【讨论】:
.setListener(null); 这对我有帮助。
如果您使用动画侦听器,请设置v.setAnimationListener(null)。将以下代码与所有选项一起使用。
v.getAnimation().cancel();
v.clearAnimation();
animation.setAnimationListener(null);
【讨论】:
v.setAnimation(null);,因为这将在v.clearAnimation(); 中完成。
你必须使用 .clearAnimation(); UI线程中的方法:
runOnUiThread(new Runnable() {
@Override
public void run() {
v.clearAnimation();
}
});
【讨论】:
使用方法 setAnimation(null) 来停止动画,它作为公共方法暴露在
View.java,它是所有widgets 的基类,用于创建交互式UI 组件(按钮、文本字段等)。
/**
* Sets the next animation to play for this view.
* If you want the animation to play immediately, use
* {@link #startAnimation(android.view.animation.Animation)} instead.
* This method provides allows fine-grained
* control over the start time and invalidation, but you
* must make sure that 1) the animation has a start time set, and
* 2) the view's parent (which controls animations on its children)
* will be invalidated when the animation is supposed to
* start.
*
* @param animation The next animation, or null.
*/
public void setAnimation(Animation animation)
【讨论】:
要停止动画,你可以设置这样的 objectAnimator 什么都不做,例如
首先手动翻转时有从左到右的动画:
flipper.setInAnimation(leftIn);
flipper.setOutAnimation(rightOut);
然后当切换到自动翻转时没有动画
flipper.setInAnimation(doNothing);
flipper.setOutAnimation(doNothing);
doNothing = ObjectAnimator.ofFloat(flipper, "x", 0f, 0f).setDuration(flipperSwipingDuration);
【讨论】:
首先,删除所有与动画师或动画师集相关的侦听器。然后取消动画师或动画师集。
inwardAnimationSet.removeAllListeners()
inwardAnimationSet.cancel()
【讨论】:
这样使用:
// start animation
TranslateAnimation anim = new TranslateAnimation( 0, 100 , 0, 100);
anim.setDuration(1000);
anim.setFillAfter( true );
view.startAnimation(anim);
// end animation or cancel that
view.getAnimation().cancel();
view.clearAnimation();
取消()
取消动画。取消动画会调用动画 如果设置了监听器,则通知动画结束。 如果手动取消动画,则必须调用 reset() 在重新开始动画之前。
clearAnimation()
取消此视图的所有动画。
【讨论】: