具有无限旋转口吃的ObjectAnimator

时间:2017-09-14 10:41:50

标签: java android animation android-animation objectanimator

我有一个动画师infinite_rotation,定义为:

<set xmlns:android="http://schemas.android.com/apk/res/android">
    <objectAnimator
        android:propertyName="rotation"
        android:repeatCount="infinite"
        android:valueFrom="0"
        android:valueTo="360"
        android:duration="2000" />
</set>

当时间(时间不确定)到来时我不再需要这个,我打电话给infinite_animator.cancel()。然后在其布局容器上播放 fade_out 动画:

<set xmlns:android="http://schemas.android.com/apk/res/android">
        <objectAnimator
            android:propertyName="alpha"
            android:repeatCount="0"
            android:valueTo="0.0"
            android:duration="1000" />
</set>

结果动画是,旋转按预期停止,但在淡出时停顿。我错过了什么?

以下是它的外观:

enter image description here

更新: 我在使用Kitkat OS的旧三星Tab 4上测试了上述问题。我刚刚使用Marshmallow OS测试了一款相当新的三星Tab 4。动画效果很好。所以我想更好的问题是如何在我的旧设备/操作系统上修复草率动画?

这是动画电话:

private void animateRefreshButton() {
    ImageView iv_refresh = (ImageView) findViewById(R.id.iv_refresh);

    if(infinite_animator == null) {
        infinite_animator = AnimatorInflater.loadAnimator(this, R.animator.refresh_rotate);
    }
    infinite_animator.setTarget(iv_refresh);
    infinite_animator.start();
}
当应用确定刷新完成时,会启动

hideRefreshButton()。这是取消和淡出动画的调用:

private void hideRefreshButton() {
    if(infinite_animator != null) {
        infinite_animator.cancel();
    }

    Animator anim = AnimatorInflater.loadAnimator(this, R.animator.refresh_fadeout);
    anim.addListener(new Animator.AnimatorListener() {
        @Override
        public void onAnimationStart(Animator animation) {}

        @Override
        public void onAnimationEnd(Animator animation) {
            framelayout_container_of_iv_refresh.setVisibility(View.GONE);
        }

        @Override
        public void onAnimationCancel(Animator animation) {}

        @Override
        public void onAnimationRepeat(Animator animation) {}
    });
    anim.setTarget(framelayout_container_of_iv_refresh);
    anim.start();
}

1 个答案:

答案 0 :(得分:10)

由于Animation API,我个人不使用"problems" it has。相反,在这种情况下,我会选择Animators API

有这个片段:


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        View view = findViewById(R.id.imageView);

        ObjectAnimator objectAnimator = ObjectAnimator.ofFloat(view, View.ROTATION, 0.0f, 360.0f);

        objectAnimator.setDuration(2000);
        objectAnimator.setRepeatCount(Animation.INFINITE);
        objectAnimator.setInterpolator(new LinearInterpolator());

        objectAnimator.addListener(new AnimatorListenerAdapter() {
            @Override
            public void onAnimationCancel(Animator animation) {
                view.animate()
                        .alpha(0.0f)
                        .setDuration(1000);
            }
        });

        objectAnimator.start();

        view.setOnClickListener((v) -> objectAnimator.cancel());

    }

然后这将是输出:

enter image description here

我已经在模拟器(API 19)上测试过:按预期工作。