第四章 属性动画进阶
-
PropertyValuesHolder与KeyFrame
-
ofFloat()、ofInt()
val rotationHolder =
PropertyValuesHolder.ofInt("Rotation", 60, -60, -40, -20, 20, 10)
val alphaHolder = PropertyValuesHolder.ofFloat("alpha", 0f, 1f, 0.4f, 1f, 0.5f, 0f)
val objectAnimator =
ObjectAnimator.ofPropertyValuesHolder(image, rotationHolder, alphaHolder)
objectAnimator.duration = 3000
objectAnimator.start() -
ofObject()
-
Keyframe
-
类比Flash中平移动画,只需要两个关键帧,起始点和终止点
val keyFrame1 = Keyframe.ofFloat(0f, 0f)
val keyFrame2 = Keyframe.ofFloat(0.1f, 0.3f)
val keyFrame3 = Keyframe.ofFloat(1f, 1f)
val keyHolder = PropertyValuesHolder.ofKeyframe("alpha", keyFrame1, keyFrame2, keyFrame3)
val keyObjAnimator = ObjectAnimator.ofPropertyValuesHolder(image, keyHolder)
keyObjAnimator.start()-
使用插值器
前一帧到当前帧(设置插值器)会有效果
-
-
-
ViewPropertyAnimator
val alpha = imageView.animate().alpha(0f)
alpha.duration = 1000
alpha.interpolator = AnticipateOvershootInterpolator()
alpha.start() -
ViewGroup内组件添加动画
-
布局添加属性
<LinearLayout
......
// 不能更改动画样式,系统默认样式
android:animateLayoutChanges="true" /> -
LayoutTransition
-
APPEARING:元素在容器中出现时所定义的动画
-
DISAPPEARING:元素在容器中消失时所定义的动画
-
CHANGE_APPEARING:容器中要显现一个新的元素,其他需要变化的元素所应用的动画
-
CHANGE_DISAPPEARING:容器中某个元素消失时,其他需要变化的元素所采用的动画
val layoutTransition = LayoutTransition()
val animOut = ObjectAnimator.ofFloat(null, "rotation", 0f, 90f, 0f, -90f, 0f)
layoutTransition.setAnimator(LayoutTransition.DISAPPEARING, animOut)
container.layoutTransition = layoutTransitionTips:CHANGE_APPEARING、CHANGE_DISAPPEARING需要用Keyframe构造PropertyValuesHolder动画,并且left、top属性是必须的
-
-