1. 程式人生 > >自定義工具類-----動畫工具類

自定義工具類-----動畫工具類

此程式碼是用的kotlin寫的,java也類比
class AnimUtil {

    companion object {
        //平移動畫常量
        val TRANS_ENTER_FROM_LEFT = 0//平移動畫左側進入
        val TRANS_ENTER_FROM_RIGHT = 1//平移動畫右側進入
        val TRANS_EXIT_TO_LEFT = 2//平移動畫退出到左側
        val TRANS_EXIT_TO_RIGHT = 3//平移動畫退出到右側
        /**
         * 控制元件的左右平移動畫
         */
        fun transAnim(context: Context, view: View, transType: Int) {
            val windowManager = context.getSystemService(Context.WINDOW_SERVICE) as WindowManager
            val width = windowManager.defaultDisplay.width.toFloat()
            val animator: ObjectAnimator
            when (transType) {
                TRANS_ENTER_FROM_LEFT -> animator = ObjectAnimator.ofFloat(view, "translationX", -width, 0f)
                TRANS_ENTER_FROM_RIGHT -> animator = ObjectAnimator.ofFloat(view, "translationX", width, 0f)
                TRANS_EXIT_TO_LEFT -> animator = ObjectAnimator.ofFloat(view, "translationX", 0f, -width)
                TRANS_EXIT_TO_RIGHT -> animator = ObjectAnimator.ofFloat(view, "translationX", 0f, width)
                else -> animator = ObjectAnimator.ofFloat(view, "translationX", 0f, -width)
            }
            animator.duration = 1000
            animator.start()
        }
    }

}