基本上,为了使您的代码更具可读性,您可以使用内置的AnimatorListenerAdapter将您从匿名内部类混乱中拯救出来。您甚至可以将其抽象化,使其看起来像withEndAction
这样:
import android.animation.Animator;
import android.animation.AnimatorListenerAdapter;
import android.app.Application;
import android.support.v4.view.ViewPropertyAnimatorListener;
import android.view.View;
public class AnimatorActionListener extends AnimatorListenerAdapter {
public enum ActionType {
START, END;
}
final private ActionType type;
final private Runnable action;
public AnimatorActionListener(Runnable action, ActionType type) {
this.action = action;
this.type = type;
}
@Override
public void onAnimationEnd(Animator animation) {
if (type == ActionType.END) {
action.run();
}
}
@Override
public void onAnimationStart(Animator animation) {
if (type == ActionType.START) {
action.run();
}
}
}
然后用法变成:
view.animate()./*...some parameters...*/.setListener(new AnimatorActionListener(new Runnable() {
@Override
public void run() {
// do stuff
}
}, ActionType.END)); // or ActionType.START
而对于那些必须使用平台内置函数的人来说withStartAction, withEndAction
,只需将SDK检查代码AnimatorActionListener
也放入其中,以免客户端类杂乱无章。