官方地址:http://developer.android.com/guide/topics/graphics/prop-animation.html
注:高级动画要在3.0以上版本才能使用
高级动画包括:ValueAnimator、TimeAnimator、ObjectAnimator、AnimatorSet
关系图:
1.xml定义属性及属性介绍:
<set
android:ordering=["together" | "sequentially"]>
<objectAnimator
android:propertyName="string"
android:duration="int"
android:valueFrom="float | int | color"
android:valueTo="float | int | color"
android:startOffset="int"
android:repeatCount="int"
android:repeatMode=["repeat" | "reverse"]
android:valueType=["intType" | "floatType"]/>
<animator
android:duration="int"
android:valueFrom="float | int | color"
android:valueTo="float | int | color"
android:startOffset="int"
android:repeatCount="int"
android:repeatMode=["repeat" | "reverse"]
android:valueType=["intType" | "floatType"]/>
</set>
<!--
set
android:ordering 它指定该集合中动画的播放顺序:
sequentially:在本组集合中按顺序播放动画
together(默认):同一时间播放本组集合中的动画
objectAnimator
android:propertyName 字符串类型,通过这个名字来引用动画的对象属性(必须)
android:valueTo 浮点类型、整型、或颜色值,它是动画属性的结束值(必须)
android:valueFrom 浮点类型、整型、或颜色值,它是动画属性的开始值
android:duration 整数类型。以毫秒为单位,指定动画时间
android:startOffset 整数类型。在start()方法被调用之后,动画延迟的毫秒数。
android:repeatCount 整数类型。指定动画的重复次数。设置为-1无限次重复,为1播放两次。默认值是0,不重复。
android:repeatMode 正整数。指定在动画到达结尾时的行为方式。只有android:repeatCount被设置为正整数或“-1”时,这个属性才有效果。
如果设置为“reverse”,那么每次重复时会逆向播放动画,如果设置为“repeat”,那么每次重复时,会从动画的开头来播放动画。
android:valueType 关键属性。如果值是颜色值,那么不用指定这个属性。动画框架会自动处理颜色值。
intType 指定动画的值是整数
floatType(默认) 指定动画的值是浮点数
-->
2.ValueAnimator/TimeAnimator使用
ValueAnimator anim = ValueAnimator.ofInt(0, 100);
anim.setDuration(100);
anim.start();
/*然后通过ValueAnimator的AnimatorUpdateListener可以得到动画执行每一帧所返回的值,
利用返回的值对View处理动画效果*/
anim.setEvaluator(new TypeEvaluator<Integer>() {
@Override
public Integer evaluate(float fraction, Integer startValue, Integer endValue) {
return (int)((startValue + fraction * (endValue - startValue)) / 10 * 10);
}
});
anim.addUpdateListener(new AnimatorUpdateListener() {
@Override
public void onAnimationUpdate(ValueAnimator animation) {
//frameValue是通过startValue和endValue以及Fraction计算出来的
int frameValue = (Integer)animation.getAnimatedValue();
//利用每一帧返回的值,可以做一些改变View大小,坐标,透明度等等操作
//用处可以举一反三 (TimeAnimator更好用)
}
});
3 ObjectAnimator介绍:
1)通过传递进来一个对象,以及对象的属性名称,在anim.start()执行过程中不断更改对象的属性值,来实现动画效果的。
2)前提是传递进来的对象的属性,必须要具有相应的set和get方法。
3)ObjectAnimator是继承自ValueAnimator,前者的许多方法其实是调用ValueAnimator的方法,所以说ValueAnimator是动画核心,因为它处理了驱动引擎这一块。
4)ObjectAnimator多传入了两个参数:
一个是target对象(可以是任何对象,不限于View),
一个是对象属性propertyName(前提是所属对象必须拥有对应的setPropertyName(),getPropertyName方法)
5)在上一篇文章介绍的基础动画都可以用ObjectAnimator来实现,如:
ObjectAnimator anm = ObjectAnimator.ofFloat(myView, "alpha", 1);
anm.setDuration(200);
anm.start();
动画对象属性:
1)translationX 和 translationY:这两个属性控制了View所处的位置,
它们的值是由layout容器设置的,是相对于坐标原点(0,0左上角)的一个偏移量。
2)rotation, rotationX 和 rotationY:控制View绕着轴点(pivotX和pivotY)立体旋转。
3)scaleX 和 scaleY:控制View基于pivotX和pivotY的缩放。
4)pivotX 和 pivotY:旋转的轴点和缩放的基准点,默认是View的中心点。
5)x 和 y:描述了view在其父容器中的最终位置,是左上角左标和偏移量(translationX,translationY)的和。
6)aplha:透明度,1是完全不透明,0是完全透明。
4.AnimatorSet使用:
4.1.加载xml文件
AnimatorSet set=(AnimatorSet)AnimatorInflater.loadAnimator(myContext, R.anim.demo_animator);
set.setTarget(Object);
set.start();
4.2.多个属性
PropertyValuesHolder pvhX = PropertyValuesHolder.ofFloat("x", 50f);
PropertyValuesHolder pvhY = PropertyValuesHolder.ofFloat("y", 100f);
ObjectAnimator.ofPropertyValuesHolder(myView, pvhX, pvyY).start();
4.3.多个动画
ObjectAnimator animX = ObjectAnimator.ofFloat(myView, "x", 50f);
ObjectAnimator animY = ObjectAnimator.ofFloat(myView, "y", 100f);
AnimatorSet animSetXY = new AnimatorSet();
animSetXY.playTogether(animX, animY);
animSetXY.start();
//或者
AnimatorSet set3 = new AnimatorSet();
AnimatorSet.Builder builder = set3.play(animX);
builder.with(animY);
// builder.before(anim);
// builder.after(anim);
set3.start();
4.view独有的
ViewPropertyAnimator animator = View.animate();
animator.x(50f);
animator.start();
5.如果要多次改变动画效果,可以直接设置keyFrame:
//前半段从0变到360度,后半段从360度变回0度
Keyframe kf0 = Keyframe.ofFloat(0f, 0f);
Keyframe kf1 = Keyframe.ofFloat(.5f, 360f);
Keyframe kf2 = Keyframe.ofFloat(1f, 0f);
PropertyValuesHolder pvhRotation = PropertyValuesHolder.ofKeyframe("rotation", kf0, kf1, kf2);
ObjectAnimator rotationAnim = ObjectAnimator.ofPropertyValuesHolder(target, pvhRotation)
rotationAnim.setDuration(5000ms);
6.用属性动画换背景色
private static final int RED = 0xffFF8080;
private static final int BLUE = 0xff8080FF;
private static final int CYAN = 0xff80ffff;
private static final int GREEN = 0xff80ff80;
//动画 变色
ObjectAnimator colorAnim = ObjectAnimator.ofInt(this, "backgroundColor", CYAN, BLUE, RED);
colorAnim.setTarget(ll_animation);
colorAnim.setEvaluator(new ArgbEvaluator());
colorAnim.setRepeatCount(ValueAnimator.INFINITE);
colorAnim.setRepeatMode(ValueAnimator.REVERSE);
colorAnim.setDuration(3000);
colorAnim.start();
7.LayoutTransition
/*
* ViewGroup中使用LayoutTransition 进行 监听布局的改变,而创建动画
* LayoutTransition.APPEARING 新增出现时
* CHANGE_APPEARING
* CHANGE_DISAPPEARING 子view消失时
* CHANGING
* ViewGroup布局中:android:animateLayoutChanges="true" 使用的默认的LayoutTransition制作动画
*/
LayoutTransition layoutTransition = new LayoutTransition();
layoutTransition.setDuration(5000);
layoutTransition.setAnimator(LayoutTransition.APPEARING, scaleAnimator);
ll_animation.setLayoutTransition(layoutTransition);
final TextView tv = new TextView(this);
tv.setWidth(100);
tv.setHeight(100);
tv.setText("中华人民共和国");
ll_animation.addView(tv);//对应type = APPEARING
ll_animation.postDelayed(new Runnable() {
@Override
public void run() {
ll_animation.removeView(tv);
}
}, 2000);