Rotate ImageView in Android around a fixed point using RotateAnimation

mmmbaileys picture mmmbaileys · Mar 6, 2011 · Viewed 7.8k times · Source

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?

Answer

Shirish Herwade picture Shirish Herwade · Dec 31, 2012

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