-2

I have an imageview layer and I want to rotate it on touch and stop It when I touch again, how can I do?

Matt
  • 14,906
  • 27
  • 99
  • 149
kingmaker
  • 11
  • 3

2 Answers2

1

Create Animation rotate.xml in anim dir

<?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>

Now in you class file load animation

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

And on click of your image start animation

your_image.startAnimation(animation);
Kishan Vaghela
  • 7,678
  • 5
  • 42
  • 67
  • thanks and last question How Can I handle if I touch again the image I need to stop animation ? – kingmaker Jan 20 '16 at 09:47
  • Take one boolean isAnimationRunning and set it to true when animation started and vice versa. When you click image only start animation if not running. – Kishan Vaghela Jan 20 '16 at 10:12
0

You can try to set an animation programmatically like this:

    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);

Hope it helps.

Kristo
  • 1,339
  • 12
  • 22