属性动画包含:
ObjectAnimator 动画的执行类
ValueAnimator 动画的执行类
AnimatorSet 用于控制一组动画的执行:线性,一起,每个动画的先后执行等。
AnimatorInflater 用户加载属性动画的xml文件
缩放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 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();
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轴上的变化
}
});
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();
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();
<?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();