Rotate ImageView in Android around a fixed point using RotateAnimation

7.9k views Asked by At

I'd like to rotate an image 360 degrees continuously around a fixed point. I've seen a few examples already such as:

RotateAnimation anim = new RotateAnimation(0, 360,150,150);
anim.setInterpolator(new LinearInterpolator());
anim.setRepeatCount(Animation.INFINITE);
anim.setDuration(2000);
[imageview].startAnimation(anim);

This does rotate the image, but it does so on an arc/circular path. Ie. the image is moving/rotating in a circular motion but isn't staying fixed at it's starting location.

What I basically want is to mimic the rotation of a WindMill's arms.

Any thoughts?

3

There are 3 answers

0
mmmbaileys On

Ok so I got this working perfectly after some tweaking. Like Macarse said, it did involve the padding around the ImageView.

To fix this issue, all you have to do is put your ImageView inside a RelativeLayout:

<RelativeLayout
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"        
    >

    <ImageView
        android:id="@+id/imageview"
        android:layout_height="wrap_content"
        android:layout_width="wrap_content"
        android:src="@drawable/image"
        />

</RelativeLayout>
0
Dexter wu On

i will say set the pivot point of imageview.. to the x = imgView.getWidth()/2, and y = imgView.getHeight()/2

2
Shirish Herwade On

Use this code

RotateAnimation rotateAnimation1 = new RotateAnimation(0, 360,
        Animation.RELATIVE_TO_SELF, 0.5f,
        Animation.RELATIVE_TO_SELF, 0.5f);
rotateAnimation1.setInterpolator(new LinearInterpolator());
rotateAnimation1.setDuration(duration);
rotateAnimation1.setRepeatCount(0);
img.startAnimation(rotateAnimation1);

this will rotate your image on its fixed position, i.e. around itself