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

Android-如何在触摸屏上旋转图像视图,android,rotation,imageview,touch,Android,Rotation,Imageview,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(

我有一个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);

希望有帮助。

animdir中创建动画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>
然后单击图像开始动画

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

这里回答了相同的问题-如何进行ontouch旋转和ontouch再次停止旋转?谢谢,最后一个问题是,如果我再次触摸需要停止动画的图像,我该如何处理?取一个布尔值isAnimationRunning,并在动画开始时将其设置为true,反之亦然。单击“图像”时,仅在未运行时启动动画。