本文将按视图动画和属性动画讲解。
android3.0之前视图动画一家独大,使用也非常简单,提供了AlphaAnimation(透明度),RotateAnimation(旋转动画),TranslateAnimation(平移动画),ScaleAnimation(放大缩小动画)。android3.0之后,出现属性动画,由于视图动画交互事件的缺陷性(视图动画后事件的相应依旧在原动画位置),属性动画得到了开发者的青睐。
一、视图动画
1.透明度动画
/**
* 透明度动画
*/
private void alphaAnim(){
AlphaAnimation anim=new AlphaAnimation(1.0f,0.5f);
anim.setDuration(500);
slideTv.startAnimation(anim);
}
2.旋转动画
/**
* 旋转动画
*/
private void rotateAnim(){
//参数:起始角度,终止角度,中心的坐标[0,0]
RotateAnimation anim= new RotateAnimation(90,180,0,0);
anim.setDuration(500);
slideTv.startAnimation(anim);
}
3.平移动画
/**
* 平移动画
*/
private void translateAnim(){
//参数:x起始位置,x终止位置,y起始位置,y终止位置
TranslateAnimation anim= new TranslateAnimation(0,100,0,300);
anim.setDuration(500);
slideTv.startAnimation(anim);
}
4.放大缩小
/**
* 放大缩小动画
*/
private void scaleAnim(){
//参数:x起始倍数,x终止倍数,y倍数位置,y终止倍数。1为原始,大于1放大,小于1缩小
ScaleAnimation anim= new ScaleAnimation(1,1.5f,1,1.5f);
anim.setDuration(500);
slideTv.startAnimation(anim);
}
5.AnimationSet动画集,同时执行放大,缩小,透明度等动画
private void animSet(){
AnimationSet anim= new AnimationSet(true);
anim.setDuration(500);
ScaleAnimation Sanim= new ScaleAnimation(1,1.5f,1,1.5f);
Sanim.setDuration(500);
anim.addAnimation(Sanim);
TranslateAnimation Tanim= new TranslateAnimation(0,100,0,300);
Tanim.setDuration(500);
anim.addAnimation(Tanim);
slideTv.startAnimation(anim);
}
二、属性动画
1.ObjectAnimatior是属性动画框架重要的实行类,需要注意的是属性动画的实施需要对象的属性必须有set和get方法,android内部会通过反射机制调用到set函数对对象的属性进行修改。
先通过ObjectAnimtor实现上述的透明度,旋转,平移,放大缩小:
/**
* 透明度动画
*/
private void objAlphaAnim(){
/**
* 属性值只有:alpha
*/
ObjectAnimator anim=ObjectAnimator.ofFloat(slideTv,"alpha",0.5f);
anim.setDuration(500);
anim.start();
}
/**
* 旋转动画
*/
private void objRotateAnim(){
/**
* 表示横向中心旋转360
* 属性值:rotationX,rotationY,rotation
*/
ObjectAnimator anim=ObjectAnimator.ofFloat(slideTv,"rotationX",360);
anim.setDuration(1500);
anim.start();
}
/**
* 平移动画
*/
private void objTranslateAnim(){
/**
* 表示从距离当前位置200处向距离当前100处移动,然后从距离当前100处移动到距离当前三百处
* 属性值:translationX,translationY
*/
ObjectAnimator anim=ObjectAnimator.ofFloat(slideTv,"translationX",200,100,300);
anim.setDuration(1500);
anim.start();
}
/**
* 放大缩小动画
*/
private void objScaleAnim(){
/**
* 属性值:scaleX,scaleY
*/
ObjectAnimator anim=ObjectAnimator.ofFloat(slideTv,"scaleX",0.2f);
anim.setDuration(500);
anim.start();
}
2.PropertyValuesHolder
该它类似视图动画中的动画集AnimationSet,可以实现多种动画一起播放,譬如:x,y轴的同时位移,如果只使用ObjectAnimation只能顺序的播放x方向位移,再播放y方向的位移。
private void objAnimSet(){
PropertyValuesHolder pvh1=PropertyValuesHolder.ofFloat("scaleX",1.2f);
PropertyValuesHolder pvh2=PropertyValuesHolder.ofFloat("scaleY",1.2f);
PropertyValuesHolder pvh3=PropertyValuesHolder.ofFloat("translationX",100);
PropertyValuesHolder pvh4=PropertyValuesHolder.ofFloat("translationY",100);
PropertyValuesHolder pvh5=PropertyValuesHolder.ofFloat("alpha",0.3f);
ObjectAnimator.ofPropertyValuesHolder(slideTv,pvh1,pvh2,pvh3,pvh4,pvh5).setDuration(2000).start();
}
3.ValueAnimtor
ValueAnimtor本身不提供任何的动画效果,ObjectAnimtor继承自ValueAnimtor,ValueAnimtor更像一个数值监听器,监听数值变化完成动画效果。
我们写一个需求:当滑动view时,不断变化其背景色,效果图:
代码:
private void valueAnim(int offSetX){
ValueAnimator anim=ValueAnimator.ofFloat(0,offSetX);
anim.setDuration(300);
anim.start();
anim.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() {
@Override
public void onAnimationUpdate(ValueAnimator animation) {
if((float)animation.getAnimatedValue()%3==0){
slideTv.setBackgroundResource(R.color.test1);
}else if((float)animation.getAnimatedValue()%3==1){
slideTv.setBackgroundResource(R.color.test2);
}else {
slideTv.setBackgroundResource(R.color.test3);
}
}
});
}
4.AnimatorSet
注意AnimatorSet和视图动画中的AnimationSet拼写是不一样的。但它和视图动画都能同时完成几个动画效果。它能和PropertyValuesHolder达到同样的效果,且比PropertyValuesHolder有更丰富的效果,譬如:通过playTogether,playSequentially等对动画播放顺序进行精确控制。
private void objAnimSet(){
ObjectAnimator anim1=ObjectAnimator.ofFloat(slideTv,"scaleX",1.2f);
ObjectAnimator anim2=ObjectAnimator.ofFloat(slideTv,"translationY",300);
ObjectAnimator anim3=ObjectAnimator.ofFloat(slideTv,"alpha",0.2f,1f);
AnimatorSet set=new AnimatorSet();
set.setDuration(1000);
set.playTogether(anim1,anim2,anim3);
set.start();
}
5.view的animate方法
我们也可以直接通过view.animate()方法直接驱动动画,它相当于属性动画的简写方式。
private void objViewSet(){
slideTv.animate().alpha(0.2f).translationY(400).withStartAction(new Runnable() {
@Override
public void run() {
Toast.makeText(MainActivity.this,"动画开始",Toast.LENGTH_SHORT).show();
}
}).withEndAction(new Runnable() {
@Override
public void run() {
Toast.makeText(MainActivity.this,"end",Toast.LENGTH_SHORT).show();
}
}).setDuration(3000).start();
}