Android基础动画

来源:互联网 发布:美橙互联 数据库 编辑:程序博客网 时间:2024/06/03 12:33
  1. 动画的各种写法:
  2. public class firstActivity extends Activity {  
  3. /** Called when the activity is first created. */  
  4. @Override  
  5. public void onCreate(Bundle savedInstanceState) {//重载onCreate方法  
  6.     super.onCreate(savedInstanceState);  
  7.     setContentView(R.layout.main);  
  8.  
  9.     final ImageView image=(ImageView)findViewById(R.id.imageView1); //ImageView对象  
  10.     Button btn1=(Button)findViewById(R.id.button1);//按钮对象  
  11.     Button btn2=(Button)findViewById(R.id.button2);  
  12.     final Animation translateAnimation=new TranslateAnimation(0,200,0,200); //移动动画效果  
  13.  
  14.     btn1.setOnClickListener(new View.OnClickListener() { //设置监听器  
  15.           
  16.         @Override  
  17.         public void onClick(View v) {  
  18.             // TODO Auto-generated method stub  
  19.             translateAnimation.setDuration(3000);//设置动画持续时间  
  20.             translateAnimation.setStartOffset(3000);//设置启动时间  
  21.   translateAnimation.setRepeatMode(Animation.REVERSE);//反方向执行
  22.             image.setAnimation(translateAnimation); //设置动画效果  
  23.             translateAnimation.startNow();//启动动画  
  24.         }  
  25.     });  
  26.     btn2.setOnClickListener(new View.OnClickListener() {//设置监听器  
  27.           
  28.         @Override  
  29.         public void onClick(View v) {  
  30.             // TODO Auto-generated method stub  
  31.             translateAnimation.cancel(); //取消动画执行  
  32.         }  
  33.     });  
  34. }  

  35. 标注1:

  36. TranslateAnimation (float fromXDelta, float toXDelta, float fromYDelta, float toYDelta)的四个参数:

    fromXDelta:位置变化的起始点X坐标。

    toXDelta:位置变化的结束点X坐标。

    fromYDelta:位置变化的起始点Y坐标。

    toYDelta:位置变化的结束点Y坐标。


  37.    标注2:
  38. 如果设置的重复模式为Animation.RESTART,则表示重新从头开始执行。读者执行这段代码,当点击"执行动画"按钮时,图片开始移动,当一次动画执行结束之后,图片将重新从头开始执行,执行效果如图9.5所示。

    如果设置的重复模式为Animation.REVERSE,则表示反方向执行。读者执行这段代码,当点击"执行动画"按钮时,图片开始移动,当一次动画执行结束之后,图片将向反方向运动,执行效果如图9.6所示。



        //帧动画
  1. public class firstActivity extends Activity {  
  2. /** Called when the activity is first created. */  
  3. @Override  
  4. public void onCreate(Bundle savedInstanceState) {/重载onCreate方法  
  5.     super.onCreate(savedInstanceState);  
  6.     setContentView(R.layout.main);  
  7.  
  8.     final ImageView image=(ImageView)findViewById(R.id.imageView1); //ImageView对象  
  9.     Button btn1=(Button)findViewById(R.id.button1);//按钮对象  
  10.     Button btn2=(Button)findViewById(R.id.button2);  
  11.     final AnimationDrawable ad=(AnimationDrawable)image.getBackground();//声明帧动画对象  
  12.     btn1.setOnClickListener(new View.OnClickListener() {//设置监听器  
  13.           
  14.         @Override  
  15.         public void onClick(View v) {  
  16.             // TODO Auto-generated method stub    
  17.             ad.setAlpha(100);//设置透明度  
  18.             ad.start();//开始动画  
  19.         }  
  20.     });  
  21.     btn2.setOnClickListener(new View.OnClickListener() {//设置监听器  
  22.           
  23.         @Override  
  24.         public void onClick(View v) {  
  25.             // TODO Auto-generated method stub  
  26.             ad.stop();//停止动画  
  27.         }  
  28.     });  
  29. }  
  30. }