android animate()withEndAction()与setListener()onAnimationEnd() [英] android animate() withEndAction() vs setListener() onAnimationEnd()

查看:415
本文介绍了android animate()withEndAction()与setListener()onAnimationEnd()的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

通常我使用ViewPropertyAnimator并使用其withEndAction()函数来设置结束动作,例如:

Often I use ViewPropertyAnimator and set end action using its withEndAction() function like:

view.animate().translationY(0).withEndAction(new Runnable() {
    @Override
    public void run() {
        // do something
    }
}).start();

但是您也可以设置结束动作设置特殊的侦听器,例如:

But also you can set end action setting special listener like:

view.animate().translationY(0).setListener(new AnimatorListenerAdapter() {
    @Override
    public void onAnimationEnd(Animator animation) {
        // do something
    }
});

那么这两种方法之间的区别是什么?何时应该使用它们?

So what is the difference between these two approaches and when I should use each of them?

推荐答案

没有太大区别,请看两者均在onAnimationEnd上执行.

但是可运行对象在启动后被删除. 因此,Runnable仅执行一次,并且监听器可能会多次调用.

But the runnable gets removed after it was started. So The Runnable is just executed once and the Listener might be called multiple times.

@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()与setListener()onAnimationEnd()的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆