Bläddra i källkod

Add AnimationUtil & add parameter 'duration' to ConstraintLayout.applyConstraintSet

cooperku_kdanmobile 5 år sedan
förälder
incheckning
fe2d56903e

+ 7 - 4
reader/src/main/java/com/kdanmobile/reader/Utils.kt

@@ -12,6 +12,7 @@ import android.support.v4.content.ContextCompat
 import android.support.v4.graphics.drawable.DrawableCompat
 import android.support.v7.widget.AppCompatImageButton
 import android.support.v7.widget.AppCompatImageView
+import android.transition.AutoTransition
 import android.transition.TransitionManager
 import android.view.View
 import android.widget.ImageView
@@ -54,15 +55,17 @@ object Utils {
     }
 
     @RequiresApi(Build.VERSION_CODES.KITKAT)
-    fun ConstraintLayout.applyConstraintSet(constraintSet: ConstraintSet) {
-        TransitionManager.beginDelayedTransition(this)
+    fun ConstraintLayout.applyConstraintSet(constraintSet: ConstraintSet, duration: Long = 300) {
+        val transition = AutoTransition()
+        transition.duration = duration
+        TransitionManager.beginDelayedTransition(this, transition)
         constraintSet.applyTo(this)
     }
 
     @RequiresApi(Build.VERSION_CODES.KITKAT)
-    fun ConstraintLayout.applyConstraintSet(context: Context, @LayoutRes layoutResId: Int) {
+    fun ConstraintLayout.applyConstraintSet(context: Context, @LayoutRes layoutResId: Int, duration: Long = 300) {
         val constraintSet = ConstraintSet()
         constraintSet.clone(context, layoutResId)
-        this.applyConstraintSet(constraintSet)
+        this.applyConstraintSet(constraintSet, duration)
     }
 }

+ 294 - 0
reader/src/main/java/com/kdanmobile/reader/utils/AnimationUtil.kt

@@ -0,0 +1,294 @@
+package com.kdanmobile.reader.utils
+
+import android.animation.Animator
+import android.animation.AnimatorListenerAdapter
+import android.animation.AnimatorSet
+import android.animation.ObjectAnimator
+import android.animation.PropertyValuesHolder
+import android.animation.ValueAnimator
+import android.os.Handler
+import android.os.Looper
+import android.view.View
+import android.view.animation.AccelerateInterpolator
+import android.view.animation.Animation
+import android.view.animation.LinearInterpolator
+
+/**
+ * @项目名称:PDFReader_android_for_as
+ * @包名称:com.kdanmobile.pdf.tools
+ * @文件名称:AnimationUtil
+ * @创建时间:15/9/12 - 上午11:29 - 11
+ * @创建人:luozhipeng
+ * @修改记录:
+ * @修改人: --------------------------------------
+ * @Copyright (c)-2015kdanmobile
+ */
+class AnimationUtil private constructor() {
+
+    init {
+        /* cannot be instantiated */
+        throw UnsupportedOperationException("cannot be instantiated")
+    }
+
+    companion object {
+        private var showTopToBottom: ObjectAnimator? = null
+        private var showBottomToTop: ObjectAnimator? = null
+        private var showRightToLeft: ObjectAnimator? = null
+        private var showLeftToRight: ObjectAnimator? = null
+
+        fun ViewScaleAnimation(view: View?, toScale: Float, duration: Long = 16000) {
+            if (view != null && view.animation != null) {
+                view.animation.start()
+                return
+            }
+            val anim1 = ObjectAnimator.ofFloat(view, "scaleX", 1.0f, toScale, 1.0f)
+            anim1.duration = duration
+            anim1.repeatMode = ValueAnimator.RESTART
+            anim1.repeatCount = Animation.INFINITE
+            val anim2 = ObjectAnimator.ofFloat(view, "scaleY", 1.0f, toScale, 1.0f)
+            anim2.duration = duration
+            anim2.repeatMode = ValueAnimator.RESTART
+            anim2.repeatCount = Animation.INFINITE
+            //        ObjectAnimator anim3 = ObjectAnimator.ofFloat(view, "alpha",
+            //                1.0f, 0.9f, 1.0f);
+            //        anim3.setDuration(16000);
+            //        anim3.setRepeatMode(Animation.RESTART);
+            //        anim3.setRepeatCount(Animation.INFINITE);
+            val animSet = AnimatorSet()
+            animSet.interpolator = LinearInterpolator()
+            animSet.playTogether(anim1, anim2)
+            animSet.start()
+        }
+
+        private fun ImageViewAnimation(view: View, duration: Long = 16000) {
+            val pvhX = PropertyValuesHolder.ofFloat("alpha", 1f, 0.4f, 1f)
+            val pvhY = PropertyValuesHolder.ofFloat("scaleX", 1f, 1.2f, 1f)
+            val pvhZ = PropertyValuesHolder.ofFloat("scaleY", 1f, 1.2f, 1f)
+            val anim = ObjectAnimator.ofPropertyValuesHolder(view, pvhX, pvhY, pvhZ)
+            anim.repeatCount = Animation.INFINITE//1->2->1的循环模式
+            anim.repeatMode = ValueAnimator.RESTART//无限模式
+            anim.interpolator = AccelerateInterpolator()
+            anim.setDuration(duration).start()
+        }
+
+        /**
+         * 隐藏buttons控件,从控件顶部往底部回收隐藏
+         *
+         * @param view
+         */
+        fun hideViewFromTopToBottom(view: View?, duration: Long = 300) {
+            if (showBottomToTop != null && showBottomToTop!!.isRunning())
+                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)
+                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()
+            }
+        }
+
+        /**
+         * 显示buttons控件,从控件底部往控件顶部扩展显示
+         *
+         * @param view
+         */
+        fun showViewFromBottomToTop(view: View?, duration: Long = 300) {
+            if (showBottomToTop != null && showBottomToTop!!.isRunning())
+                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())
+                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()
+            }
+        }
+
+        fun showViewFromRightToLeft(view: View?, duration: Long = 300) {
+            if (showRightToLeft != null && showRightToLeft!!.isRunning)
+                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())
+                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()
+            }
+        }
+
+        fun hideViewFromLeftToRight(view: View?, duration: Long = 300) {
+            if (showRightToLeft != null && showRightToLeft!!.isRunning)
+                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)
+                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()
+            }
+        }
+
+        /**
+         * 显示buttons控件,从控件顶部往控件底部扩展显示
+         *
+         * @param view
+         */
+        fun showViewFromTopToBottom(view: View?, duration: Long = 300) {
+            if (showTopToBottom != null && showTopToBottom!!.isRunning)
+                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())
+                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()
+            }
+        }
+
+        /**
+         * 隐藏buttons控件,从控件底部往控件顶部收缩隐藏
+         *
+         * @param view
+         */
+        fun hideViewFromBottomToTop(view: View?, duration: Long = 300) {
+            if (showTopToBottom != null && showTopToBottom!!.isRunning)
+                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)
+                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()
+            }
+        }
+
+        fun showViewFromLeftToRight(view: View?, duration: Long = 300) {
+            if (showLeftToRight != null && showLeftToRight!!.isRunning)
+                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())
+                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()
+            }
+        }
+
+        fun hideViewFromRightToLeft(view: View?, duration: Long = 300) {
+            if (showLeftToRight != null && showLeftToRight!!.isRunning)
+                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)
+                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()
+            }
+        }
+
+        fun showViewAlpha(view: View?, duration: Long = 300) {
+            if (view != null && view.visibility != View.VISIBLE) {
+                val pvhA = PropertyValuesHolder.ofFloat("alpha", 0f, 1.0f)
+                val animAlpha = ObjectAnimator.ofPropertyValuesHolder(view, pvhA)
+                animAlpha.interpolator = AccelerateInterpolator()
+                animAlpha.addListener(object : AnimatorListenerAdapter() {
+                    override fun onAnimationStart(animation: Animator) {
+                        super.onAnimationStart(animation)
+                        view.visibility = View.VISIBLE
+                    }
+                })
+                animAlpha.setDuration(duration).start()
+            }
+        }
+
+        fun hideViewAlpha(view: View?, duration: Long = 300) {
+            if (view != null && view.visibility == View.VISIBLE) {
+                val pvhA = PropertyValuesHolder.ofFloat("alpha", 1.0f, 0f)
+                val animAlpha = ObjectAnimator.ofPropertyValuesHolder(view, pvhA)
+                animAlpha.interpolator = AccelerateInterpolator()
+                animAlpha.addListener(object : AnimatorListenerAdapter() {
+                    override fun onAnimationEnd(animation: Animator) {
+                        super.onAnimationEnd(animation)
+                        Handler(Looper.getMainLooper()).post { view.visibility = View.GONE }
+                    }
+                })
+                animAlpha.setDuration(duration).start()
+            }
+        }
+
+        /**
+         * 结束该动画
+         */
+        fun cancelAnimations() {
+            if (showTopToBottom != null)
+                showTopToBottom!!.cancel()
+            if (showBottomToTop != null)
+                showBottomToTop!!.cancel()
+            if (showLeftToRight != null)
+                showLeftToRight!!.cancel()
+            if (showRightToLeft != null)
+                showRightToLeft!!.cancel()
+            showTopToBottom = null
+            showBottomToTop = null
+            showRightToLeft = null
+            showLeftToRight = null
+        }
+    }
+}