android之animation

时间:2021-12-15 10:08:53

Android  Animation一共有四种

Alpha: 淡入淡出效果

Scale: 缩放效果

Rotate: 旋转效果

Translate:移动效果

使用Tweened Animations的步骤

1.创建一个AnimationSet对象

2.根据需要创建需要的Animation对象

3.根据软件动画的需要,为Animation对象设置相应的数据

4.将Animation对象添加到AnimationSet对象中

5.使控件对象开始执行AnimationSet

Alpha动画

AnimationSet as=new AnimationSet(true); 
AlphaAnimation al=new AlphaAnimation(1,0); 
//1代表完全不透明,0代表完全透明  
al.setDuration(3000); 
as.addAnimation(al); 
iv.startAnimation(as);

AnimationSet as=new AnimationSet(true);
AlphaAnimation al=new AlphaAnimation(1,0);
 //1代表完全不透明,0代表完全透明
 al.setDuration(3000);
 as.addAnimation(al);
 iv.startAnimation(as);

Rotate动画

AnimationSet as=new AnimationSet(true); 
RotateAnimation al=new RotateAnimation (0,720,Animation.RELATIVE_TO_PARENT,0.5f,Animation.RELATIVE_TO_PARENT,0.5f); 
//前两个参数参数旋转的角度,后面几个参数决定旋转的中心  
//Animation.ABSOLUTE:绝对坐标  
//Animation.RELATIVE_TO_PARENT:相对父控件  
//Animation.RELATIVE_TO_SELF:相对自己  
al.setDuration(3000); 
as.addAnimation(al); 
iv.startAnimation(as);

AnimationSet as=new AnimationSet(true);
  RotateAnimation al=new RotateAnimation (0,720,Animation.RELATIVE_TO_PARENT,0.5f,Animation.RELATIVE_TO_PARENT,0.5f);
  //前两个参数参数旋转的角度,后面几个参数决定旋转的中心
  //Animation.ABSOLUTE:绝对坐标
  //Animation.RELATIVE_TO_PARENT:相对父控件
  //Animation.RELATIVE_TO_SELF:相对自己
  al.setDuration(3000);
  as.addAnimation(al);
  iv.startAnimation(as);

Scale动画

AnimationSet as=new AnimationSet(true); 
ScaleAnimation al=new ScaleAnimation(0,1,0,1,Animation.RELATIVE_TO_SELF,0.5f,Animation.RELATIVE_TO_SELF,0.5f); 
//前四个参数是X从多大到多大,Y从多大到多大,后面的参数是缩放的中心点  
            al.setDuration(3000); 
            as.addAnimation(al); 
            iv.startAnimation(as);

AnimationSet as=new AnimationSet(true);
ScaleAnimation al=new ScaleAnimation(0,1,0,1,Animation.RELATIVE_TO_SELF,0.5f,Animation.RELATIVE_TO_SELF,0.5f);
   //前四个参数是X从多大到多大,Y从多大到多大,后面的参数是缩放的中心点
   al.setDuration(3000);
   as.addAnimation(al);
   iv.startAnimation(as);

Translate动画

AnimationSet as=new AnimationSet(true); 
TranslateAnimation al=new TranslateAnimation(Animation.RELATIVE_TO_SELF,0,Animation.RELATIVE_TO_SELF,2,Animation.RELATIVE_TO_SELF,0,Animation.RELATIVE_TO_SELF,2); 
            al.setDuration(3000); 
            as.addAnimation(al); 
            iv.startAnimation(as);

AnimationSet as=new AnimationSet(true);
   TranslateAnimation al=new TranslateAnimation(Animation.RELATIVE_TO_SELF,0,Animation.RELATIVE_TO_SELF,2,Animation.RELATIVE_TO_SELF,0,Animation.RELATIVE_TO_SELF,2);
   al.setDuration(3000);
   as.addAnimation(al);
   iv.startAnimation(as);
Animation还有几个方法

setFillAfter(boolean fillAfter)

如果fillAfter的值为真的话,动画结束后,控件停留在执行后的状态

setFillBefore(boolean fillBefore)

如果fillBefore的值为真的话,动画结束后,控件停留在动画开始的状态

setStartOffset(long startOffset)

设置动画控件执行动画之前等待的时间

setRepeatCount(int repeatCount)

设置动画重复执行的次数