定义
通过不断控制 值 的变化,再不断 手动 赋给对象的属性,从而实现动画效果
使用
已有属性动画
palpha.gifobjectAnimator = ObjectAnimator.ofFloat(mPropertyIv, "alpha", 1.0f, 0.1f)
objectAnimator.apply {
duration = 5000
repeatCount = -1
repeatMode = ObjectAnimator.REVERSE
}
objectAnimator.start()
pscale.gif
mScaleXAnimator = ObjectAnimator.ofFloat(mPropertyIv, "scaleX", 1.0f,0.3f)
mScaleYAnimator = ObjectAnimator.ofFloat(mPropertyIv, "scaleY", 1.0f,0.3f)
mScaleYAnimator.repeatCount = -1
mScaleYAnimator.repeatMode = ObjectAnimator.REVERSE
val set = AnimatorSet()
set.playTogether(mScaleXAnimator,mScaleYAnimator)
set.duration = 5000
set.start()
protate.gif
mRotationAnimator = ObjectAnimator.ofFloat(mPropertyIv, "rotation", 0f,100f,200f,300f)
mRotationAnimator.duration = 5000
mRotationAnimator.repeatCount= 3
mRotationAnimator.repeatMode = ObjectAnimator.REVERSE
mRotationAnimator.start()
ptranlate.gif
mTranslateXAnimator = ObjectAnimator.ofFloat(mPropertyIv, "translationX", 0f,100f,200f,300f)
mTranslateYAnimator = ObjectAnimator.ofFloat(mPropertyIv, "translationY", 0f,100f)
mTranslateYAnimator.repeatCount = -1
mTranslateYAnimator.repeatMode = ObjectAnimator.REVERSE
val set = AnimatorSet()
set.playTogether(mTranslateXAnimator,mTranslateYAnimator)
set.duration = 5000
set.start()
组合属性动画
ObjectAnimator alphaAnim = ObjectAnimator.ofFloat(myView, "alpha", 1.0f, 0.5f, 0.8f, 1.0f);
ObjectAnimator scaleXAnim = ObjectAnimator.ofFloat(myView, "scaleX", 0.0f, 1.0f);
ObjectAnimator scaleYAnim = ObjectAnimator.ofFloat(myView, "scaleY", 0.0f, 2.0f);
ObjectAnimator rotateAnim = ObjectAnimator.ofFloat(myView, "rotation", 0, 360);
ObjectAnimator transXAnim = ObjectAnimator.ofFloat(myView, "translationX", 100, 400);
ObjectAnimator transYAnim = ObjectAnimator.ofFloat(myView, "tranlsationY", 100, 750);
AnimatorSet set = new AnimatorSet();
set.playTogether(alphaAnim, scaleXAnim, scaleYAnim, rotateAnim,transXAnim, transYAnim);
// set.playSequentially(alphaAnim, scaleXAnim, scaleYAnim, rotateAnim, transXAnim, transYAnim);
set.setDuration(3000);
set.start();
可以看到这些动画可以同时播放,或者是按序播放。
自定义属性动画
- 如果是自定义控件,需要添加 setter / getter 方法;
- 用 ObjectAnimator.ofXXX() 创建 ObjectAnimator 对象;
- 用 start() 方法执行动画。
public class SportsView extends View {
float progress = 0;
......
// 创建 getter 方法
public float getProgress() {
return progress;
}
// 创建 setter 方法
public void setProgress(float progress) {
this.progress = progress;
invalidate();
}
@Override
public void onDraw(Canvas canvas) {
super.onDraw(canvas);
......
canvas.drawArc(arcRectF, 135, progress * 2.7f, false, paint);
......
}
}
......
// 创建 ObjectAnimator 对象
ObjectAnimator animator = ObjectAnimator.ofFloat(view, "progress", 0, 65);
// 执行动画
animator.start();
网友评论