如何暂停和恢复旋转动画android?

How to pause and resume rotate animation android?

我已经在音频播放器屏幕上实现了图像旋转动画,其中当音频开始时图像旋转开始并且当用户单击暂停按钮动画停止时,我使用 clearAnimation() 方法停止动画,当用户恢复时我调用 startAnimation () 方法,但这看起来很糟糕,因为当我清除动画图像时,像 90 这样的度数是不同的,当我再次启动动画()时,它的起点是 0。请检查这个视频,其中动画有问题,而 play/pause 音频 Watch This Video.

所以我找到了一个 pause()/resume() 动画来获得更好的流畅度。谁能帮我解决动画平滑度或工作连击问题?

使用 Animator 对象为视图的 属性 设置动画(类似于 android:rotation):

View imgView = findViewById(R.id.image);
View playPauseBtn = findViewById(R.id.button);

final ObjectAnimator anim = ObjectAnimator.ofFloat(imgView, View.ROTATION, 0f, 90f)
        .setDuration(4000);
anim.setRepeatCount(Animation.INFINITE);
anim.setInterpolator(new LinearInterpolator());
anim.start();

playPauseBtn.setOnClickListener(new View.OnClickListener() {
    @Override
    public void onClick(View v) {
        if (anim.isPaused()) {
            anim.resume();
        } else {
            anim.pause();
        }
    }
});