【问题标题】:Android AnimatorSet.cancel() does not work on Marshmallow version devicesAndroid AnimatorSet.cancel() 不适用于 Marshmallow 版本的设备
【发布时间】:2021-08-01 12:37:32
【问题描述】:
AnimatorSet animatorSet = new AnimatorSet();
ValueAnimator anim1 = ValueAnimator.ofFloat(0f, 1f);
ValueAnimator anim2 = ValueAnimator.ofFloat(1f, 0f);
~~~
animatorSet.play(anim1).after(anim2);
animatorSet.addListener(new AnimatorListenerAdapter() {
    @Override
    public void onAnimationCancel(Animator animation) {
        Log.d("Testing", "cancel");
    }
}

animatorSet.start();


button.setOnclickListener((v) -> {
    animatorSet.cancel();
})

当点击Button 时,取消监听器运行良好。但是,cancel() 不仅仅在API23 版本(Marshmallow)中被调用。 有什么问题?

【问题讨论】:

    标签: android animation animatorset


    【解决方案1】:

    尝试将您的cancel() 方法修改为:

    public void cancel() {
      if (animatorSet != null) {
        animatorSet.cancel();
      }
      if (next != null) {
        next.cancel();
        next = null;
      }
    }
    

    它也应该适用于 API 23。如果没有,请分享您的项目的更多代码以重建它并找到其他解决方案。

    【讨论】:

    • Animatorset有clearAnimation功能吗?我认为它只有取消和结束功能。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-09-08
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多