Reversing an Animation

前端 未结 8 424
清歌不尽
清歌不尽 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:22

    No, sadly you cannot do it with the Animation object. But you can simulate it using an interpolator that will inverse the animation:

    package com.example.android;
    
    import android.view.animation.Interpolator;
    
    public class ReverseInterpolator implements Interpolator {
        @Override
        public float getInterpolation(float paramFloat) {
            return Math.abs(paramFloat -1f);
        }
    }
    

    Then on your animation you can set your new interpolator:

    myAnimation.setInterpolator(new ReverseInterpolator());
    
    0 讨论(0)
  • 2020-11-30 02:29

    Simplest solution i came up with

    <?xml version="1.0" encoding="utf-8"?>
    <set xmlns:android="http://schemas.android.com/apk/res/android" android:interpolator="@android:anim/linear_interpolator">
        <alpha
            android:duration="2000"
            android:fromAlpha="0.1"
            android:repeatCount="infinite"
            android:repeatMode="reverse"
            android:toAlpha="1.0">
        </alpha>
    </set>
    
    0 讨论(0)
提交回复
热议问题