Jelajahi Sumber

Change top/bottom/left/right toolbar animation

cooperku_kdanmobile 5 tahun lalu
induk
melakukan
711a234676

+ 14 - 23
reader/src/main/java/com/kdanmobile/reader/ReaderActivity.kt

@@ -261,12 +261,6 @@ abstract class ReaderActivity : AppCompatActivity() {
 
             override fun onTapMainDocArea() {
                 super.onTapMainDocArea()
-                if (isBelowKitkat()) {
-                    isHideToolbar = toolbar_readerActivity.visibility != View.VISIBLE
-                            || viewGroup_readerActivity_bottomToolbar.visibility != View.VISIBLE
-                            || viewGroup_readerActivity_leftToolbar.visibility != View.VISIBLE
-                            || viewGroup_readerActivity_rightToolbar.visibility != View.VISIBLE
-                }
                 when (isHideToolbar) {
                     true -> showAllToolbars()
                     false -> hideAllToolbars()
@@ -281,12 +275,6 @@ abstract class ReaderActivity : AppCompatActivity() {
             }
 
             override fun onScroll(e1: MotionEvent?, e2: MotionEvent?, distanceX: Float, distanceY: Float): Boolean {
-                if (isBelowKitkat()) {
-                    isHideToolbar = toolbar_readerActivity.visibility != View.VISIBLE
-                            || viewGroup_readerActivity_bottomToolbar.visibility != View.VISIBLE
-                            || viewGroup_readerActivity_leftToolbar.visibility != View.VISIBLE
-                            || viewGroup_readerActivity_rightToolbar.visibility != View.VISIBLE
-                }
                 if (!isHideToolbar) {
                     hideAllToolbars()
                     isHideToolbar = true
@@ -321,10 +309,11 @@ abstract class ReaderActivity : AppCompatActivity() {
 
     private fun showAllToolbars() {
         if (isBelowKitkat()) {
-            AnimationUtil.showViewFromTopToBottom(toolbar_readerActivity, UI_ANIMATION_DURATION)
-            AnimationUtil.showViewFromBottomToTop(viewGroup_readerActivity_bottomToolbar, UI_ANIMATION_DURATION)
-            AnimationUtil.showViewFromLeftToRight(viewGroup_readerActivity_leftToolbar, UI_ANIMATION_DURATION)
-            AnimationUtil.showViewFromRightToLeft(viewGroup_readerActivity_rightToolbar, UI_ANIMATION_DURATION)
+            val duration = ((1 - toolbar_readerActivity.alpha) * UI_ANIMATION_DURATION).toLong()
+            AnimationUtil.showViewFromTopToBottom(toolbar_readerActivity, duration)
+            AnimationUtil.showViewFromBottomToTop(viewGroup_readerActivity_bottomToolbar, duration)
+            AnimationUtil.showViewFromLeftToRight(viewGroup_readerActivity_leftToolbar, duration)
+            AnimationUtil.showViewFromRightToLeft(viewGroup_readerActivity_rightToolbar, duration)
         } else {
             constrainLayout_readerActivity_root.applyConstraintSet(originConstraintSet, UI_ANIMATION_DURATION)
         }
@@ -332,10 +321,11 @@ abstract class ReaderActivity : AppCompatActivity() {
 
     private fun hideAllToolbars() {
         if (isBelowKitkat()) {
-            AnimationUtil.hideViewFromBottomToTop(toolbar_readerActivity, UI_ANIMATION_DURATION)
-            AnimationUtil.hideViewFromTopToBottom(viewGroup_readerActivity_bottomToolbar, UI_ANIMATION_DURATION)
-            AnimationUtil.hideViewFromRightToLeft(viewGroup_readerActivity_leftToolbar, UI_ANIMATION_DURATION)
-            AnimationUtil.hideViewFromLeftToRight(viewGroup_readerActivity_rightToolbar, UI_ANIMATION_DURATION)
+            val duration = (toolbar_readerActivity.alpha * UI_ANIMATION_DURATION).toLong()
+            AnimationUtil.hideViewFromBottomToTop(toolbar_readerActivity, duration)
+            AnimationUtil.hideViewFromTopToBottom(viewGroup_readerActivity_bottomToolbar, duration)
+            AnimationUtil.hideViewFromRightToLeft(viewGroup_readerActivity_leftToolbar, duration)
+            AnimationUtil.hideViewFromLeftToRight(viewGroup_readerActivity_rightToolbar, duration)
             hideSearchViewSoftKeyboard()
         } else {
             constrainLayout_readerActivity_root.applyConstraintSet(this, R.layout.activity_reader_hide_all, UI_ANIMATION_DURATION)
@@ -344,9 +334,10 @@ abstract class ReaderActivity : AppCompatActivity() {
 
     private fun hideTopLeftBottomToolbars() {
         if (isBelowKitkat()) {
-            AnimationUtil.hideViewFromBottomToTop(toolbar_readerActivity, UI_ANIMATION_DURATION)
-            AnimationUtil.hideViewFromTopToBottom(viewGroup_readerActivity_bottomToolbar, UI_ANIMATION_DURATION)
-            AnimationUtil.hideViewFromRightToLeft(viewGroup_readerActivity_leftToolbar, UI_ANIMATION_DURATION)
+            val duration = (toolbar_readerActivity.alpha * UI_ANIMATION_DURATION).toLong()
+            AnimationUtil.hideViewFromBottomToTop(toolbar_readerActivity, duration)
+            AnimationUtil.hideViewFromTopToBottom(viewGroup_readerActivity_bottomToolbar, duration)
+            AnimationUtil.hideViewFromRightToLeft(viewGroup_readerActivity_leftToolbar, duration)
             hideSearchViewSoftKeyboard()
         } else {
             constrainLayout_readerActivity_root.applyConstraintSet(this, R.layout.activity_reader_hide_top_left_bottom, UI_ANIMATION_DURATION)

+ 217 - 116
reader/src/main/java/com/kdanmobile/reader/utils/AnimationUtil.kt

@@ -72,88 +72,141 @@ class AnimationUtil private constructor() {
         }
 
         /**
-         * 隐藏buttons控件,从控件顶部往底部回收隐藏
+         * 显示buttons控件,从控件底部往控件顶部扩展显示
          *
          * @param view
          */
-        fun hideViewFromTopToBottom(view: View?, duration: Long = 300) {
-            if (showBottomToTop != null && showBottomToTop!!.isRunning())
-                return;
+        fun showViewFromBottomToTop(view: View?, duration: Long = 300) {
+            if (showBottomToTop != null && showBottomToTop!!.isRunning) {
+                if (showBottomToTop?.target == view) {
+                    showBottomToTop?.cancel()
+                    showBottomToTop = null
+                } else {
+                    return
+                }
+            }
 
-            if (view != null && view.visibility == View.VISIBLE) {
-                val pvhA = PropertyValuesHolder.ofFloat("alpha", 1.0f, 0f)
-                val pvhY = PropertyValuesHolder.ofFloat("Y", view.top.toFloat(), view.top.toFloat() + view.height)
+            if (view != null) {
+                val pvhA = PropertyValuesHolder.ofFloat("alpha", view.alpha, 1.0f)
+                val pvhY = PropertyValuesHolder.ofFloat("Y", Math.max(view.y, view.top.toFloat() + view.height), view.top.toFloat())
                 showBottomToTop = ObjectAnimator.ofPropertyValuesHolder(view, pvhA, pvhY)
-                showBottomToTop!!.interpolator = AccelerateInterpolator()
-                showBottomToTop!!.addListener(object : AnimatorListenerAdapter() {
-                    override fun onAnimationEnd(animation: Animator) {
-                        super.onAnimationEnd(animation)
-                        Handler(Looper.getMainLooper()).post { view.visibility = View.INVISIBLE }
-                    }
-                })
-                showBottomToTop!!.setDuration(duration).start()
+                showBottomToTop?.apply {
+                    interpolator = AccelerateInterpolator()
+                    addListener(object : AnimatorListenerAdapter() {
+                        override fun onAnimationStart(animation: Animator) {
+                            super.onAnimationStart(animation)
+                            view.visibility = View.VISIBLE
+                        }
+
+                        override fun onAnimationCancel(animation: Animator?) {
+                            super.onAnimationCancel(animation)
+                            animation?.removeAllListeners()
+                        }
+                    })
+                    setDuration(duration).start()
+                }
             }
         }
 
         /**
-         * 显示buttons控件,从控件底部往控件顶部扩展显示
+         * 隐藏buttons控件,从控件顶部往底部回收隐藏
          *
          * @param view
          */
-        fun showViewFromBottomToTop(view: View?, duration: Long = 300) {
-            if (showBottomToTop != null && showBottomToTop!!.isRunning())
-                return;
+        fun hideViewFromTopToBottom(view: View?, duration: Long = 300) {
+            if (showBottomToTop != null && showBottomToTop!!.isRunning) {
+                if (showBottomToTop?.target == view) {
+                    showBottomToTop?.cancel()
+                    showBottomToTop = null
+                } else {
+                    return
+                }
+            }
 
-            if (view != null && view.visibility != View.VISIBLE) {
-                val pvhA = PropertyValuesHolder.ofFloat("alpha", 0f, 1.0f)
-                val pvhY = PropertyValuesHolder.ofFloat("Y", view.top.toFloat() + view.height, view.top.toFloat())
+            if (view != null) {
+                val pvhA = PropertyValuesHolder.ofFloat("alpha", view.alpha, 0f)
+                val pvhY = PropertyValuesHolder.ofFloat("Y", Math.min(view.y, view.top.toFloat()), view.top.toFloat() + view.height)
                 showBottomToTop = ObjectAnimator.ofPropertyValuesHolder(view, pvhA, pvhY)
-                showBottomToTop!!.interpolator = AccelerateInterpolator()
-                showBottomToTop!!.addListener(object : AnimatorListenerAdapter() {
-                    override fun onAnimationStart(animation: Animator) {
-                        super.onAnimationStart(animation)
-                        view.visibility = View.VISIBLE
-                    }
-                })
-                showBottomToTop!!.setDuration(duration).start()
+                showBottomToTop = ObjectAnimator.ofPropertyValuesHolder(view, pvhA, pvhY)
+                showBottomToTop?.apply {
+                    interpolator = AccelerateInterpolator()
+                    addListener(object : AnimatorListenerAdapter() {
+                        override fun onAnimationEnd(animation: Animator) {
+                            super.onAnimationEnd(animation)
+                            view.visibility = View.INVISIBLE
+                        }
+
+                        override fun onAnimationCancel(animation: Animator?) {
+                            super.onAnimationCancel(animation)
+                            animation?.removeAllListeners()
+                        }
+                    })
+                    setDuration(duration).start()
+                }
             }
         }
 
         fun showViewFromRightToLeft(view: View?, duration: Long = 300) {
-            if (showRightToLeft != null && showRightToLeft!!.isRunning)
-                return
+            if (showRightToLeft != null && showRightToLeft!!.isRunning) {
+                if (showRightToLeft?.target == view) {
+                    showRightToLeft?.cancel()
+                    showRightToLeft = null
+                } else {
+                    return
+                }
+            }
 
-            if (view != null && view.visibility != View.VISIBLE) {
-                val pvhA = PropertyValuesHolder.ofFloat("alpha", 0f, 1.0f)
-                val pvhX = PropertyValuesHolder.ofFloat("X", view.left.toFloat() + view.width, view.left.toFloat())
+            if (view != null) {
+                val pvhA = PropertyValuesHolder.ofFloat("alpha", view.alpha, 1.0f)
+                val pvhX = PropertyValuesHolder.ofFloat("X", Math.min(view.x, view.left.toFloat() + view.width), view.left.toFloat())
                 showRightToLeft = ObjectAnimator.ofPropertyValuesHolder(view, pvhA, pvhX)
-                showRightToLeft!!.interpolator = AccelerateInterpolator()
-                showRightToLeft!!.addListener(object : AnimatorListenerAdapter() {
-                    override fun onAnimationStart(animation: Animator) {
-                        super.onAnimationStart(animation)
-                        view.visibility = View.VISIBLE
-                    }
-                })
-                showRightToLeft!!.setDuration(duration).start()
+                showRightToLeft?.apply {
+                    interpolator = AccelerateInterpolator()
+                    addListener(object : AnimatorListenerAdapter() {
+                        override fun onAnimationStart(animation: Animator) {
+                            super.onAnimationStart(animation)
+                            view.visibility = View.VISIBLE
+                        }
+
+                        override fun onAnimationCancel(animation: Animator?) {
+                            super.onAnimationCancel(animation)
+                            animation?.removeAllListeners()
+                        }
+                    })
+                    setDuration(duration).start()
+                }
             }
         }
 
         fun hideViewFromLeftToRight(view: View?, duration: Long = 300) {
-            if (showRightToLeft != null && showRightToLeft!!.isRunning)
-                return
+            if (showRightToLeft != null && showRightToLeft!!.isRunning) {
+                if (showRightToLeft?.target == view) {
+                    showRightToLeft?.cancel()
+                    showRightToLeft = null
+                } else {
+                    return
+                }
+            }
 
-            if (view != null && view.visibility == View.VISIBLE) {
-                val pvhA = PropertyValuesHolder.ofFloat("alpha", 1.0f, 0f)
-                val pvhX = PropertyValuesHolder.ofFloat("X", view.left.toFloat(), view.left.toFloat() + view.width)
+            if (view != null) {
+                val pvhA = PropertyValuesHolder.ofFloat("alpha", view.alpha, 0f)
+                val pvhX = PropertyValuesHolder.ofFloat("X", Math.max(view.x, view.left.toFloat()), view.left.toFloat() + view.width)
                 showRightToLeft = ObjectAnimator.ofPropertyValuesHolder(view, pvhA, pvhX)
-                showRightToLeft!!.interpolator = AccelerateInterpolator()
-                showRightToLeft!!.addListener(object : AnimatorListenerAdapter() {
-                    override fun onAnimationEnd(animation: Animator) {
-                        super.onAnimationEnd(animation)
-                        Handler(Looper.getMainLooper()).post { view.visibility = View.INVISIBLE }
-                    }
-                })
-                showRightToLeft!!.setDuration(duration).start()
+                showRightToLeft?.apply {
+                    interpolator = AccelerateInterpolator()
+                    addListener(object : AnimatorListenerAdapter() {
+                        override fun onAnimationEnd(animation: Animator) {
+                            super.onAnimationEnd(animation)
+                            view.visibility = View.INVISIBLE
+                        }
+
+                        override fun onAnimationCancel(animation: Animator?) {
+                            super.onAnimationCancel(animation)
+                            animation?.removeAllListeners()
+                        }
+                    })
+                    setDuration(duration).start()
+                }
             }
         }
 
@@ -163,21 +216,34 @@ class AnimationUtil private constructor() {
          * @param view
          */
         fun showViewFromTopToBottom(view: View?, duration: Long = 300) {
-            if (showTopToBottom != null && showTopToBottom!!.isRunning)
-                return
+            if (showTopToBottom != null && showTopToBottom!!.isRunning) {
+                if (showTopToBottom?.target == view) {
+                    showTopToBottom?.cancel()
+                    showTopToBottom = null
+                } else {
+                    return
+                }
+            }
 
-            if (view != null && view.visibility != View.VISIBLE) {
-                val pvhA = PropertyValuesHolder.ofFloat("alpha", 0f, 1.0f)
-                val pvhY = PropertyValuesHolder.ofFloat("Y", view.top.toFloat() - view.height, view.top.toFloat())
+            if (view != null) {
+                val pvhA = PropertyValuesHolder.ofFloat("alpha", view.alpha, 1.0f)
+                val pvhY = PropertyValuesHolder.ofFloat("Y", Math.min(view.y, view.top.toFloat() + view.height), view.top.toFloat())
                 showTopToBottom = ObjectAnimator.ofPropertyValuesHolder(view, pvhA, pvhY)
-                showTopToBottom!!.interpolator = AccelerateInterpolator()
-                showTopToBottom!!.addListener(object : AnimatorListenerAdapter() {
-                    override fun onAnimationStart(animation: Animator) {
-                        super.onAnimationStart(animation)
-                        view.visibility = View.VISIBLE
-                    }
-                })
-                showTopToBottom!!.setDuration(duration).start()
+                showTopToBottom?.apply {
+                    interpolator = AccelerateInterpolator()
+                    addListener(object : AnimatorListenerAdapter() {
+                        override fun onAnimationStart(animation: Animator) {
+                            super.onAnimationStart(animation)
+                            view.visibility = View.VISIBLE
+                        }
+
+                        override fun onAnimationCancel(animation: Animator?) {
+                            super.onAnimationCancel(animation)
+                            animation?.removeAllListeners()
+                        }
+                    })
+                    setDuration(duration).start()
+                }
             }
         }
 
@@ -187,59 +253,98 @@ class AnimationUtil private constructor() {
          * @param view
          */
         fun hideViewFromBottomToTop(view: View?, duration: Long = 300) {
-            if (showTopToBottom != null && showTopToBottom!!.isRunning)
-                return
+            if (showTopToBottom != null && showTopToBottom!!.isRunning) {
+                if (showTopToBottom?.target == view) {
+                    showTopToBottom?.cancel()
+                    showTopToBottom = null
+                } else {
+                    return
+                }
+            }
 
-            if (view != null && view.visibility == View.VISIBLE) {
-                val pvhA = PropertyValuesHolder.ofFloat("alpha", 1.0f, 0f)
-                val pvhY = PropertyValuesHolder.ofFloat("Y", view.top.toFloat(), view.top.toFloat() - view.height)
+            if (view != null) {
+                val pvhA = PropertyValuesHolder.ofFloat("alpha", view.alpha, 0f)
+                val pvhY = PropertyValuesHolder.ofFloat("Y", Math.max(view.y, view.top.toFloat()), view.top.toFloat() - view.height)
                 showTopToBottom = ObjectAnimator.ofPropertyValuesHolder(view, pvhA, pvhY)
-                showTopToBottom!!.interpolator = AccelerateInterpolator()
-                showTopToBottom!!.addListener(object : AnimatorListenerAdapter() {
-                    override fun onAnimationEnd(animation: Animator) {
-                        super.onAnimationEnd(animation)
-                        Handler(Looper.getMainLooper()).post { view.visibility = View.INVISIBLE }
-                    }
-                })
-                showTopToBottom!!.setDuration(duration).start()
+                showTopToBottom?.apply {
+                    interpolator = AccelerateInterpolator()
+                    addListener(object : AnimatorListenerAdapter() {
+                        override fun onAnimationEnd(animation: Animator) {
+                            super.onAnimationEnd(animation)
+                            view.visibility = View.INVISIBLE
+                        }
+
+                        override fun onAnimationCancel(animation: Animator?) {
+                            super.onAnimationCancel(animation)
+                            animation?.removeAllListeners()
+                        }
+                    })
+                    setDuration(duration).start()
+                }
             }
         }
 
         fun showViewFromLeftToRight(view: View?, duration: Long = 300) {
-            if (showLeftToRight != null && showLeftToRight!!.isRunning)
-                return
+            if (showLeftToRight != null && showLeftToRight!!.isRunning) {
+                if (showLeftToRight?.target == view) {
+                    showLeftToRight?.cancel()
+                    showLeftToRight = null
+                } else {
+                    return
+                }
+            }
 
-            if (view != null && view.visibility != View.VISIBLE) {
-                val pvhA = PropertyValuesHolder.ofFloat("alpha", 0f, 1.0f)
-                val pvhX = PropertyValuesHolder.ofFloat("X", view.left.toFloat() - view.width, view.left.toFloat())
+            if (view != null) {
+                val pvhA = PropertyValuesHolder.ofFloat("alpha", view.alpha, 1.0f)
+                val pvhX = PropertyValuesHolder.ofFloat("X", Math.max(view.x, view.left.toFloat() - view.width), view.left.toFloat())
                 showLeftToRight = ObjectAnimator.ofPropertyValuesHolder(view, pvhA, pvhX)
-                showLeftToRight!!.interpolator = AccelerateInterpolator()
-                showLeftToRight!!.addListener(object : AnimatorListenerAdapter() {
-                    override fun onAnimationStart(animation: Animator) {
-                        super.onAnimationStart(animation)
-                        view.visibility = View.VISIBLE
-                    }
-                })
-                showLeftToRight!!.setDuration(duration).start()
+                showLeftToRight?.apply {
+                    interpolator = AccelerateInterpolator()
+                    addListener(object : AnimatorListenerAdapter() {
+                        override fun onAnimationStart(animation: Animator) {
+                            super.onAnimationStart(animation)
+                            view.visibility = View.VISIBLE
+                        }
+
+                        override fun onAnimationCancel(animation: Animator?) {
+                            super.onAnimationCancel(animation)
+                            animation?.removeAllListeners()
+                        }
+                    })
+                    setDuration(duration).start()
+                }
             }
         }
 
         fun hideViewFromRightToLeft(view: View?, duration: Long = 300) {
-            if (showLeftToRight != null && showLeftToRight!!.isRunning)
-                return
+            if (showLeftToRight != null && showLeftToRight!!.isRunning) {
+                if (showLeftToRight?.target == view) {
+                    showLeftToRight?.cancel()
+                    showLeftToRight = null
+                } else {
+                    return
+                }
+            }
 
-            if (view != null && view.visibility == View.VISIBLE) {
-                val pvhA = PropertyValuesHolder.ofFloat("alpha", 1.0f, 0f)
-                val pvhX = PropertyValuesHolder.ofFloat("X", view.left.toFloat(), view.left.toFloat() - view.width)
+            if (view != null) {
+                val pvhA = PropertyValuesHolder.ofFloat("alpha", view.alpha, 0f)
+                val pvhX = PropertyValuesHolder.ofFloat("X", Math.min(view.x, view.left.toFloat()), view.left.toFloat() - view.width)
                 showLeftToRight = ObjectAnimator.ofPropertyValuesHolder(view, pvhA, pvhX)
-                showLeftToRight!!.interpolator = AccelerateInterpolator()
-                showLeftToRight!!.addListener(object : AnimatorListenerAdapter() {
-                    override fun onAnimationEnd(animation: Animator) {
-                        super.onAnimationEnd(animation)
-                        Handler(Looper.getMainLooper()).post { view.visibility = View.INVISIBLE }
-                    }
-                })
-                showLeftToRight!!.setDuration(duration).start()
+                showLeftToRight?.apply {
+                    interpolator = AccelerateInterpolator()
+                    addListener(object : AnimatorListenerAdapter() {
+                        override fun onAnimationEnd(animation: Animator) {
+                            super.onAnimationEnd(animation)
+                            view.visibility = View.INVISIBLE
+                        }
+
+                        override fun onAnimationCancel(animation: Animator?) {
+                            super.onAnimationCancel(animation)
+                            animation?.removeAllListeners()
+                        }
+                    })
+                    setDuration(duration).start()
+                }
             }
         }
 
@@ -277,14 +382,10 @@ class AnimationUtil private constructor() {
          * 结束该动画
          */
         fun cancelAnimations() {
-            if (showTopToBottom != null)
-                showTopToBottom!!.cancel()
-            if (showBottomToTop != null)
-                showBottomToTop!!.cancel()
-            if (showLeftToRight != null)
-                showLeftToRight!!.cancel()
-            if (showRightToLeft != null)
-                showRightToLeft!!.cancel()
+            showTopToBottom?.cancel()
+            showBottomToTop?.cancel()
+            showLeftToRight?.cancel()
+            showRightToLeft?.cancel()
             showTopToBottom = null
             showBottomToTop = null
             showRightToLeft = null