您可以使用withEndAction
:
banana.animate().alpha(1).setDuration(3000)
.withEndAction(new Runnable() {
@Override
public void run() {
banana.animate().alpha(0).setDuration(3000);
}
});
您可以改为使用自定义TimeInterpolator会自动反转动画,这里有一个我刚刚敲了起来:
import android.animation.TimeInterpolator;
import android.view.animation.LinearInterpolator;
/**
* Maps time into another interpolator so that the animation plays, and then reverses.
* i.e. time maps like so:
* [0..0.5, 0.5..1] maps to [0..1, 1..0]
*/
public final class AutoReverseInterpolator implements TimeInterpolator {
private final TimeInterpolator inner;
/**
* Decorator constructor, use if you don't want a linear interpolation.
* @param inner
*/
public AutoReverseInterpolator(TimeInterpolator inner) {
this.inner = inner;
}
/**
* Linear interpolation constructor.
*/
public AutoReverseInterpolator() {
this(new LinearInterpolator());
}
@Override
public float getInterpolation(float input) {
input *= 2;
if (input > 1) {
input = 2 - input;
}
return inner.getInterpolation(input);
}
}
用法:
banana.animate().alpha(1).setDuration(6000)
.setInterpolator(new AutoReverseInterpolatorDecorator());
或者对于更复杂的动画链,您应该查看使用xml来定义动画或使用AnimatorSet
。
嘿,谢谢! –
如果你只关心结束状态,你也可以使用['withEndAction()'](https://developer.android.com/reference/android/view/ViewPropertyAnimator.html#withEndAction(java.lang.Runnable))而不是完整的'AnimatorListener'回调。 – ianhanniballake