gpt4 book ai didi

android - 让 Android AnimatorSet 停止动画

转载 作者:塔克拉玛干 更新时间:2023-11-02 23:33:03 31 4
gpt4 key购买 nike

我有以下 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、模式、暂停、结束、取消,但仍然没有。

最佳答案

dialCenterImageView.clearAnimation();

这对 ObjectAnimator 创建的动画没有影响。您只能通过 startAnimation()

清除在 View 上启动的动画
findViewById(R.id.yourViewId).startAnimation(yourAnimation);
findViewById(R.id.yourViewId).clearAnimation();

any object animator, or animatorset with repeatCount set to Infinite will NOT stop, no matter what you do, short of leaving the view.

没错!今天通过艰难的方式学到了这一点。所以会加上我的两分钱。您需要存储 ObjectAnimator 实例的引用,然后在其上调用 cancel()

我在旋转 Android 手机屏幕后动画仍在继续时遇到了这种情况,在这种情况下,您的 Activity 基本上是用新布局重新创建的。

这就是我做的

@Override
protected void onSaveInstanceState(Bundle outState) {
super.onSaveInstanceState(outState);
ObjectAnimator buttonOneObjectAnimator = myOnClickListener.getButtonOneColorAnim();
if(buttonOneObjectAnimator != null)
buttonOneObjectAnimator.cancel();
}

您也可以在 onPause() 中很好地完成它。

还有一点需要注意。如果您在 ObjectAnimator 实例上调用 start() n 次,那么您将不得不调用 cancel() < strong>n 次 动画完全停止。

此外,如果您添加了 AnimatorSet 监听器,请确保在调用取消之前已移除监听器。

yourAnimatorSet.removeAllListeners();
yourAnimatorSet.end();
yourAnimatorSet.cancel();

关于android - 让 Android AnimatorSet 停止动画,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25254046/

31 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com