How to reset view to original state after using animators to animates its some properties?
Asked Answered
T

5

47

I am using animators (ObjectAnimator) to animate few properties (scale, rotate) of a view.

Target view is animating properly when ObjectAnimators are set to it.

But there is an added requirement to get view to the original position (reset) after a while.

I tried to cancel() the animator but it only cancels the animation and doesn't reset the view.

Possible solution : creating another animator that does just opposite of the initial animator.

Is there any other way to reset it ?

Tweeze answered 16/6, 2015 at 5:39 Comment(6)
does it need to reset in the same animation? like fadein-fadeout?Levelheaded
I have set duration to it. once it is completed I want to reset it to its original unanimated state like it was before animation. I need same implementation that occurs with view animation when we have fillEnabled = false.Tweeze
try custom interpolator then, something like CycleInterpolatorLevelheaded
@Levelheaded CycleInterpolator will work in my case with some changes that I need to make with cycles and duration adjustments. Thanks.Tweeze
that's why i suggested a custom Interpolator...Levelheaded
Can you give code sample? How will using CycleInterpolator work except repeating the animation for a specified number of cycles?Ghibelline
J
1

I met the same problem, because I animate the view use ViewPropertyAnimator, addView() and removeView() again and again for not creating new view, but a view can be shown once, when you remove the view, you call addview() again, it is no show, but you see the property visibility is visible and animationListener also be called. it is strange.

Joslyn answered 21/11, 2016 at 6:57 Comment(0)
G
1

See the solution I came up with as I had a similar issue with animations inside views in a recycler view, so I had to find a way to reset them:

Trying to reset values from Property Animator to be used in recycler view

Good news in Android O these will be better supported

Ghibelline answered 16/8, 2017 at 18:39 Comment(0)
D
1

I had the Same problem with my views . so I came over this problem with this trick . So We assume we have a view that applying an animate after clicking on it .


          viewLayout.arrow_back.setOnClickListener {arrow ->
            arrow.animate().rotationXBy(50F) // this is a just simple anim not so usefull
                .start()
            arrow.isEnabled = false
        }

I hope it'll Help someone .

Disconcert answered 8/12, 2020 at 16:54 Comment(0)
S
1

Both ObjectAnimator.cancel() and ObjectAnimator.end() don't cancel the effects the animation had on the animated object, but rather just cancel its execution at the point in which the methods were called (see ValueAnimator's documentation for more details).

Now, I can think of three main ways to accomplish what you wanted:

  1. Put a listener on the ObjectAnimator, and when OnAnimationEnd/OnAnimationCancel is called, manully reset the properties of the view.
  2. If the properties of the view you want to reset are those given to it in the XML file, you can re-inflate it. For more details, check out this answer.
  3. Use the ObjectAnimator.reverse() method to reverse the execution of the animation, effectively reseting the properties of the view. Preferably, you would want to call start the reverse when the animation ends, so you'd probably need to put a listener on the animator and check when OnAnimationEnd with the animtor to reverse is called.
Spaak answered 14/4, 2022 at 18:53 Comment(0)
W
0

Do you mean stop a running animation? If so, call clearAnimation() to remove animations from the views that you called startAnimation(); If what you mean is to reset the view to its original appearance after the animation is over, always setFillAfter(false); to the animations.

Whitt answered 16/6, 2015 at 5:45 Comment(2)
I mean to reset the view to unanimated position as it was before applying animators. clearAnimation() doesn't work. Also it is PropertyAnimation not usual view animations.Tweeze
This is the correct solution for resetting an animation. setFillAfter(false); did the trick perfectly.Abaft

© 2022 - 2024 — McMap. All rights reserved.