Rotate ImageView in Android around a fixed point using RotateAnimation

I would like to rotate the image 360 ​​degrees continuously around a fixed point. I have already seen several examples, for example:

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

This rotates the image, but it is done in an arc / circular path. I.e. the image moves / rotates in a circular motion, but does not remain motionless at its original location.

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

Any thoughts?

+4
source share
4 answers

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 in a fixed position, i.e. around you

+7
source

This may be due to filling. Check out this similar question .

0
source

Ok, so I got this working fine after some tweaking. As Makarsa said, this is due to the padding around ImageView .

To fix this problem, all you have to do is place the 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
source

I will say to set the anchor point of the image. x = imgView.getWidth () / 2 and y = imgView.getHeight () / 2

-1
source

Source: https://habr.com/ru/post/1342555/


All Articles