android動畫編

四月份來了,我也要加緊學習了,下面的動畫類型

 

  1. package com.smart;  
  2.  
  3. import android.app.Activity;  
  4. import android.os.Bundle;  
  5. import android.view.View;  
  6. import android.view.animation.Animation;  
  7. import android.view.animation.AnimationUtils;  
  8. import android.view.animation.TranslateAnimation;  
  9. import android.widget.AdapterView;  
  10. import android.widget.ArrayAdapter;  
  11. import android.widget.Spinner;  
  12.  
  13. public class Main extends Activity implements AdapterView.OnItemSelectedListener  
  14. {  
  15.     private static final String[] INTERPOLATORS={  
  16.         "加快""減速""加快/減速""預料",  
  17.         "過沖""預計/過沖""彈跳"   
  18.     };//類型  
  19.       
  20.     @Override 
  21.     public void onCreate(Bundle savedInstanceState) {  
  22.         super.onCreate(savedInstanceState);  
  23.         setContentView(R.layout.main);  
  24.         Spinner s=(Spinner)findViewById(R.id.spinner);  
  25.         ArrayAdapter<String> adapter=new ArrayAdapter<String>(this,android.R.layout.simple_spinner_item,INTERPOLATORS);  
  26.         adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);  
  27.         s.setAdapter(adapter);//得到控件  
  28.         s.setOnItemSelectedListener(this);  
  29.           
  30.           
  31.     }  
  32.  
  33.       
  34.     @Override 
  35.     public void onItemSelected(AdapterView<?> parent, View v, int position,  
  36.             long id) {  
  37.         final View target=(View)findViewById(R.id.target);  
  38.         final View targetParent=(View)target.getParent();//得到動畫  
  39.         Animation animation= new TranslateAnimation(0.0f, targetParent  
  40.                 .getWidth()  
  41.                 - target.getWidth()  
  42.                 - targetParent.getPaddingLeft()  
  43.                 - targetParent.getPaddingRight(), 0.0f, 0.0f);  
  44.         animation.setDuration(1000);  
  45.         animation.setStartOffset(300);  
  46.         animation.setRepeatMode(Animation.RESTART);  
  47.         animation.setRepeatCount(Animation.INFINITE);  
  48.         //選擇動來類型  
  49.         switch (position) {  
  50.         case 0:  
  51.             animation.setInterpolator(AnimationUtils.loadInterpolator(this, android.R.anim.accelerate_interpolator));  
  52.               
  53.             break;  
  54.         case 1:  
  55.             animation.setInterpolator(AnimationUtils.loadInterpolator(this, android.R.anim.decelerate_interpolator));  
  56.               
  57.             break;  
  58.         case 2:  
  59.             animation.setInterpolator(AnimationUtils.loadInterpolator(this, android.R.anim.accelerate_decelerate_interpolator));  
  60.               
  61.             break;  
  62.         case 3:  
  63.             animation.setInterpolator(AnimationUtils.loadInterpolator(this, android.R.anim.anticipate_interpolator));  
  64.               
  65.             break;  
  66.         case 4:  
  67.             animation.setInterpolator(AnimationUtils.loadInterpolator(this, android.R.anim.overshoot_interpolator));  
  68.               
  69.             break;  
  70.         case 5:  
  71.             animation.setInterpolator(AnimationUtils.loadInterpolator(this, android.R.anim.anticipate_overshoot_interpolator));  
  72.               
  73.             break;  
  74.         case 6:  
  75.             animation.setInterpolator(AnimationUtils.loadInterpolator(this, android.R.anim.bounce_interpolator));  
  76.               
  77.             break;  
  78.               
  79.         }  
  80.         target.startAnimation(animation);  
  81.           
  82.           
  83.     }  
  84.  
  85.       
  86.       
  87.     @Override 
  88.     public void onNothingSelected(AdapterView<?> arg0) {  
  89.           
  90.           
  91.           
  92.     }  
  93. }  
  94.  
  95. <?xml version="1.0" encoding="utf-8"?>  
  96. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
  97.     android:orientation="vertical" 
  98.     android:padding="10dip" 
  99.     android:layout_width="fill_parent" 
  100.     android:layout_height="wrap_content" 
  101.     android:clipToPadding="false" 
  102.     >  
  103. <TextView    
  104. android:id="@+id/target" 
  105.     android:layout_width="wrap_content"   
  106.     android:layout_height="wrap_content"   
  107.     android:textSize="16sp" 
  108.     android:text="朋友,看看我,跑得多快!" 
  109.     />  
  110.       
  111.     <TextView    
  112.     android:layout_width="fill_parent"   
  113.     android:layout_height="wrap_content"   
  114.     android:textSize="15dip" 
  115.     android:layout_marginBottom="5dip" 
  116.     android:text="請選擇動畫類型" 
  117.     />  
  118.       
  119.     <Spinner    
  120.     android:id="@+id/spinner" 
  121.     android:layout_width="fill_parent"   
  122.     android:layout_height="wrap_content"   
  123.     />  
  124. </LinearLayout>  

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章