vkislicins
vkislicins

Reputation: 3391

Getting progress of the rotation animation on Android

I need to cancel my rotation animation, get the degrees it was cancelled, and set a new animation using the degrees value as fromValue.

I've tried the following to get the progress of the animation when cancel is called:

 @Override
    public void onAnimationEnd(Animation animation) {
        mAnimatingRotation = false;
        Transformation transformation = new Transformation();
        animation.getTransformation(AnimationUtils.currentAnimationTimeMillis(),transformation);
        Matrix matrix = transformation.getMatrix();
    }

However I'm not getting the same transformation object as the one inside the animation object. What I get looks like this:

Transformation{alpha=1.0 matrix=[1.0, -0.0, 0.0][0.0, 1.0, 0.0][0.0, 0.0, 1.0]}

I think the problem in in AnimationUtils.currectAnimationTimeMillis() and that it is not the time that I should pass to the method. I've also tried passing in System.currentTimeMillis() with thesame result

Upvotes: 1

Views: 234

Answers (1)

Pedro Oliveira
Pedro Oliveira

Reputation: 20500

You can use ViewPropertyAnimator which retains it's value over the view. Here is a simple example:

    myView.animate().rotation(90).setDuration(5000);
    new Handler().postDelayed(new Runnable() {
        @Override
        public void run() {
            myView.animate().cancel();
            new Handler().postDelayed(new Runnable() {
                @Override
                public void run() {
                    myView.animate().rotation(90).setDuration(3000);
                }
            }, 2000);
        }
    }, 2000);

This will rotate the view to 90 degrees. The initial duration is 5 seconds. It will stop the animation at 2seconds and after 2 seconds it will continue the animation again.

p.s the handlers are just to show you the effect :)

Upvotes: 2

Related Questions