Android - 如何在触摸时旋转图像视图

Android - How to rotate imageview on touch

我有一个 imageview 层,我想在触摸时旋转它并在我再次触摸时停止它,我该怎么做?

您可以尝试像这样以编程方式设置动画:

    RotateAnimation rotate = new RotateAnimation(0, 360,
    Animation.RELATIVE_TO_SELF, 0.5f, Animation.RELATIVE_TO_SELF,
    0.5f);

   rotate.setDuration(4000);
   rotate.setRepeatCount(Animation.INFINITE);
   yourView.setAnimation(rotate);

希望对您有所帮助。

anim目录

中创建动画rotate.xml
<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android"
    android:shareInterpolator="false">
    <rotate
        android:duration="2500"
        android:interpolator="@android:anim/linear_interpolator"
        android:pivotX="50%"
        android:pivotY="50%"
        android:repeatCount="infinite"
        android:repeatMode="restart"
        android:toDegrees="360" />
</set>

现在在你class文件加载动画

Animation animation = AnimationUtils.loadAnimation(getContext(),R.anim.rotate);

然后点击您的图片开始动画

your_image.startAnimation(animation);