一、属性动画
- ObjectAnimator
ObjectAnimator是属性动画框架中最重要的实行类,创建一个ObjectAnimator只需通过他的静态工厂类直接返回一个ObjectAnimator对象。参数包括一个对象和对象的属性名字,但这个属性必须有get和set函数,内部会通过JAVA反射机制来调用set函数修改属性值,同样,你也可以调用setInterpolator设置相信的差值器。
例子:实现平移动画:
/** * view:需要实现动画效果的view * "translationX":需要什么动画 * 300:可变参数数组,需要传进改属性的一个取值过程 */ ObjectAnimator animator=ObjectAnimator.ofFloat(view,"translationX",300); animator.setDuration(2000); animator.start();
使用ObjectAnimator的时候,操纵的属性必须要有get和set方法,但是一般内部都会自己创建。
下面是一些常用的可以直接使用属性动画的属性值:
translationX和translationY:这连个属性为平移,从view左上角坐标开始位置
rotation、rotationX和rotationY:围绕支点2D和3D旋转
scaleX和 scaleY:围绕支点2D缩放
pivotX和pivoY:这个是控制支点位置,支点控制着旋转和缩放,默认为view中点为支点
x和y:View最终位置,它是最初是view左上角坐标和translationX、translationY值的累积和
alpha:透明度,默认为1(不透明),0位完全透明。
- PropertyValuesHolder
类似视图动画中的AnimationSet
例子:
/** * view将先缩小,透明度慢慢变为0,在慢慢放大,透明度变为1 */ PropertyValuesHolder pvh1 = PropertyValuesHolder.ofFloat("scaleX", 1f, 0f, 1f); PropertyValuesHolder pvh2 = PropertyValuesHolder.ofFloat("scaleY", 1f, 0f, 1f); PropertyValuesHolder pvh3 = PropertyValuesHolder.ofFloat("alpha",1f, 0f, 1f); ObjectAnimator.ofPropertyValuesHolder(view, pvh1, pvh2, pvh3).setDuration(4000).start();
- ValueAnimtor
属性动画的核心所在,ObjectAnimator继承于它
ValueAnimator animator = ValueAnimator.ofFloat(0, 1); animator.setTarget(view); animator.setDuration(2000).start(); animator.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() { @Override public void onAnimationUpdate(ValueAnimator valueAnimator) { Float value = (Float) valueAnimator.getAnimatedValue();//拿到value然后进行操作 value由0-->1 view.setAlpha(value); } });
- 动画监听
ObjectAnimator animator = ObjectAnimator.ofFloat(imageView, "alpha",1f, 0f, 1f); animator.addListener(new Animator.AnimatorListener() { @Override public void onAnimationStart(Animator animator) { } @Override public void onAnimationEnd(Animator animator) { } @Override public void onAnimationCancel(Animator animator) { } @Override public void onAnimationRepeat(Animator animator) { } }); animator.start();
我们一般只用到end这个方法,android提供了另外一个方法:
animator.addListener(new AnimatorListenerAdapter() { @Override public void onAnimationEnd(Animator animation) { super.onAnimationEnd(animation); } });
- Animatorset
对于一个属性同时作用多个属性动画效果,前面已经用PropetryValuesHolder实现了这样的效果。而AnimatorSet不仅能实现这样的效果,同时也能实现更为精确的顺序控制。
ObjectAnimator animator1 = ObjectAnimator.ofFloat(imageView, "scaleX", 1f, 0f, 1f); ObjectAnimator animator2 = ObjectAnimator.ofFloat(imageView, "scaleY", 1f, 0f, 1f); ObjectAnimator animator3 = ObjectAnimator.ofFloat(imageView, "alpha", 1f, 0f, 1f); AnimatorSet set = new AnimatorSet(); set.setDuration(2000); set.playTogether(animator1, animator2, animator3);//顺序依次是1-->2-->3 set.start();
在AnimatorSet正是通过playTogether()、playSequentially()、animSet.play().with()、defore()、after()这些方法来控制多个动画的协同工作方式,从而做到对动画播放顺序的精确控制。
- 在xml中使用属性动画
在res文件中创建文件夹:animator,在这个文件夹里面创建objectAnimator资源文件
<?xml version="1.0" encoding="utf-8"?> <objectAnimator xmlns:android="http://schemas.android.com/apk/res/android" android:duration="1000" android:propertyName="scaleX" android:valueFrom="1.0" android:valueTo="2.0" android:valueType="floatType"> </objectAnimator>
Animator animator = AnimatorInflater.loadAnimator(this, R.animator.test); animator.setTarget(view); animator.start();
- view的animate方法
animate方法可以理解为属性动画的一种简写方式
view.animate().alpha(0).y(300).setDuration(300).withStartAction(new Runnable() { @Override public void run() { } }).withEndAction(new Runnable() { @Override public void run() { runOnUiThread(new Runnable() { @Override public void run() { } }); } }).start();
二、布局动画
所谓的布局动画是指作用的ViewGroup上,给ViewGroup增加子View时添加一个动画过度效果。
在ViewGroup的xml中添加和这个属性:
android:animateLayoutChanges="true"
这个效果是android默认的显示的过度效果,我们无法自定义,需要自在定义的过度效果则需要使用LayoutAnimationController类来自定义一个。
/** * 第一个参数:需要作用的时间 * 第二个参数:每个子view显示的delay时间 * 当第二个参数不为0的时候可以设置View的显示顺序: * LayoutAnimationController.ORDER_NORMAL:顺序 * LayoutAnimationController.ORDER_RANDOM:随机 * LayoutAnimationController.ORDER_REVERSE:反序 * */ AlphaAnimation animation = new AlphaAnimation(0, 1); animation.setDuration(2000); LayoutAnimationController lac=new LayoutAnimationController(animation); lac.setOrder(LayoutAnimationController.ORDER_REVERSE); rl.setLayoutAnimation(lac);
xml中设置: