做个记录,之前项目中用得比较少 一、Activity中代码段
/**
* 动态生成ValueAnimator
* 执行后不断改变view得最小宽度
*/
private fun animationBigger() {
//属性从300递增到500
val anim = ValueAnimator.ofInt(300, 400).apply {
this.duration = 2000 //执行时长
this.repeatCount = 0
this.repeatMode = ValueAnimator.RESTART
}
anim.addUpdateListener {
// 获得改变后的值
val currentValue = it.animatedValue as Int
// 步骤4:将改变后的值赋给对象的属性值,下面会详细说明
binding.btn1.minWidth = currentValue
// 步骤5:刷新视图,即重新绘制,从而实现动画效果
binding.btn1.requestLayout();
}
anim.addListener(object : Animator.AnimatorListener {
override fun onAnimationStart(animation: Animator?) {
}
override fun onAnimationEnd(animation: Animator?) {
startAnimset()
}
override fun onAnimationCancel(animation: Animator?) {
}
override fun onAnimationRepeat(animation: Animator?) {
}
})
anim.start()//开始执行动画
startAnimXml()
}
/**
* 动态生成ObjectAnimator,AnimatorSet
* 执行:沿Y轴旋转后位移300
*/
private fun startAnimset() {
val anim = ObjectAnimator.ofFloat(binding.btn1, "translationX", 0f, 300f)
val animRotate = ObjectAnimator.ofFloat(binding.btn1, "rotationY", 0f, 360f)
val animatorSet = AnimatorSet()
animatorSet.playSequentially(anim)
animatorSet.duration = 2000
//通过animatorSet的play方法获取 感兴趣的话 可以都试试 看一下效果
animatorSet.play(anim).after(animRotate)
/**
* 系统自带BounceInterpolator,AccelerateInterpolator加速,DecelerateInterpolator减速,LinearInterpolator匀速
* CycleInterpolator(0.5f)来回,OvershootInterpolator
* http://inloop.github.io/interpolator/ 插值器示例
*/
animatorSet.interpolator = MInterpolator(0.5f)//自定义插值器
// animatorSet.interpolator = CycleInterpolator(0.5f)
// animatorSet.interpolator = OvershootInterpolator(2.0f)
animatorSet.addListener(object : Animator.AnimatorListener {
override fun onAnimationStart(animation: Animator?) {
}
override fun onAnimationEnd(animation: Animator?) {
// translate()//重复执行
}
override fun onAnimationCancel(animation: Animator?) {
}
override fun onAnimationRepeat(animation: Animator?) {
}
})
animatorSet.start()
}
private var startTime = 0L
/**
* 根据静态文件生成Animator
* 执行:旋转后位移
* 注意:静态文件配置得duration并不完全生效(有一定得效果)
*/
private fun startAnimXml() {
AnimatorInflater.loadAnimator(this, R.animator.anim_first).apply {
this.setTarget(binding.text)
this.interpolator = LinearInterpolator()
// this.startDelay = 5000//start后延迟5秒执行
// this.duration = 10000;//生效
this.addListener(object : Animator.AnimatorListener {
override fun onAnimationStart(animation: Animator?) {
startTime = System.currentTimeMillis()
"开始执行".d()
}
override fun onAnimationEnd(animation: Animator?) {
"timelong=${(System.currentTimeMillis() - startTime)}".d()
}
override fun onAnimationCancel(animation: Animator?) {
}
override fun onAnimationRepeat(animation: Animator?) {
}
})
}.start()
}
二、自定义插值器
import android.animation.TimeInterpolator
import kotlin.math.pow
import kotlin.math.sin
/**
* @author Liushihua
* @date 2022-5-17 16:51
* @desc
*/
class MInterpolator(var factor: Float = 0.15f) : TimeInterpolator {
// 因子数值越小振动频率越高
override fun getInterpolation(input: Float): Float {
return (2.0.pow(-10 * input.toDouble()) * sin((input - factor / 4) * (2 * Math.PI) / factor) + 1).toFloat()
}
}
三、values中新建animator文件夹,anim_first.xml
<set xmlns:android="http://schemas.android.com/apk/res/android"
android:ordering="sequentially">
<!-- sequentially按顺序执行 together同时执行 -->
<objectAnimator
android:duration="5000"
android:propertyName="rotation"
android:valueFrom="0"
android:valueTo="360" />
<objectAnimator
android:duration="7000"
android:propertyName="translationX"
android:valueFrom="0"
android:valueTo="560" />
</set>
参考: https://blog.csdn.net/weixin_45558166/article/details/110310651 https://www.jianshu.com/p/39ee86df9e5a
|