属性动画
ViewPropertyAnimator
imageView.animate()
.translationX(200.dp)
.translationY(100.dp)
.scaleX(2f)
.scaleY(2f)
.alpha(0.5f)
.setStartDelay(1000)
缺点:
- 有局限性,只能改变其中的几个属性,横向纵向 渐变旋转
- 比如需要更改圆半径大小的动画,对于这种自定义的属性不能实现
优点:
- 使用方便
ObjectAnimator
class CircleView @JvmOverloads constructor(
context: Context, attrs: AttributeSet? = null, defStyleAttr: Int = 0
) : View(context, attrs, defStyleAttr) {
var radius = 50.dp
set(value) {
field = value
invalidate()
}
private val paint = Paint(Paint.ANTI_ALIAS_FLAG).apply {
color = Color.GREEN
}
override fun onDraw(canvas: Canvas) {
super.onDraw(canvas)
canvas.drawCircle(width/2f,height/2f,radius,paint)
}
}
val animtor = ObjectAnimator.ofFloat(imageView, "radius", 150.dp)
animtor.startDelay = 1000
animtor.start()
优点:
- 支持自定义的属性值
缺点:
- 一次只支持一个属性值,如果操作多个属性值的话,需要设置多个,然后一起执行。
AnimatorSet 动画集(对多个动画进行合成)
animtor,animtor1,animtor2 三个ObjectAnimator动画顺序执行
val animatorSet = AnimatorSet()
animatorSet.playSequentially(animtor,animtor1,animtor2)
animatorSet.start()
PropertyValuesHolder
- 用法
val propertyValuesHolder = PropertyValuesHolder.ofFloat("radius", 150.dp)
val objectAnimator = ObjectAnimator.ofPropertyValuesHolder(imageView,propertyValuesHolder)
objectAnimator.startDelay = 1000
objectAnimator.duration = 2000
objectAnimator.start()
优点
- 支持更精细的动画控制,可以支持多个自定义属性值(ObjectAnimator.ofPropertyValuesHolder此方法支持多个holder)
和AnimatorSet区别
- 不支持多个属性值动画按顺序执行
Keyframe
使用方式
val distance = 200.dp
val keyframe = Keyframe.ofFloat(0f, 0f * distance)
val keyframe1 = Keyframe.ofFloat(0.2f, 0.4f * distance)
val keyframe2 = Keyframe.ofFloat(0.8f, 0.6f * distance)
val keyframe3 = Keyframe.ofFloat(1f, 1f * distance)
val holder = PropertyValuesHolder.ofKeyframe(
"translationX",
keyframe,
keyframe1,
keyframe2,
keyframe3
)
val animator = ObjectAnimator.ofPropertyValuesHolder(imageView,holder)
animator.startDelay = 2000
animator.start()
优点
- 更加精准的控制动画执行的快慢进度,实现特殊的动画效果
Interpolator 插值器
使用方式
val animator = ObjectAnimator.ofFloat(imageView,"translationX",200.dp)
animator.interpolator = LinearInterpolator()
animator.startDelay = 2000
animator.start()
总结
- 适用于场景内的动画AccelerateDecelerateInterpolator()
- 适用于出场动画 AccelerateInterpolator
- 适用于入场动画 DecelerateInterpolator
TypeEvaluator
使用方式
val animtor =
ObjectAnimator.ofObject(imageView, "point", PointFEvaluator(), PointF(100.dp, 200.dp))
animtor.startDelay = 1000
animtor.start()
private class PointFEvaluator :TypeEvaluator<PointF>{
override fun evaluate(fraction: Float, startValue: PointF, endValue: PointF): PointF {
val startX = startValue.x
val startY = startValue.y
val endX = endValue.x
val endY = endValue.y
val currentX = startX + (endX-startX)*fraction
val currentY = startY + (endY-startY)*fraction
return PointF(currentX,currentY)
}
}
class PointFView @JvmOverloads constructor(
context: Context, attrs: AttributeSet? = null, defStyleAttr: Int = 0
) : View(context, attrs, defStyleAttr) {
private val mPaint = Paint(Paint.ANTI_ALIAS_FLAG).apply {
strokeWidth = 20.dp
strokeCap = Paint.Cap.ROUND
}
var point = PointF(0f,0f)
set(value) {
field = value
invalidate()
}
override fun onDraw(canvas: Canvas) {
super.onDraw(canvas)
canvas.drawPoint(point.x,point.y,mPaint)
}
}
Listeners
各种动画的监听器,监听动画开始啦 结束啦等
ValueAnimator
不常用
硬件加速
利用GPU提高视图渲染的速度? 没搞明白