我的新书《Android App开发入门与实战》已于2020年8月由人民邮电出版社出版,欢迎购买。点击进入详情
项目地址
https://github.com/ddnosh/android-demo-animation
用法
- 透明度
ObjectAnimator objectAnimator = ObjectAnimator.ofFloat(iv, "alpha", 0f, 1f);
objectAnimator.setDuration(1000);
objectAnimator.start();
- 缩放
ObjectAnimator scaleXAnimator = ObjectAnimator.ofFloat(iv, "scaleX", 0f, 1f);
ObjectAnimator scaleYAnimator = ObjectAnimator.ofFloat(iv, "scaleY", 0f, 1f);
AnimatorSet set = new AnimatorSet();
set.play(scaleXAnimator).with(scaleYAnimator);
set.setDuration(1000);
set.start();
- 位移
ObjectAnimator objectAnimatorTranslate = ObjectAnimator.ofFloat(iv, "translationX", 0f, 500f);
objectAnimatorTranslate.setDuration(1000);
objectAnimatorTranslate.start();
- 旋转
ObjectAnimator objectAnimatorScale = ObjectAnimator.ofFloat(iv, "rotation", 0f, 360f);
objectAnimatorScale.setDuration(1000);
objectAnimatorScale.start();
- 组合
AnimatorSet group = new AnimatorSet();
ObjectAnimator objectAnimatorScaleX = ObjectAnimator.ofFloat(iv, "scaleX", 0f, 1f);
ObjectAnimator objectAnimatorScaleY = ObjectAnimator.ofFloat(iv, "scaleY", 0f, 1f);
ObjectAnimator objectAnimatorRotateX = ObjectAnimator.ofFloat(iv, "rotationX", 0f, 360f);
ObjectAnimator objectAnimatorRotateY = ObjectAnimator.ofFloat(iv, "rotationY", 0f, 360f);
ObjectAnimator objectAnimatorTranslate = ObjectAnimator.ofFloat(iv, "translationX", 0f, 500f);
group.setDuration(2000);
group.play(objectAnimatorScaleX).with(objectAnimatorScaleY)
.before(objectAnimatorRotateX).before(objectAnimatorRotateY).after(objectAnimatorTranslate);
group.start();
- ValueAnimator
ValueAnimator valueAnimator = new ValueAnimator();
valueAnimator.setDuration(3000);
valueAnimator.setObjectValues(new PointF(0, 0));
valueAnimator.setInterpolator(new LinearInterpolator());
valueAnimator.setEvaluator(new TypeEvaluator<PointF>()
{
@Override
public PointF evaluate(float fraction, PointF startValue,
PointF endValue)
{
PointF point = new PointF();
point.x = 200 * fraction * 3;
point.y = 0.5f * 200 * (fraction * 3) * (fraction * 3);
return point;
}
});
valueAnimator.addUpdateListener(new ValueAnimator.AnimatorUpdateListener()
{
@Override
public void onAnimationUpdate(ValueAnimator animation)
{
PointF point = (PointF) animation.getAnimatedValue();
iv.setX(point.x);
iv.setY(point.y);
}
});
valueAnimator.start();