Android动画

动画

Android动画共分为四类

  1. Alpha 淡入淡出
  2. Scale 缩放
  3. Rotate 旋转
  4. Translate 平移

Alpha

//参数范围 0~1,0:完全透明, 1:完全不透明
//参数1: fromAlpha开始的透明度
//参数2: toAlpha结束的透明度
AlphaAnimation aa = new AlphaAnimation(1, 0);
aa.setDuration(300);//动画执行时间
View.startAnimation(aa);//启动动画,把View替换成要添加动画的控件

Scale

/** 
* Constructor to use when building a ScaleAnimation from code 
* 
* @param fromX Horizontal scaling factor to apply at the start of the 
*        animation 
* @param toX Horizontal scaling factor to apply at the end of the animation 
* @param fromY Vertical scaling factor to apply at the start of the 
*        animation 
* @param toY Vertical scaling factor to apply at the end of the animation 
* @param pivotXType Specifies how pivotXValue should be interpreted. One of 
*        Animation.ABSOLUTE, Animation.RELATIVE_TO_SELF, or 
*        Animation.RELATIVE_TO_PARENT. 
* @param pivotXValue The X coordinate of the point about which the object 
*        is being scaled, specified as an absolute number where 0 is the 
*        left edge. (This point remains fixed while the object changes 
*        size.) This value can either be an absolute number if pivotXType *        is ABSOLUTE, or a percentage (where 1.0 is 100%) otherwise. 
* @param pivotYType Specifies how pivotYValue should be interpreted. One of 
*        Animation.ABSOLUTE, Animation.RELATIVE_TO_SELF, or 
*        Animation.RELATIVE_TO_PARENT. 
* @param pivotYValue The Y coordinate of the point about which the object
*        is being scaled, specified as an absolute number where 0 is the 
*        top edge. (This point remains fixed while the object changes 
*        size.) This value can either be an absolute number if pivotYType 
*        is ABSOLUTE, or a percentage (where 1.0 is 100%) otherwise. 
*/
ScaleAnimation sa = new ScaleAnimation(0,1,0,1,Animation.RELATIVE_TO_SELF, 0.5f, Animation.RELATIVE_TO_SELF,0.5f);
sa.setDuration(3000);
View.startAnimation(sa);

Rotate

/** 
* Constructor to use when building a RotateAnimation from code 
*  
* @param fromDegrees Rotation offset to apply at the start of the 
*        animation. 
*  
* @param toDegrees Rotation offset to apply at the end of the animation. 
*  
* @param pivotXType Specifies how pivotXValue should be interpreted. One of 
*        Animation.ABSOLUTE, Animation.RELATIVE_TO_SELF, or 
*        Animation.RELATIVE_TO_PARENT. 
* @param pivotXValue The X coordinate of the point about which the object 
*        is being rotated, specified as an absolute number where 0 is the 
*        left edge. This value can either be an absolute number if 
*        pivotXType is ABSOLUTE, or a percentage (where 1.0 is 100%) 
*        otherwise. 
* @param pivotYType Specifies how pivotYValue should be interpreted. One of 
*        Animation.ABSOLUTE, Animation.RELATIVE_TO_SELF, or 
*        Animation.RELATIVE_TO_PARENT. 
* @param pivotYValue The Y coordinate of the point about which the object 
*        is being rotated, specified as an absolute number where 0 is the 
*        top edge. This value can either be an absolute number if 
*        pivotYType is ABSOLUTE, or a percentage (where 1.0 is 100%) 
*        otherwise. 
*/
RotateAnimation ra = new RotateAnimation(0,720,Animation.RELATIVE_TO_PARENT,0.5f,Animation.RELATIVE_TO_PARENT,0.5f);
ra.setDuration(3000);
View.startAnimation(ra);

Translate

TranslateAnimation ta = new TranslateAnimation(Animation.RELATIVE_TO_SELF,0,Animation.RELATIVE_TO_SELF,2,Animation.RELATIVE_TO_SELF,0,Animation.RELATIVE_TO_SELF,2);
ta.setDuration(3000);
View.startAnimation(ta);

Animation其他常用的方法

setRepeatCount(Animation.INFINITE);//设置动画的重复次数,INFINITE为无限次
setFillAfter(boolean fillAfter);//true:动画结束后,控件停留在执行后的状态
setFillBefore(boolean fillBefore);//true:动画结束后,控件停留在动画开始的状态
setInterpolator(new Interpolator);//动画的运动速度

组合动画

AnimationSet set = new Animation(true);
set.addAnimation(aa);
set.addAnimation(ra);
set.addAnimation(sa);
set.addAnimation(ta);
set.setDuration(3000);
set.setStartOffset(100);
View.startAnimation(set);

XML动画

xml文件存放在:res/anim下

XML组合动画 animation.xml

  
  
     
      
       
      
      

  

XML动画用法

代码获取动画

Animation a=AnimationUtils.loadAnimation(this, R.anim.animation);
View.startAnimation(a);

XML配置动画








android:theme="@style/Theme.DialogActivity"

帧动画


  
 
 
 
   
   
 

ImageView.setBackgroundResource(R.drawable.anim);
AnimationDrawable ad = (AnimationDrawable)ImageView.getBackground();
ad.start();

LayoutAnimationController动画

XML 实现


  
  
      
  



  



  

代码实现

AlphaAnimation alpha=new AlphaAnimation(0, 1);  
alpha.setDuration(3000);  
LayoutAnimationController lac=new LayoutAnimationController(alpha); 
//LayoutAnimationController.ORDER_NORMAL;//顺序显示  
//LayoutAnimationController.ORDER_REVERSE;//反显示  
//LayoutAnimationController.ORDER_RANDOM;//随机显示
lac.setOrder(LayoutAnimationController.ORDER_NORMAL);  
lv.setLayoutAnimation(lac); 

你可能感兴趣的:(Android动画)