ID #59435

animation-list旋转的地球以及Timer的使用

用到animation-list其实就是帧叠加 ,通常要注意两个问题

1.一般布局要用在frameLayout

2.它呢一般作为背景 然后在取出来

 

Java代码 
  1. public class EarthAnimationActivity extends Activity {  
  2.   
  3.     private static final String TAG = "EarthAnimationActivity";  
  4.   
  5.     protected static final int FORWARD = 0;  
  6.     protected static final int REVERSE = 1;  
  7.   
  8.     private Button earthButton;  
  9.   
  10.     private AnimationDrawable earthButtonAnimation;  
  11.   
  12.     protected int direction;  
  13.   
  14.       
  15.     /** Called when the activity is first created. */  
  16.     @Override  
  17.     public void onCreate(Bundle savedInstanceState) {  
  18.         super.onCreate(savedInstanceState);  
  19.         requestWindowFeature(Window.FEATURE_NO_TITLE);  
  20.         setContentView(R.layout.main);  
  21.   
  22.         earthButton = (Button) findViewById(R.id.earth_button);  
  23.         earthButtonAnimation = (AnimationDrawable) earthButton.getBackground();  
  24.         direction = FORWARD;  
  25.           
  26.         earthButton.setOnClickListener(new OnClickListener() {  
  27.   
  28.             @Override  
  29.             public void onClick(View v) {  
  30.                 if ( ! earthButtonAnimation.isRunning() ) {  
  31.                     earthButtonAnimation.start();  
  32.                     earthButton.setText(R.string.click_me_to_stop);  
  33.                 }  
  34.                 else {  
  35.                     earthButtonAnimation.stop();  
  36.                     int resId = R.anim.earth_animation_rev;  
  37.                     if ( direction == FORWARD ) {  
  38.                         direction = REVERSE;  
  39.                     }  
  40.                     else {  
  41.                         resId = R.anim.earth_animation;  
  42.                         direction = FORWARD;  
  43.                     }  
  44.                     earthButton.setBackgroundResource(resId);  
  45.                     earthButtonAnimation = (AnimationDrawable) earthButton.getBackground();  
  46.                     earthButton.setText(R.string.click_me_to_start);  
  47.                 }  
  48.             }  
  49.               
  50.         });  
  51.     }  
  52.   
  53.     /* (non-Javadoc) 
  54.      * @see Android.app.Activity#onResume() 
  55.      */  
  56.     @Override  
  57.     protected void onResume() {  
  58.         super.onResume();  
  59.   
  60.         (new Timer(false)).schedule(new AnimationTimer(earthButtonAnimation), 100);  
  61.     }  
  62.   
  63.     /* (non-Javadoc) 
  64.      * @see android.app.Activity#onPause() 
  65.      */  
  66.     @Override  
  67.     protected void onPause() {  
  68.         super.onPause();  
  69.         earthButtonAnimation.stop();  
  70.     }  
  71.   
  72.     private static class AnimationTimer extends TimerTask {  
  73.         AnimationDrawable animation;  
  74.           
  75.         public AnimationTimer(AnimationDrawable animation) {  
  76.             this.animation = animation;  
  77.         }  
  78.   
  79.         @Override  
  80.         public void run() {  
  81.             animation.start();  
  82.             this.cancel();  
  83.         }  
  84.           
  85.     }  
  86.       
  87. }  

 

Java代码 
  1. <?xml version="1.0" encoding="utf-8"?>  
  2. <FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"  
  3.     android:orientation="vertical" android:layout_width="fill_parent"  
  4.     android:layout_height="fill_parent" android:id="@+id/frame_layout"  
  5.     android:background="@drawable/space_background">  
  6.   
  7.     <Button android:id="@+id/earth_button" android:background="@anim/earth_animation"  
  8.         android:text="@string/click_me_to_stop" android:layout_gravity="center"  
  9.         android:textStyle="bold" android:textColor="#ff9900"  
  10.         android:layout_marginBottom="12dip" android:layout_marginRight="12dip"  
  11.         android:layout_marginTop="12dip" android:maxHeight="296dip"  
  12.         android:maxWidth= "296dip" android:layout_height="wrap_content"  
  13.         android:layout_width="wrap_content" android:width="296dip"  
  14.         android:height="296dip" android:layout_marginLeft="-10dip"  
  15.         android:textSize="24dip"></Button>  
  16.   
  17. </FrameLayout>  

 上面主vxml

下面是地球的正反旋转

Java代码 
  1. <?xml version="1.0" encoding="UTF-8"?>  
  2.     <!-- Animation frames for earth -->  
  3.     <!--  android:id="@+id/earth_animation" -->  
  4. <animation-list xmlns:android="http://schemas.android.com/apk/res/android"  
  5.     android:oneshot="false" android:visible="true">  
  6.     <item android:drawable="@drawable/earth0" android:duration="150" />  
  7.     <item android:drawable="@drawable/earth1" android:duration="150" />  
  8.     <item android:drawable="@drawable/earth2" android:duration="150" />  
  9.     <item android:drawable="@drawable/earth3" android:duration="150" />  
  10.     <item android:drawable="@drawable/earth4" android:duration="150" />  
  11.     <item android:drawable="@drawable/earth5" android:duration="150" />  
  12.     <item android:drawable="@drawable/earth6" android:duration="150" />  
  13.     <item android:drawable="@drawable/earth7" android:duration="150" />  
  14.     <item android:drawable="@drawable/earth8" android:duration="150" />  
  15.     <item android:drawable="@drawable/earth9" android:duration="150" />  
  16. </animation-list>  

 

Java代码 
  1. <?xml version="1.0" encoding="UTF-8"?>  
  2.     <!-- Animation frames for earth -->  
  3.     <!--  android:id="@+id/earth_animation_rev" -->  
  4. <animation-list xmlns:android="http://schemas.android.com/apk/res/android"  
  5.     android:oneshot="false" android:visible="true">  
  6.     <item android:drawable="@drawable/earth9" android:duration="150" />  
  7.     <item android:drawable="@drawable/earth8" android:duration="150" />  
  8.     <item android:drawable="@drawable/earth7" android:duration="150" />  
  9.     <item android:drawable="@drawable/earth6" android:duration="150" />  
  10.     <item android:drawable="@drawable/earth5" android:duration="150" />  
  11.     <item android:drawable="@drawable/earth4" android:duration="150" />  
  12.     <item android:drawable="@drawable/earth3" android:duration="150" />  
  13.     <item android:drawable="@drawable/earth2" android:duration="150" />  
  14.     <item android:drawable="@drawable/earth1" android:duration="150" />  
  15.     <item android:drawable="@drawable/earth0" android:duration="150" />  
  16. </animation-list>