I'm trying to do a rotating image animation. I need to rotate an icon around itself just like they do in a progressbar, but what I'm getting is an image rotating around a circle. Here is my animation code
<?xml version="1.0" encoding="utf-8"?> <rotate xmlns:android="http://schemas.android.com/apk/res/android" android:fromDegrees="0" android:toDegrees="360" android:interpolator="@android:anim/linear_interpolator" android:pivotX="50%" android:pivotY="50%" android:duration="2500" android:repeatCount="infinite" android:repeatMode="restart" />
Where am I going wrong in here? Thanks
android.view.animation.RotateAnimation. An animation that controls the rotation of an object. This rotation takes place in the X-Y plane. You can specify the point to use for the center of the rotation, where (0,0) is the top left point. If not specified, (0,0) is the default rotation point.
Use clearAnimation() to stop an animation.
This is the source code for the layout main.xml:
<?xml version="1.0" encoding="utf-8"?> <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="fill_parent" android:layout_height="fill_parent" > <Button android:id="@+id/testButton" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="animationText" android:onClick="AnimClick"/> <ImageView android:id="@+id/testImage" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_centerInParent="true" android:contentDescription="image_desc" android:scaleType="fitCenter" android:src="@drawable/cat2" /> </RelativeLayout>
To implement the rotation animation, we can define the animation by XML or Java code. If we want to write the animation in the xml, we need to create an animation xml file under /res/anim folder. Here, we create a xml file named rotate_around_center_point.xml
<?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>
and this is my Activity:
public class MainActivity extends Activity implements OnClickListener { @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); Button btn = (Button) findViewById(R.id.testButton); btn.setOnClickListener(this); } @Override public boolean onCreateOptionsMenu(Menu menu) { getMenuInflater().inflate(R.menu.activity_main, menu); return true; } @Override public void onClick(View v) { // TODO Auto-generated method stub ImageView animationTarget = (ImageView) this.findViewById(R.id.testImage); Animation animation = AnimationUtils.loadAnimation(this, R.anim.rotate_around_center_point); animationTarget.startAnimation(animation); } }
You could try with the following code, rather than doing it in XML:
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);
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With