android animation

Animation主要有四大属性,分别是淡入淡出,绕轴旋转,变化大小,位移变化,如图:

(图)android中Animation的使用!

 

这些属性还有一些共同的方法:

(图)android中Animation的使用!

 

 

下面是一个实例代码:

JAVA代码
  1.   
  2. public class MainActivity extends Activity implements OnClickListener     
  3. {     
  4.     /**   
  5.      * 定义四个按钮和一张图片   
  6.      */     
  7.     private ImageView imageView = null;     
  8.     private Button rotateButton = null;     
  9.     private Button scaleButton = null;     
  10.     private Button alphaButton = null;     
  11.     private Button translateButton = null;     
  12.      
  13.     @Override     
  14.     public void onCreate(Bundle savedInstanceState)     
  15.     {     
  16.         super.onCreate(savedInstanceState);     
  17.         setContentView(R.layout.main);     
  18.         initView();     
  19.     }     
  20.      
  21.     /**   
  22.      * 初始化界面   
  23.      */     
  24.     public void initView()     
  25.     {     
  26.         imageView = (ImageView) findViewById(R.id.imageViewId);     
  27.      
  28.         rotateButton = (Button) findViewById(R.id.rotateButtonId);     
  29.         translateButton = (Button) findViewById(R.id.translateButtonId);     
  30.         scaleButton = (Button) findViewById(R.id.scaleButtonId);     
  31.         alphaButton = (Button) findViewById(R.id.alphaButtonId);     
  32.      
  33.         rotateButton.setOnClickListener(this);     
  34.         scaleButton.setOnClickListener(this);     
  35.         alphaButton.setOnClickListener(this);     
  36.         translateButton.setOnClickListener(this);     
  37.     }     
  38.      
  39.     @Override     
  40.     public void onClick(View v)     
  41.     {     
  42.         // TODO Auto-generated method stub     
  43.         int switchID = v.getId();     
  44.         switch (switchID)     
  45.         {     
  46.         case R.id.alphaButtonId:     
  47.         {     
  48.                  
  49.             AnimationSet animationSet = new AnimationSet(true);//创建一个AnimationSet对象     
  50.             AlphaAnimation alphaAnimation = new AlphaAnimation(1, 0);//创建一个AlphaAnimation对象              
  51.             alphaAnimation.setDuration(1000);//设置动画执行的时间(单位:毫秒)         
  52.             animationSet.addAnimation(alphaAnimation);//将AlphaAnimation对象添加到AnimationSet当中           
  53.             imageView.startAnimation(animationSet);//使用ImageView的startAnimation方法开始执行动画          
  54.             break;     
  55.         }     
  56.      
  57.         case R.id.rotateButtonId:     
  58.         {     
  59.             AnimationSet animationSet = new AnimationSet(true);     
  60.      
  61.             /**   
  62.              * 前两个参数定义旋转的起始和结束的度数,后两个参数定义圆心的位置   
  63.              */     
  64.             RotateAnimation rotateAnimation = new RotateAnimation(0, 360,     
  65.                     Animation.RELATIVE_TO_PARENT, 1f,     
  66.                     Animation.RELATIVE_TO_PARENT, 0f);     
  67.      
  68.             rotateAnimation.setDuration(5000);     
  69.             animationSet.addAnimation(rotateAnimation);     
  70.             imageView.startAnimation(animationSet);     
  71.             break;     
  72.         }     
  73.      
  74.         case R.id.scaleButtonId:     
  75.         {     
  76.             AnimationSet animationSet = new AnimationSet(true);     
  77.                  
  78.             /**   
  79.              * 围绕一个点伸缩   
  80.              */     
  81.             ScaleAnimation scaleAnimation = new ScaleAnimation(1, 0.1f, 1,     
  82.                     0.1f, Animation.RELATIVE_TO_SELF, 0.5f,     
  83.                     Animation.RELATIVE_TO_SELF, 0.5f);     
  84.             animationSet.addAnimation(scaleAnimation);     
  85.             animationSet.setStartOffset(1000);     
  86.             animationSet.setFillAfter(true);     
  87.             animationSet.setFillBefore(false);     
  88.             animationSet.setDuration(2000);     
  89.             imageView.startAnimation(animationSet);     
  90.             break;     
  91.         }     
  92.      
  93.         case R.id.translateButtonId:     
  94.         {     
  95.             AnimationSet animationSet = new AnimationSet(true);     
  96.                  
  97.             /**   
  98.              * x和y轴的起始和结束位置   
  99.              */     
  100.             TranslateAnimation translateAnimation = new TranslateAnimation     
  101.             (     
  102.                     Animation.RELATIVE_TO_SELF, 0f,      
  103.                     Animation.RELATIVE_TO_SELF,0.5f,      
  104.                     Animation.RELATIVE_TO_SELF, 0f,     
  105.                     Animation.RELATIVE_TO_SELF, 1.0f     
  106.             );     
  107.                  
  108.             translateAnimation.setDuration(1000);     
  109.             animationSet.addAnimation(translateAnimation);     
  110.             imageView.startAnimation(animationSet);     
  111.             break;     
  112.         }     
  113.         }     
  114.     }     
  115. }     
posted @ 2011-12-06 00:35  xiaohuang  阅读(221)  评论(0)    收藏  举报