我有以下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;
}这是在名为throbber的var上设置的,偶尔会通过以下方法进行更新:
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、cancel,但仍然一无所获。
发布于 2015-05-14 03:19:04
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()中也可以很好地做到这一点。
另一个需要注意的重要问题。如果在ObjectAnimator实例上调用start() n次,则必须调用cancel() n次才能完全停止动画。
另外,如果您添加了AnimatorSet侦听器,则在调用cancel之前,请确保已删除这些侦听器。
yourAnimatorSet.removeAllListeners();
yourAnimatorSet.end();
yourAnimatorSet.cancel();发布于 2018-03-04 02:32:32
但是,我所做的,并不是因为谷歌习惯于半途而废,而是在它的标记中传递了视图的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();
}
}我假设这将完成这项工作,无论它是单个动画还是一组动画。
https://stackoverflow.com/questions/25254046
复制相似问题