旋转动画中的恒定速度

时间:2018-10-27 15:31:10

标签: android animation

我正在使用RotateAnimationImageView旋转到一定程度(我的ImageView是一个圆圈)。

degree_old = degree % 360;
degree = random_sector.nextInt(3600) + 720;

这是我第一次旋转的代码:

final RotateAnimation rotate = new RotateAnimation(0, degree, RotateAnimation.RELATIVE_TO_SELF,
            0.5f, RotateAnimation.RELATIVE_TO_SELF, 0.5f);
    rotate.setDuration(5000);
    rotate.setFillAfter(true);
    rotate.setRepeatCount(0);
    rotate.setRepeatMode(0);
    rotate.setInterpolator(new DecelerateInterpolator());

然后此代码再次旋转到起始位置:

 final RotateAnimation rotateBack = new RotateAnimation(degree, (back_degree + degree), RotateAnimation.RELATIVE_TO_SELF,
                            0.5f, RotateAnimation.RELATIVE_TO_SELF, 0.5f);
                    rotateBack.setDuration(3000);
                    rotateBack.setFillAfter(true);
                    rotateBack.setRepeatCount(0);
                    rotateBack.setRepeatMode(0);
                    rotateBack.setInterpolator(new LinearInterpolator());

有效。

我的问题是每次旋转的速度都不一样。有时它以很高的速度旋转,而其他时候则以非常低的速度旋转。

我的问题是:有没有办法每次都保持相同的速度?我希望动画以低速旋转5秒。

如何更改纺纱速度?

1 个答案:

答案 0 :(得分:0)

不确定是否只想使用RotateAnimation。

以下代码将Image旋转360度,然后非常平滑地返回。 显然,您可以调整此代码以包括随机生成器。

public class MainActivity extends AppCompatActivity {

ImageView my_image;

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

    my_image = findViewById(R.id.my_image);
    animate_it();
}

private void animate_it() {
    my_image.setRotation(0);
    my_image.animate().rotation(360).setDuration(5000).setListener(new Animator.AnimatorListener() {
        @Override
        public void onAnimationStart(Animator animation) {

        }

        @Override
        public void onAnimationEnd(Animator animation) {
            my_image.animate().rotation(0).setDuration(5000);
        }

        @Override
        public void onAnimationCancel(Animator animation) {

        }

        @Override
        public void onAnimationRepeat(Animator animation) {

        }
    });
}

}