ViewPropertyAnimator、ObjectAnimator、ValueAnimator 这三种 Animator,
它们其实是一种递进的关系:从左到右依次变得更加难用,也更加灵活。
它们的性能是一样的,因为 ViewPropertyAnimator 和 ObjectAnimator 的内部实现其实都是 ValueAnimator,ObjectAnimator 更是本来就是 ValueAnimator 的子类,它们三个的性能并没有差别。
它们的差别只是使用的便捷性以及功能的灵活性。
所以在实际使用时候的选择,只要遵循一个原则就行:尽量用简单的。
能用 View.animate() 实现就不用 ObjectAnimator,
能用 ObjectAnimator 就不用 ValueAnimator。
老版本属性动画
ObjectAnimator objectAnimator = ObjectAnimator.ofFloat(btnShow, "translationX", 0, 300);
ObjectAnimator objectAnimator1 = ObjectAnimator.ofFloat(btnShow, "rotation", 0, 360);
AnimatorSet animatorSet = new AnimatorSet();
animatorSet.setDuration(2000);
animatorSet.play(objectAnimator).with(objectAnimator1);
//animatorSet.playTogether(objectAnimator,objectAnimator1);
animatorSet.start();
简化版属性动画:
btnShow.animate()
.setDuration(2000)
.translationX(300)
.rotation(360)
.start();
ViewPropertyAnimator
View类的animate()方法,是在Android 3.1系统上新增的一个方法,其作用就是返回ViewPropertyAnimator的实例对象。
/**
* This method returns a ViewPropertyAnimator object, which can be used to animate
* specific properties on this View.
*
* @return ViewPropertyAnimator The ViewPropertyAnimator associated with this View.
*/
public ViewPropertyAnimator animate() {
if (mAnimator == null) {
mAnimator = new ViewPropertyAnimator(this);
}
return mAnimator;
}
特点:
1、专门针对View对象动画而操作的类。
2、提供了更简洁的链式调用设置多个属性动画,这些动画可以同时进行的。
3、拥有更好的性能,多个属性动画是一次同时变化,只执行一次UI刷新(也就是只调用一次invalidate,而n个ObjectAnimator就会进行n次属性变化,就有n次invalidate)。
4、每个属性提供两种类型方法设置。
5、该类只能通过View的animate()获取其实例对象的引用
参考:属性动画-Property Animation之ViewPropertyAnimator 你应该知道的一切
Android动画-属性动画-ViewPropertyAnimator
常见设置
如下:
view.animate()//获取ViewPropertyAnimator对象
//动画持续时间
.setDuration(5000)
//透明度
.alpha(0)
.alphaBy(0)
//旋转
.rotation(360)
.rotationBy(360)
.rotationX(360)
.rotationXBy(360)
.rotationY(360)
.rotationYBy(360)
//缩放
.scaleX(1)
.scaleXBy(1)
.scaleY(1)
.scaleYBy(1)
//平移
.translationX(100)
.translationXBy(100)
.translationY(100)
.translationYBy(100)
.translationZ(100)
.translationZBy(100)
//更改在屏幕上的坐标
.x(10)
.xBy(10)
.y(10)
.yBy(10)
.z(10)
.zBy(10)
//插值器
.setInterpolator(new BounceInterpolator())//回弹
.setInterpolator(new AccelerateDecelerateInterpolator())//加速再减速
.setInterpolator(new AccelerateInterpolator())//加速
.setInterpolator(new DecelerateInterpolator())//减速
.setInterpolator(new LinearInterpolator())//线性
//动画延迟
.setStartDelay(1000)
//是否开启硬件加速
.withLayer()
//监听
.setListener(new Animator.AnimatorListener() {
@Override
public void onAnimationStart(Animator animation) {
Log.i("MainActivity", "run: onAnimationStart");
}
@Override
public void onAnimationEnd(Animator animation) {
Log.i("MainActivity", "run: onAnimationEnd");
}
@Override
public void onAnimationCancel(Animator animation) {
Log.i("MainActivity", "run: onAnimationCancel");
}
@Override
public void onAnimationRepeat(Animator animation) {
Log.i("MainActivity", "run: onAnimationRepeat");
}
})
.setUpdateListener(new ValueAnimator.AnimatorUpdateListener() {
@Override
public void onAnimationUpdate(ValueAnimator animation) {
Log.i("MainActivity", "run: onAnimationUpdate==");
}
})
.withEndAction(new Runnable() {
@Override
public void run() {
Log.i("MainActivity", "run: end");
}
})
.withStartAction(new Runnable() {
@Override
public void run() {
Log.i("MainActivity", "run: start");
}
}) .start();