我经常使用 ViewPropertyAnimator 并使用它的 withEndAction() 函数设置结束 Action ,例如:
view.animate().translationY(0).withEndAction(new Runnable() {
@Override
public void run() {
// do something
}
}).start();
但您也可以设置结束 Action 设置特殊监听器,例如:
view.animate().translationY(0).setListener(new AnimatorListenerAdapter() {
@Override
public void onAnimationEnd(Animator animation) {
// do something
}
});
那么这两种方法有什么区别,什么时候应该使用它们呢?
最佳答案
没有太大区别,看看souce code .
两者都在 onAnimationEnd
上执行。
但是 runnable 在启动后会被删除。 所以 Runnable 只执行一次,而 Listener 可能会被调用多次。
@Override
public void onAnimationEnd(Animator animation) {
mView.setHasTransientState(false);
if (mListener != null) {
mListener.onAnimationEnd(animation); // this is your listener
}
if (mAnimatorOnEndMap != null) {
Runnable r = mAnimatorOnEndMap.get(animation); // this is your runnable
if (r != null) {
r.run();
}
mAnimatorOnEndMap.remove(animation);
}
if (mAnimatorCleanupMap != null) {
Runnable r = mAnimatorCleanupMap.get(animation);
if (r != null) {
r.run();
}
mAnimatorCleanupMap.remove(animation);
}
mAnimatorMap.remove(animation);
}
关于android animate() withEndAction() vs setListener() onAnimationEnd(),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34152421/