简介
属性动画包含:
ObjectAnimator 动画的执行类
ValueAnimator 动画的执行类
AnimatorSet 用于控制一组动画的执行:线性,一起,每个动画的先后执行等。
AnimatorInflater 用户加载属性动画的xml文件
ObjectAnimator 单一属性动画
缩放X轴:ScaleX
ObjectAnimator().ofFloat(imageView,”ScaleY”,1.0f,0.0f).setDuration(3000).start();
缩放Y轴:ScaleY
ObjectAnimator().ofFloat(imageView,”ScaleX”,1.0f,0.0f).setDuration(3000).start();
旋转动画(X轴):rotationX
ObjectAnimator().ofFloat(imageView,”rotationX”,0.0f,360f).setDuration(3000).start();
旋转动画(Y轴):rotationY
ObjectAnimator().ofFloat(imageView,”rotationY”,0.0f,360f).setDuration(3000).start();
旋转动画(中心点):rotation
ObjectAnimator().ofFloat(imageView,”rotation”,0.0f,360f).setDuration(3000).start();
淡入淡出:alpha
ObjectAnimator().ofFloat(imageView,”alpha”,1.0f,0.0f).setDuration(3000).start();
位移动画(X轴):
ObjectAnimator().ofFloat(imageView,”translationX”,0.0f,100.0f).setDuration(3000);
位移动画(Y轴):
ObjectAnimator().ofFloat(imageView,”translationY”,0.0f,100.0f).setDuration(3000);
代码实现如下
imageView.clearAnimation();
ObjectAnimator objectAnimator=new ObjectAnimator().ofFloat(imageView,"translationX",0.0f,100.0f,0.0f,100.0f).setDuration(3000);
objectAnimator.start();
在xml中实现
- 首先需要在res文件夹下新建animator文件夹,在其中定义xml文件如下
<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android"
android:duration="3000">
<objectAnimator
android:propertyName="scaleX"
android:repeatCount="-1"
android:valueFrom="0.0f"
android:valueTo="1.0f"></objectAnimator>
</set>
- 相应的代码中实现如下
Animator animatorObjext = AnimatorInflater.loadAnimator(this, R.animator.objectanimator);
animatorObjext.setTarget(imageView2);
animatorObjext.start();
ValueAnimator
- 代码实现如下
imageView2.clearAnimation();
imageView2.setImageResource(R.mipmap.ic_launcher);
//这里的属性值可以根据需要设置多个
ValueAnimator animator = ValueAnimator.ofFloat(1.0f, 0.0f, 1.0f, 0.0f, 1.0f);//设置属性值
animator.setTarget(imageView2);//设置操作对象
animator.setDuration(5000).start();//动画开始
animator.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() {
@Override
public void onAnimationUpdate(ValueAnimator animation) {
imageView2.setScaleY((Float) animation.getAnimatedValue());//设置Y轴上的变化
imageView2.setScaleX((Float) animation.getAnimatedValue());//设置X轴上的变化
}
});
PropertyValuesHolder 组合动画
- 就是设置多个不同的动画,同时让他们加载就好了
- 相应的代码实现
imageView.clearAnimation();
PropertyValuesHolder scaleX=PropertyValuesHolder.ofFloat("scaleX",1,0,1);
PropertyValuesHolder scaleY=PropertyValuesHolder.ofFloat("scaleY",1,0,1);
PropertyValuesHolder rotattion=PropertyValuesHolder.ofFloat("rotation",0,360);
ObjectAnimator objectAnimator2=new ObjectAnimator().ofPropertyValuesHolder(imageView,scaleX,scaleY,rotattion).setDuration(3000);
objectAnimator2.start();
AnimatorSet 组合动画(可以设置动画的顺序)
- 相应的代码如下
ObjectAnimator objectAnimator3=new ObjectAnimator().ofFloat(imageView,"translationX",0.0f,100.0f,0.0f,100.0f).setDuration(3000);
ObjectAnimator objectAnimator4=new ObjectAnimator().ofFloat(imageView,"scaleX",1,0,1).setDuration(3000);
ObjectAnimator objectAnimator5=new ObjectAnimator().ofFloat(imageView,"rotation",0,360).setDuration(3000);
AnimatorSet animatorSet=new AnimatorSet();
//设置动画同时执行
animatorSet.playTogether(objectAnimator3,objectAnimator4);
//设置动画的执行顺序
animatorSet.play(objectAnimator5).after(objectAnimator3);
animatorSet.start();
]
AnimatorInflater 加载属性动画的xml文件
- xml还是在res文件夹下新建的animator文件夹下的xml文件
- 相应的xml文件的定义如下
<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android"
android:ordering="together"
android:duration="1000">
<!-- ordering 指动画是一起播放还是一个接一个播放-->
<objectAnimator
android:propertyName="scaleX"
android:valueFrom="0.0"
android:valueTo="1.0"></objectAnimator>
<objectAnimator
android:propertyName="scaleY"
android:valueFrom="0.0"
android:valueTo="1.0"></objectAnimator>
</set>
- 相应的逻辑代码如下
imageView2.clearAnimation();
imageView2.setImageResource(R.mipmap.ic_launcher);
Animator animatorMy = AnimatorInflater.loadAnimator(getApplicationContext(), R.animator.animatorinflater);
animatorMy.setTarget(imageView2);
animatorMy.start();