没有找到比较官方的定义,所以这是我对它的理解:随着时间来不断修改属性的值,以实现动画的效果
-
ViewPropertyAnimator
最简单的属性动画,当然功能也很简单。只有位移、缩放、透明度、旋转等
iv_dayu.animate()
//设置属性
.translationX(200.dp)
.translationY(200.dp)
.alpha(0.5f)
.scaleX(2f)
.scaleY(0.5f)
.rotation(90f)
//设置插值器
.setInterpolator(LinearOutSlowInInterpolator())
//设置延迟时间
.setStartDelay(1000)
//设置持续时间
.duration = 5000
-
ObjectAnimator
相比于ViewPropertyAnimator
,它可以为自定义属性设置动画
class MyImageView(context: Context?, attrs: AttributeSet?) : View(context, attrs) {
val paint = Paint(Paint.ANTI_ALIAS_FLAG)
var imageWidth: Float = 50.dp
set(value) {
field = value
invalidate()
}
var startX = 0f
set(value) {
field = value
invalidate()
}
var startY = 0f
set(value) {
field = value
invalidate()
}
override fun onDraw(canvas: Canvas) {
super.onDraw(canvas)
val img = getAvatar(imageWidth, R.drawable.dayu1)
canvas.drawBitmap(img, startX, startY, paint)
}
}
修改自定义viewMyImageView
的图片宽度imageWidth
val objectAnnotation = ObjectAnimator.ofFloat(my_image, "imageWidth", 50.dp, 300.dp)
objectAnnotation.startDelay = 1000
objectAnnotation.duration = 2000
objectAnnotation.start()
-
PropertyValuesHolder
设置更加详细的动画,可以同时改变一个view的多个属性,也可以对一个属性在多个阶段进行设置。
val bottomFlipHolder = PropertyValuesHolder.ofFloat("bottomFlip", 60f)
val flipRotationHolder = PropertyValuesHolder.ofFloat("flipRotation", 270f)
val topFlipHolder = PropertyValuesHolder.ofFloat("topFlip", -60f)
val holderAnimator = ObjectAnimator.ofPropertyValuesHolder(
camera_view,
bottomFlipHolder,
flipRotationHolder,
topFlipHolder
)
holderAnimator.startDelay = 1000
holderAnimator.duration = 2000
holderAnimator.start()
val keyframe = Keyframe.ofFloat(0.2f, 20f)
val keyframe2 = Keyframe.ofFloat(0.5f, 60f)
val keyframe3 = Keyframe.ofFloat(0.8f, 30f)
val holder = PropertyValuesHolder.ofKeyframe("bottomFlip", keyframe, keyframe2, keyframe3)
val objectAnimator = ObjectAnimator.ofPropertyValuesHolder(camera_view, holder)
objectAnimator.startDelay = 1000
objectAnimator.duration = 2000
objectAnimator.start()
-
AnimatorSet
将多个Animator合并在一起,以顺序或并行的方式进行
val topFlipAnimator = ObjectAnimator.ofFloat(camera_view, "topFlip", -60f)
topFlipAnimator.startDelay = 200
topFlipAnimator.duration = 1000
// topFlipAnimator.start()
val bottomFlipAnimator = ObjectAnimator.ofFloat(camera_view, "bottomFlip", 60f)
bottomFlipAnimator.startDelay = 1000
bottomFlipAnimator.duration = 1000
// bottomFlipAnimator.start()
val flipRotationAnimator = ObjectAnimator.ofFloat(camera_view, "flipRotation", 270f)
flipRotationAnimator.startDelay = 200
flipRotationAnimator.duration = 1000
// flipRotationAnimator.start()
//顺序执行多个属性
val animatorSet = AnimatorSet()
animatorSet.playSequentially(bottomFlipAnimator, flipRotationAnimator, topFlipAnimator)
//animatorSet.playTogether(bottomFlipAnimator, flipRotationAnimator, topFlipAnimator)
animatorSet.start()
-
Interpolator
插值器,用于设置时间完成度和动画完成度的计算公式
animator.interpolator=LinearInterpolator()
-
TypeEvaluator
用于设置动画完成度到属性具体值的计算公式。比如argb颜色就不能简单的进行数值的简单增长
animator.setEvaluator(ArgbEvaluator())
也可以进行自定义
//对一个PointF 坐标进行运输
public class HsvEvaluator implements TypeEvaluator<Integer> {
@Override
public Object evaluate(float fraction, Object startValue, Object endValue) {
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
PointF(currentX, currentY)
}
}
更多知识:
https://blog.csdn.net/carson_ho/article/details/72909894
https://www.jianshu.com/p/b117c974deaf
网友评论