Reversing an Animation

前端 未结 8 423
清歌不尽
清歌不尽 2020-11-30 01:41

I have an ImageView that gets animated when it is added to a layout. When it is removed, I want to reverse the same animation.

Is there a way to reverse an animation

相关标签:
8条回答
  • 2020-11-30 02:06

    If you are using Object or ValueAnimator to animate the view, you can simply do

    ValueAnimator myAnimator = new ValueAnimator();  
    myAnimator.reverse()
    

    Documentation can be found here.

    0 讨论(0)
  • 2020-11-30 02:07

    Based on pcans idea, you can reverse any interpolator, not just linear.

    class ReverseInterpolator implements Interpolator{
        private final Interpolator delegate;
    
        public ReverseInterpolator(Interpolator delegate){
            this.delegate = delegate;
        }
    
        public ReverseInterpolator(){
            this(new LinearInterpolator());
        }
    
        @Override
        public float getInterpolation(float input) {
            return 1 - delegate.getInterpolation(input);
        }
    }
    

    Usage

    ReverseInterpolator reverseInterpolator = new ReverseInterpolator(new AccelerateInterpolator())
    myAnimation.setInterpolator(reverseInterpolator);
    
    0 讨论(0)
  • 2020-11-30 02:12

    this worked for me

     ObjectAnimator anim = ObjectAnimator.ofFloat(imageViewUpb, "rotation", rotationAngle, rotationAngle + 180);
    
                if (linearLayoutb.getVisibility()==GONE){
    
                    linearLayoutb.setVisibility(VISIBLE);
                    anim.setDuration(500);
                    anim.start();
                    rotationAngle += 180;
                    rotationAngle = rotationAngle%360;
            imageViewUpb.animate().rotation(rotationAngle).setDuration(500).start();
    
                }else{
    
                    linearLayoutb.setVisibility(GONE);
                    anim.setDuration(500);
                    anim.start();
                    rotationAngle += 180;
                    rotationAngle = rotationAngle%180;
    imageViewUpDownb.animate().rotation(rotationAngle).setDuration(500).start();
    
                }
    

    linearlayoutb is the view that expands when the imageviewUpb faces up

    make int rotationAngle = 0; global parameter

    0 讨论(0)
  • 2020-11-30 02:14

    You can make the code remember the original position and the end position. And let your code dynamically get those values when triggering animation.

    0 讨论(0)
  • 2020-11-30 02:17

    If you are using animation from xml then an easy way is to made an exact same reverse animation to original animation. Add Animation.AnimationListener to original animation and in onAnimationEnd method start the reverse animation.

    0 讨论(0)
  • I have a similar approach to pcans buts slightly different. It takes an Interpolator and will effectively pass out values that would be the same as using the passed in Interpolator normally and then in REVERSE mode. Saves you having to think about the buggy implementations of Animation.REVERSE across Android. See the code here

    public class ReverseInterpolator implements Interpolator {
    
        private final Interpolator mInterpolator;
    
        public ReverseInterpolator(Interpolator interpolator){
            mInterpolator = interpolator;
        }
    
        @Override
        public float getInterpolation(float input) {
            return mInterpolator.getInterpolation(reverseInput(input));
        }
    
        /**
         * Map value so 0-0.5 = 0-1 and 0.5-1 = 1-0
         */
        private float reverseInput(float input){        
            if(input <= 0.5)
                return input*2;
            else
                return Math.abs(input-1)*2;        
        }
    }
    
    0 讨论(0)
提交回复
热议问题