安卓提供的Api四种动画:
动画常用API:
1 setDuration(3000); //动画播放时间毫秒
2 setFillAfter(true); //显示动画结束时View的样子,动画结束时View长啥样,之后的显示就长啥样
3 setRepeatCount(5); //重复播放次数
淡入淡出:AlphaAnimation
最简单的动画,透明到显示或者显示到透明的动画.
1 public void alpha(View view){
2 float fromAlpha = 0.1f; //
3 float toAlpha = 1f;
4 AlphaAnimation alpha = new AlphaAnimation(fromAlpha, toAlpha);
5 alpha.setDuration(3000); //动画持续时间
6 alpha.setRepeatCount(5); //重复播放次数
7 iv.startAnimation(alpha);
8 }
比例缩放:ScaleAnimation
这种动画还是挺好用的,掌握好缩放尺寸可以了,当然它也是有锚点的.
1 public void scale(View view){
2 //这4个值,0表示缩小到没有,1表示正常,比1大就是放大
3 float fromX = 0.1f; //动画开始时x的缩放尺寸
4 float toX = 2.0f; //动画结束时x的缩放尺寸
5 float fromY = 0.1f; //动画开始时y的缩放尺寸
6 float toY = 2.0f; //动画结束时y的缩放尺寸
7 //设置锚点的四个参数
8 int pivotXType = Animation.RELATIVE_TO_SELF; //x相对于物件自己的
9 float pivotXValue = 0.5f; //取值范围0~1之间,相对的x轴位置,相对于物件就是物件的宽度中间,相对于屏幕就是屏幕中间
10 int pivotYType = Animation.RELATIVE_TO_SELF; //y相对于物件自己的
11 float pivotYValue = 0.5f;//取值范围0~1之间,相对的y轴位置,相对于物件就是物件的宽度中间,相对于屏幕就是屏幕中间
12 ScaleAnimation scale = new ScaleAnimation(fromX, toX, fromY, toY, pivotXType, pivotXValue, pivotYType, pivotYValue);
13 scale.setDuration(3000);
14 scale.setFillAfter(true); //显示动画结束时View的样子,上面是2.0f倍放大,那么显示就会是那么大
15 iv.startAnimation(scale);
16 }
锚点旋转:RotateAnimation
这种动画比较容易控制的,只要想清楚锚点的四个参数,很容易掌握.
1 public void rotate(View view){
2 float fromDegrees = 0; //动画开始的角度
3 float toDegrees = -360; //动画结束时旋转的角度(可以大于360)正数顺时针旋转,负数逆时针旋转
4 //设置锚点的四个参数
5 int pivotXType = Animation.RELATIVE_TO_SELF; //x相对于物件自己的
6 float pivotXValue = 0.5f; //取值范围0~1之间,相对的x轴位置,相对于物件就是物件的宽度中间,相对于屏幕就是屏幕中间
7 int pivotYType = Animation.RELATIVE_TO_SELF; //y相对于物件自己的
8 float pivotYValue = 0.5f;//取值范围0~1之间,相对的y轴位置,相对于物件就是物件的宽度中间,相对于屏幕就是屏幕中间
9 RotateAnimation rotate = new RotateAnimation(fromDegrees, toDegrees, pivotXType, pivotXValue, pivotYType, pivotYValue);
10 rotate.setDuration(3000);
11 iv.startAnimation(rotate);
12 }
位移:
位移动画,用到比较多,比如在手机卫士程序锁中,点击一个应用加锁,就把这个应用View向又移动消失...
1 public void translate(View view){
2 int fromXType = Animation.RELATIVE_TO_SELF; //x轴相对的物件
3 float fromXValue = 0f; //从相对于物件x轴x位置
4 int toXType = Animation.RELATIVE_TO_PARENT; //x轴移动相对物
5 float toXValue = -1f; //x移动
6 int fromYType = Animation.RELATIVE_TO_SELF; //y轴相对的物件
7 float fromYValue = 0f; //从相对于物件y轴y位置
8 int toYType = Animation.RELATIVE_TO_PARENT; //y轴移动相对物
9 float toYValue = 0f; //y移动
10 TranslateAnimation translate = new TranslateAnimation(fromXType, fromXValue, toXType, toXValue, fromYType, fromYValue, toYType, toYValue);
11 translate.setDuration(3000);
12 iv.startAnimation(translate);
13 }
动画集合:AnimationSet
偶尔做一些好玩的动画还是可以的
1 public void animationSet() {
2 //创建动画
3 AlphaAnimation alpha = new AlphaAnimation(0.1f,1f);
4 alpha.setDuration(3000);
5 alpha.setRepeatCount(3);
6
7 RotateAnimation rotate = new RotateAnimation(0f,360,Animation.RELATIVE_TO_SELF,0.5f,Animation.RELATIVE_TO_SELF,0.5f);
8 rotate.setDuration(3000);
9 rotate.setRepeatCount(3);
10
11 ScaleAnimation scale = new ScaleAnimation(1f,0.1f,1f,0.1f,Animation.RELATIVE_TO_SELF,0.5f,Animation.RELATIVE_TO_SELF,0.5f);
12 scale.setDuration(3000);
13 scale.setRepeatCount(3);
14
15 TranslateAnimation translate = new TranslateAnimation(Animation.RELATIVE_TO_SELF,0f,Animation.RELATIVE_TO_PARENT,-1f,Animation.RELATIVE_TO_SELF,0f,Animation.RELATIVE_TO_PARENT,0f);
16 translate.setDuration(3000);
17 translate.setRepeatCount(3);
18
19 AnimationSet set = new AnimationSet(false); //建立动画集合,false是让每个动画参数独立
20 //把动画加入集合
21 set.addAnimation(alpha);
22 set.addAnimation(rotate);
23 set.addAnimation(scale);
24 set.addAnimation(translate);
25 iv.startAnimation(set); //控件播放动画
26 }