Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to start Android animation from the values where it was stopped?

I have an image view on which I apply a rotate animation. The animation works fine. On touch down, I attempt to stop the rotate animation using cancel(), resetting the animation using reset() and clearing the animation on the view using clearAnimation(). But the animation comes back to the original position. How to stop animation with the values it was when touch down event happens and restart from where it was stopped?

My animation is defined in the xml as below

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

I am attempting to stop the animation using the following code

private void stopAnimation(){
        mRotateAntiClockwiseAnimation.cancel();
    mRotateAntiClockwiseAnimation.reset();
    imageView.clearAnimation();
    mRotateAntiClockwiseAnimator.end();
    mRotateAntiClockwiseAnimator.cancel();
    stopAnimationForImageButton();
    }

I am setting the animation on my view using the following code

mRotateAntiClockwiseAnimation = AnimationUtils.loadAnimation(context, R.anim.rotate_anticlockwise);
        mRotateAntiClockwiseAnimation.setFillEnabled(true);
        mRotateAntiClockwiseAnimation.setFillAfter(true);
        imageView.setAnimation(mRotateAntiClockwiseAnimation);
        mRotateAntiClockwiseAnimation.startNow();
        imageView.invalidate();

As u see, even using cancel() or reset() did not help to stop the animation at the point where it was touched down. Any pointers would help

like image 852
elizabetht Avatar asked Oct 01 '22 03:10

elizabetht


People also ask

How do I open an animation file on Android?

You can find your animation file by navigating to the app > res > anim > zoomanimation.

How do I set up animator on Android?

Navigate to the app > res > Right-Click on res >> New >> Directory >> Name your directory as “anim”. Inside this directory, we will create our animations. For creating a new anim right click on the anim directory >> Animation Resource file and give the name to your file.

What is transition animation in Android?

Android's transition framework allows you to animate all kinds of motion in your UI by simply providing the starting layout and the ending layout.

Is animation possible on Android?

On Android 4.4 (API level 19) and higher, you can use the transition framework to create animations when you swap the layout within the current activity or fragment. All you need to do is specify the starting and ending layout, and what type of animation you want to use.


1 Answers

I think I got it working by starting the animator and then setting the currentPlayTime(). The documentation clearly tells (which I just stumbled upon) that if the animation has not been started, the currentPlayTime set using this method will not advance the forward!

Sets the position of the animation to the specified point in time. This time should be between 0 and the total duration of the animation, including any repetition. If the animation has not yet been started, then it will not advance forward after it is set to this time; it will simply set the time to this value and perform any appropriate actions based on that time. If the animation is already running, then setCurrentPlayTime() will set the current playing time to this value and continue playing from that point. http://developer.android.com/reference/android/animation/ValueAnimator.html#setCurrentPlayTime(long)

    private void stopAnimation(){
        mCurrentPlayTime = mRotateAntiClockwiseAnimator.getCurrentPlayTime();
        mRotateAntiClockwiseAnimator.cancel();
    }

    private void startAnimation() {
            mRotateAntiClockwiseAnimator.start();
            mRotateAntiClockwiseAnimator.setCurrentPlayTime(mCurrentPlayTime);
    }
like image 155
elizabetht Avatar answered Oct 02 '22 16:10

elizabetht