文章

ViewPropertyAnimator

ViewPropertyAnimator 使用

  • 原始 ViewPropertyAnimator

需要手动 cancel 动画

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
mContentContainer.animate()
    .setDuration(5000L)
    .translationX(mContentContainer.width.toFloat())
    .setListener(object : AnimatorListenerAdapter() {
        override fun onAnimationStart(animation: Animator) {
            super.onAnimationStart(animation)
            Logger.i(TAG, "onAnimationStart:$animation")
        }
    
        override fun onAnimationEnd(animation: Animator) {
            super.onAnimationEnd(animation)
            Logger.d(
                TAG,
                "collapse onAnimationEnd, mContentContainer gone, mHandleContainer visible mContentContainer.isAttachedToWindow=${mContentContainer.isAttachedToWindow}, mContentContainer.width=${mContentContainer.width}."
            )
            mContentContainer.gone()
            mHandleContainer.visible()
        }
    })
    .start()
  • Compat 库

ViewPropertyAnimatorCompat 是用一个 WeakHashMap 将 View 存起来的,可以不用 cancel 动画

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
ViewCompat.animate(mContentContainer)
    .setDuration(5_000L)
    .translationX(mContentContainer.width.toFloat())
    .setListener(object : ViewPropertyAnimatorListener {
        override fun onAnimationStart(view: View) {
        }

        override fun onAnimationEnd(view: View) {
            Logger.d(
                TAG,
                "collapse onAnimationEnd, mContentContainer gone, mHandleContainer visible mContentContainer.isAttachedToWindow=${mContentContainer.isAttachedToWindow}, mContentContainer.width=${mContentContainer.width}."
            )
            mContentContainer.gone()
            mHandleContainer.visible()
        }

        override fun onAnimationCancel(view: View) {
            Logger.w(
                TAG,
                "collapse onAnimationCancel"
            )
        }
    })
    .start()
本文由作者按照 CC BY 4.0 进行授权