android animate() withEndAction() vs setListener() onAnimationEnd()

前端 未结 1 713
伪装坚强ぢ
伪装坚强ぢ 2021-02-12 09:39

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

view.animate().translationY(0).withEndAction(new Runnable() {
    @O         


        
相关标签:
1条回答
  • 2021-02-12 10:16

    There is no big difference, take a look at the souce code.

    Both are executed on onAnimationEnd.

    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);
    }
    
    0 讨论(0)
提交回复
热议问题