我有以下AnimatorSet方法:
private AnimatorSet dialCenterThrob() {
int bpm = workoutStream.getHeartRate();
dialCenterImageView.clearAnimation();
AnimatorSet finalSet = new AnimatorSet();
ObjectAnimator pulseX = ObjectAnimator.ofFloat(dialCenterImageView, View.SCALE_X, 0.98f, 1.06f);
ObjectAnimator pulseY = ObjectAnimator.ofFloat(dialCenterImageView, View.SCALE_Y, 0.98f, 1.06f);
pulseX.setRepeatMode(ObjectAnimator.REVERSE);
pulseX.setRepeatCount(ObjectAnimator.INFINITE);
pulseY.setRepeatMode(ObjectAnimator.REVERSE);
pulseY.setRepeatCount(ObjectAnimator.INFINITE);
pulseX.setDuration(bpm);
pulseY.setDuration(bpm);
pulseX.setInterpolator(new AccelerateInterpolator());
pulseY.setInterpolator(new AccelerateInterpolator());
finalSet.playTogether(pulseX, pulseY);
return finalSet;
}
这是在var上设置的,称为throbber,偶尔会通过此方法更新:
private void updateThrobbing() {
if (hasThrob()) {
throbber = dialCenterThrob();
throbber.start();
} else {
if (throbber != null && throbber.isRunning()) {
stopThrobbing();
}
}
}
但是我不能让它停止动画,这是目前尝试这样做的方法:
public void stopThrobbing() {
List<Animator> throbbers = throbber.getChildAnimations();
for(Animator animator : throbbers) {
//accomplishes nothing
((ObjectAnimator)animator).setRepeatCount(0);
((ObjectAnimator)animator).setRepeatMode(0);
}
throbber.pause(); //nothing
throbber.cancel(); //and again, nothing
throbber.end();//shocking, I know, but really, nothing
throbber = null;//you'd think this would definitely do it, but no
//desparate attempt, in vein, of course
dialCenterImageView.clearAnimation();
}
我无法让它停止制作动画。更新:我只是尝试将本地参考存储到各个对象动画师,然后调用setRepeatCount,mode,pause,end,取消每个,仍然没有。
答案 0 :(得分:24)
dialCenterImageView.clearAnimation();
这对ObjectAnimator
创建的动画有无影响。您只能通过startAnimation()
findViewById(R.id.yourViewId).startAnimation(yourAnimation);
findViewById(R.id.yourViewId).clearAnimation();
任何对象动画师或者设置为无限的repeatCount的动画师都不会停止,无论你做什么,都不会离开视图。
是的!今天学到了这个艰难的道路。所以会增加我的两分钱。您需要存储ObjectAnimator
实例的引用,然后在其上调用cancel()
。
我在旋转你的Android手机屏幕后仍然继续动画时遇到了这种情况,在这种情况下你的活动基本上是用新的布局重新创建的。
所以这就是我做的事情
@Override
protected void onSaveInstanceState(Bundle outState) {
super.onSaveInstanceState(outState);
ObjectAnimator buttonOneObjectAnimator = myOnClickListener.getButtonOneColorAnim();
if(buttonOneObjectAnimator != null)
buttonOneObjectAnimator.cancel();
}
您也可以在onPause()
中完成此操作。
另一个需要注意的重点。如果您在start()
实例 n次上致电ObjectAnimator
,则必须致电cancel()
n次才能完全停止动画
此外,如果您添加了AnimatorSet
个侦听器,请确保在调用cancel之前删除了侦听器。
yourAnimatorSet.removeAllListeners();
yourAnimatorSet.end();
yourAnimatorSet.cancel();
答案 1 :(得分:1)
这里的回复很晚,但是,我做了什么,不是因为谷歌留下了半生不熟的习惯,就是在其标签中传递视图的ObjectAnimator实例,需要按顺序知道能够阻止它。
示例在视图上启动/取消无限旋转...
private void rotateStart(View view) {
ObjectAnimator animator = ObjectAnimator.ofFloat(view, "rotation", 0f, 360f);
// set animator instance as view's tag
view.setTag(animator);
animator.setDuration(800);
animator.setRepeatMode(ObjectAnimator.RESTART);
animator.setRepeatCount(ObjectAnimator.INFINITE);
animator.start();
}
private void rotateCancel(View view) {
// get view's tag, which is its animator instance
ObjectAnimator animator = (ObjectAnimator) view.getTag();
if (animator != null) {
animator.cancel();
}
}
我认为无论是单个动画还是一组,都可以胜任。