// 属性值从0变化到400
val valueAnimator = ValueAnimator.ofInt(0, 400)
valueAnimator.duration = 1000
valueAnimator.start()
// 添加监听器
valueAnimator.addUpdateListener { animation ->
val animatedValue = animation.animatedValue as Int
img.layout(animatedValue, animatedValue, animatedValue + img.width, animatedValue + img.height)
}
valueAnimator.interpolator = Interpolator { input ->
// input代表动画时间内的进度,从0-1匀速变化,此时变化是平方级变化
// 取值可以小于0或者大于1,可以参考AnticipateOvershootInterpolator
input * input }
valueAnimator.setEvaluator { fraction, startValue, endValue ->
// fraction 代表
fraction * (startValue as Int - endValue as Int) + endValue
}
// 自定义Evaluator
val evaluator : TypeEvaluator<Char> = TypeEvaluator { fraction, startValue, endValue ->
val fl = startValue.toInt() + fraction * (endValue.toInt() - startValue.toInt())
fl.toChar()
}
?
// 需要传入自定义Evaluator,告知动画执行过程中的值
val ofObjectAnim = ValueAnimator.ofObject(evaluator, ‘A‘, ‘z‘)
ofObjectAnim.start()
ofObjectAnim.addUpdateListener { animation ->
charTv.setText(animation.getAnimatedValue().toString())
}
val arrayOf = arrayOf("alpha", "rotation", "rotationX", "rotationY", "scaleX", "scaleY")
// 通过反射
objAnim = ObjectAnimator.ofFloat(image, arrayOf[animCount], 0f, 300f)
val animatorSet = AnimatorSet()
animatorSet.playSequentially(objAnim, objAnim)
val animatorSet = AnimatorSet()
animatorSet.playTogether(objAnim, objAnim)
val animatorSet = AnimatorSet()
val builder = animatorSet.play(objAnim)
builder.with(objAnim)
before
after
都是以play()中当前所播放的动画为基准的
<animator xmlns:android="http://schemas.android.com/apk/res/android"
android:duration="1000"
android:valueFrom="0"
android:valueTo="300"
android:startOffset="1000"
android:repeatCount="infinite"
android:repeatMode="reverse"
android:valueType="intType"
android:interpolator="@android:anim/anticipate_overshoot_interpolator"/>
val loadAnimator = AnimatorInflater.loadAnimator(this, R.animator.animator) as ValueAnimator
loadAnimator.addUpdateListener { animation ->
val offset = animation.getAnimatedValue() as Int
image.layout(offset, offset, image.width + offset, image.height + offset)
}
loadAnimator.start()
val objectAnimator =
AnimatorInflater.loadAnimator(this, R.animator.obj_animator) as ObjectAnimator
?
objectAnimator.target = image
objectAnimator.start()
原文:https://www.cnblogs.com/youngly15/p/13157156.html