Constant image rotation in android

I use the following code to rotate an image

<set xmlns:android="http://schemas.android.com/apk/res/android" android:shareInterpolator="false"> <rotate android:fromDegrees="0" android:toDegrees="360" android:duration="500" android:repeatCount="infinite" android:pivotX="50%" android:pivotY="50%" > </rotate> </set> 

Animation rotate1 = AnimationUtils.loadAnimation (this, R.anim.rotate_picture); rotate.startAnimation (rotate1);

The layout I use is

 <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="fill_parent" android:layout_height="fill_parent" android:orientation="vertical" > <ImageView android:layout_width="wrap_content" android:layout_height="wrap_content" android:src="@drawable/loader" android:layout_centerInParent="true" android:id="@+id/rotate" /> </RelativeLayout> 

But this is a 500 ms stop and restart again. But I need to constantly rotate the image. No stop in the middle. How can i do this.

+4
source share
3 answers
  //custom_anim.xml <?xml version="1.0" encoding="utf-8" ?> <set xmlns:android="http://schemas.android.com/apk/res/android" android:shareInterpolator="false"> <rotate android:fromDegrees="0" android:toDegrees="360" android:pivotX="50%" android:pivotY="50%" android:duration="2000" /> <alpha android:fromAlpha="0.0" android:toAlpha="1.0" android:duration="2000"> </alpha> <scale android:pivotX="50%" android:pivotY="50%" android:fromXScale=".1" android:fromYScale=".1" android:toXScale="1.0" android:toYScale="1.0" android:duration="2000" /> </set> //Oncreate Animation rotateimage = AnimationUtils.loadAnimation(this, R.anim.custom_anim); imgageview.startAnimation(rotateimage); rotateimage.setAnimationListener(new AnimationListener() { public void onAnimationStart(Animation animation) { // TODO Auto-generated method stub } public void onAnimationRepeat(Animation animation) { // TODO Auto-generated method stub } public void onAnimationEnd(Animation animation) { // TODO Auto-generated method stub AnimateandSlideShow(); } }); //function private void AnimateandSlideShow() { Animation rotateimage = AnimationUtils.loadAnimation(this, R.anim.custom_anim); imgageview.startAnimation(rotateimage); rotateimage.setAnimationListener(new AnimationListener() { public void onAnimationStart(Animation animation) { // TODO Auto-generated method stub } public void onAnimationRepeat(Animation animation) { // TODO Auto-generated method stub } public void onAnimationEnd(Animation animation) { // TODO Auto-generated method stub AnimateandSlideShow(); } }); } 
0
source

I just tried it. For some odd reason, AccelerateDecelerateInterpolator used by default. Add android:interpolator="@android:anim/linear_interpolator" and you will get something more useful.

However, after every 500 ms there is still a slight stop, I think it reloads the animation or has too many frames (0 = 360)

You can increase the values โ€‹โ€‹for a longer period to get a better effect. Try android:duration="50000" and android:toDegrees="36000" .

0
source

Add this property to the rotate attribute

 android:repeatMode="restart" 

Or modify the rotate1.xml file, remove the installation tag. Like this:

 <rotate xmlns:android="http://schemas.android.com/apk/res/android" android:fromDegrees="0" android:toDegrees="360" android:duration="500" android:repeatCount="infinite" android:pivotX="50%" android:pivotY="50%"> </rotate> 
0
source

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


All Articles