此代码是用的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()
}
}
}
自定义工具类-----动画工具类
最新推荐文章于 2021-05-25 16:37:32 发布