Alex Kombo
Alex Kombo

Reputation: 3356

Rotating an image continously

I have the following code that attempts to rotate an image continuously:

Animation animation = AnimationUtils.loadAnimation(getActivity(), R.anim.rotate_animation); image.startAnimation(animation);

The rotate_animation.xml file is as follows:

<?xml version="1.0" encoding="utf-8"?> <rotate xmlns:android="http://schemas.android.com/apk/res/android" android:duration="2000" android:fromDegrees="0" android:interpolator="@android:anim/linear_interpolator" android:pivotX="50%" android:pivotY="50%" android:repeatCount="infinite" android:toDegrees="360" />

The problem with the above code is that it rotates the image and pauses before rotating it again. What I am looking for is smooth continuous rotation that that will only stop when I explicitly need it to stop.

Upvotes: 2

Views: 3681

Answers (3)

Avijit Acharjee
Avijit Acharjee

Reputation: 31

Runnable runnable = new Runnable() {
        @Override
        public void run() {
           binding.progressImage.animate().rotationBy(360).withEndAction(this).setDuration(1500).setInterpolator(new LinearInterpolator()).start();
        }
    };

    binding.progressImage.animate().rotationBy(360).withEndAction(runnable).setDuration(1500).setInterpolator(new LinearInterpolator()).start();

Upvotes: 0

Hardian
Hardian

Reputation: 2022

I have successfully used animated-rotate in an activity, see my rotate.xml

<animated-rotate xmlns:android="http://schemas.android.com/apk/res/android"
android:drawable="@drawable/ic_rounded_loading"
android:duration="1000"
android:interpolator="@android:anim/linear_interpolator"
android:pivotX="50%"
android:pivotY="50%"
android:repeatCount="infinite" />

and in activity_main.xml file,

 <ImageView
    android:id="@+id/ivLoading"
    android:layout_width="200px"
    android:layout_height="200px"
    android:layout_centerHorizontal="true"
    android:src="@drawable/rotate"
    android:layout_centerVertical="true" />

But its not working on a fragment. So I figured it in a fragment by below code that rotates an image continously,

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
        Bundle savedInstanceState) {

    // Accessing the layout components to be displayed.
    rootView = inflater.inflate(R.layout.fragment_loading, container, false);
    ivLoading= (ImageView) rootView.findViewById(R.id.ivLoading);

    RotateAnimation rotateAnimation = new RotateAnimation(
            0,//float: Rotation offset to apply at the start of the animation.
            360,//float: Rotation offset to apply at the end of the animation.
            Animation.RELATIVE_TO_SELF,//int: Specifies how pivotXValue should be interpreted
            0.5f,//float: The X coordinate of the point about which the object is being rotated
            Animation.RELATIVE_TO_SELF,//int: Specifies how pivotYValue should be interpreted
            0.5f//float: The Y coordinate of the point about which the object is being rotated
    );
    rotateAnimation.setDuration(1500);//How long this animation should last.
    rotateAnimation.setRepeatCount(Animation.INFINITE);//Sets how many times the animation should be repeated.
    rotateAnimation.setInterpolator(new LinearInterpolator());//Sets the acceleration curve for this animation.
    ivLoading.startAnimation(rotateAnimation);

    return rootView;
}

and in fragment_loading.xml file,

     <ImageView
    android:id="@+id/ivLoading"
    android:layout_width="200px"
    android:layout_height="200px"
    android:layout_centerHorizontal="true"
    android:src="@drawable/ic_rounded_loading"
    android:layout_centerVertical="true" />

Upvotes: 4

user1930106
user1930106

Reputation: 789

Move over to property animators which is now the recommended approach for animations. Try this -

ImageView imageview = (ImageView)findViewById(R.id.yourimage);

ObjectAnimator imageViewObjectAnimator = ObjectAnimator.ofFloat(imageview ,
                    "rotation", 0f, 360f);
imageViewObjectAnimator.setRepeatCount(ObjectAnimator.INFINITE);
imageViewObjectAnimator.setRepeatMode(ObjectAnimator.RESTART);
imageViewObjectAnimator.setInterpolator(new AccelerateInterpolator());
imageViewObjectAnimator.start();

Upvotes: 4

Related Questions