-1

I want to rotate my image view by clicking in my button. I want this: when user click in the button , rotate image view 90 degree. when click in my button again ,rotate image view 180 degree. when click in my button again ,rotate image view 270 degree. when click in my button again ,rotate image view 0 degree. and this continue like this ... I can rotate my image view correctly just one time with 90 degree by this code :

        mImageView.setRotation(0);

and by this I can rotate to 90 degree and return it to 0 degree.

float deg = (mImageView.getRotation() == 90F) ? 0F : 90F;
    mImageView.animate().rotation(deg).setInterpolator(new AccelerateDecelerateInterpolator());

how I can do this?

nsr
  • 115
  • 13

3 Answers3

5

Write down below code on your Button click

imageView.setRotation(imageView.getRotation() + 90);

Initially and after completing whole circle rotation set 0 rotation value to ImageView

Haresh Chhelana
  • 24,720
  • 5
  • 57
  • 67
0
private void rotate(float degree) {
    final RotateAnimation rotateAnim = new RotateAnimation(0.0f, degree,
            RotateAnimation.RELATIVE_TO_SELF, 0.5f,
            RotateAnimation.RELATIVE_TO_SELF, 0.5f);

    rotateAnim.setDuration(0);
    rotateAnim.setFillAfter(true);
    imgview.startAnimation(rotateAnim);
}
0

You can create a rotation animation and set it to Reverse Repeat the animation, This way view will rotate to desired angle and will goes back to initial angle immediately:

ImageView diskView = (ImageView) findViewById(R.id.imageView3);

// Create an animation instance
Animation an = new RotateAnimation(0.0f, 360.0f, pivotX, pivotY);

// Set the animation's parameters
an.setDuration(10000);               // duration in ms
an.setRepeatCount(1);                // -1 = infinite repeated
an.setRepeatMode(Animation.REVERSE); // reverses each repeat
an.setFillAfter(true);               // keep rotation after animation

// Aply animation to image view
diskView.setAnimation(an);
diskView.startAnimation();
Keivan Esbati
  • 3,376
  • 1
  • 22
  • 36