動畫效果程式設計基礎--AnimationAndroid 
   Android中,分別可以在xml中定義Animation,也可以在程式碼中定義 

動畫類型 

Androidanimation由四種類型組成 

XML 
alpha 
漸變透明度動畫效果 
scale 
漸變尺寸伸縮動畫效果 
translate 
畫面轉換位置移動動畫效果 
rotate 
畫面轉移旋轉動畫效果 

代碼中 
AlphaAnimation 
漸變透明度動畫效果 
ScaleAnimation 
漸變尺寸伸縮動畫效果 
TranslateAnimation 
畫面轉換位置移動動畫效果 
RotateAnimation 
畫面轉移旋轉動畫效果 

Android動畫模式 

Animation主要有兩種動畫模式: 

一種是tweened animation(漸變動畫
alpha 
AlphaAnimation 
scale 
ScaleAnimation 


一種是frame by frame(畫面轉換動畫
translate 
TranslateAnimation 
rotate 
RotateAnimation 


如何在XML文件中定義動畫 
 打開Eclipse,新建Android工程 
 在res目錄中新建anim資料夾 
 在anim目錄中新建一個myanim.xml(注意檔案名小寫
 加入XML的動畫代碼 
<?xml version="1.0" encoding="utf-8"?> 
<set xmlns:android="http://schemas.android.com/apk/res/android"> 
  <alpha/> 
  <scale/> 
  <translate/> 
  <rotate/> 
</set> 
每個元素表示不同的動畫效果 


Android動畫解析--XML 
<alpha> 
<?xml version="1.0" encoding="utf-8"?> 
<set xmlns:android="http://schemas.android.com/apk/res/android" > 
<alpha 
android:fromAlpha="0.1" 
android:toAlpha="1.0" 
android:duration="3000" 

/> 
 
</set> 


<scale> 
<?xml version="1.0" encoding="utf-8"?> 
<set xmlns:android="http://schemas.android.com/apk/res/android"> 
   <scale 
          android:interpolator= 
                    "@android:anim/accelerate_decelerate_interpolator" 
          android:fromXScale="0.0" 
          android:toXScale="1.4" 
          android:fromYScale="0.0" 
          android:toYScale="1.4" 
          android:pivotX="50%" 
          android:pivotY="50%" 
          android:fillAfter="false" 
          android:startOffset=“700” 
          android:duration="700" /> 
</set> 
 


<translate> 

<?xml version="1.0" encoding="utf-8"?> 
<set xmlns:android="http://schemas.android.com/apk/res/android"> 
<translate 
android:fromXDelta="30" 
android:toXDelta="-80" 
android:fromYDelta="30" 
android:toYDelta="300" 
android:duration="2000" 
/> 
 
</set> 



<rotate> 

<?xml version="1.0" encoding="utf-8"?> 
<set xmlns:android="http://schemas.android.com/apk/res/android"> 
<rotate 
        android:interpolator="@android:anim/accelerate_decelerate_interpolator" 
        android:fromDegrees="0" 
        android:toDegrees="+350"        
        android:pivotX="50%" 
        android:pivotY="50%"    
        android:duration="3000" /> 
 
</set> 


在編碼中如何使用如何使用XML中的動畫效果 

在代碼中使用這個方法得到Animation實例 
public static Animation loadAnimation (Context context, int id) 
//第一個參數Context為程式的上下文   
//第二個參數id為動畫XML檔的引用 

//例子: 
myAnimation= AnimationUtils.loadAnimation(this,R.anim.my_action); 
//使用AnimationUtils類的靜態方法loadAnimation()來載入XML中的動畫XML 


如何在Java代碼中定義動畫 
//在代碼中定義 動畫實例物件 
private Animation myAnimation_Alpha; 
private Animation myAnimation_Scale; 
private Animation myAnimation_Translate; 
private Animation myAnimation_Rotate; 
   
//根據各自的構造方法來初始化一個實例物件 
myAnimation_Alpha=new AlphaAnimation(0.1f, 1.0f); 

myAnimation_Scale =new ScaleAnimation(0.0f, 1.4f, 0.0f, 1.4f, 
             Animation.RELATIVE_TO_SELF, 0.5f, Animation.RELATIVE_TO_SELF, 0.5f); 

myAnimation_Translate=new TranslateAnimation(30.0f, -80.0f, 30.0f, 300.0f); 

myAnimation_Rotate=new RotateAnimation(0.0f, +350.0f,               Animation.RELATIVE_TO_SELF,0.5f,Animation.RELATIVE_TO_SELF, 0.5f); 

-------------------------------------------------------------------- 

Android動畫解析 

AlphaAnimation 
 AlphaAnimation類物件定義 
private AlphaAnimation myAnimation_Alpha; 
 AlphaAnimation類物件構造 
AlphaAnimation(float fromAlpha, float toAlpha) 
//第一個參數fromAlpha 動畫開始時候透明度 
//第二個參數toAlpha 動畫結束時候透明度 
myAnimation_Alpha=new AlphaAnimation(0.1f, 1.0f); 
//說明
//                0.0表示完全透明 
//                1.0表示完全不透明 
 設置動畫持續時間 
myAnimation_Alpha.setDuration(5000); 
//設置時間持續時間為 5000毫秒 


ScaleAnimation 

ScaleAnimation類物件定義 
private AlphaAnimation myAnimation_Scale; 
 ScaleAnimation類物件構造 
ScaleAnimation(float fromX, float toX, float fromY, float toY, 
           int pivotXType, float pivotXValue, int pivotYType, float pivotYValue) 
//第一個參數fromX為動畫起始時 X座標上的伸縮尺寸   
//第二個參數toX為動畫結束時 X座標上的伸縮尺寸    
//第三個參數fromY為動畫起始時Y座標上的伸縮尺寸   
//第四個參數toY為動畫結束時Y座標上的伸縮尺寸 
/*說明
                    以上四種屬性值   
                    0.0表示收縮到沒有 
                    1.0表示正常無伸縮    
                    值小於1.0表示收縮 
                    值大於1.0表示放大 
*/ 
//第五個參數pivotXType為動畫在X軸相對於物件位置類型 
//第六個參數pivotXValue為動畫相對於物件的X座標的開始位置 
//第七個參數pivotXType為動畫在Y軸相對於物件位置類型  
//第八個參數pivotYValue為動畫相對於物件的Y座標的開始位置 
myAnimation_Scale =new ScaleAnimation(0.0f, 1.4f, 0.0f, 1.4f, 
             Animation.RELATIVE_TO_SELF, 0.5f, Animation.RELATIVE_TO_SELF, 0.5f); 
 設置動畫持續時間 
myAnimation_Scale.setDuration(700); 
//設置時間持續時間為 700毫秒 


TranslateAnimation 
 TranslateAnimation類物件定義 
private AlphaAnimation myAnimation_Translate; 
 TranslateAnimation類物件構造 
TranslateAnimation(float fromXDelta, float toXDelta, 
                       float fromYDelta, float toYDelta) 
//第一個參數fromXDelta為動畫起始時 X座標上的移動位置   
//第二個參數toXDelta為動畫結束時 X座標上的移動位置     
//第三個參數fromYDelta為動畫起始時Y座標上的移動位置    
//第四個參數toYDelta為動畫結束時Y座標上的移動位置 
 設置動畫持續時間 
myAnimation_Translate.setDuration(2000); 
//設置時間持續時間為 2000毫秒 

RotateAnimation 
 RotateAnimation類物件定義 
private AlphaAnimation myAnimation_Rotate; 
 RotateAnimation類物件構造 
RotateAnimation(float fromDegrees, float toDegrees, 
            int pivotXType, float pivotXValue, int pivotYType, float pivotYValue) 
//第一個參數fromDegrees為動畫起始時的旋轉角度   
//第二個參數toDegrees為動畫旋轉到的角度  
//第三個參數pivotXType為動畫在X軸相對於物件位置類型 
//第四個參數pivotXValue為動畫相對於物件的X座標的開始位置 
//第五個參數pivotXType為動畫在Y軸相對於物件位置類型  
//第六個參數pivotYValue為動畫相對於物件的Y座標的開始位置 
myAnimation_Rotate=new RotateAnimation(0.0f, +350.0f,             Animation.RELATIVE_TO_SELF,0.5f,Animation.RELATIVE_TO_SELF, 0.5f) 
 設置動畫持續時間 
myAnimation_Rotate.setDuration(3000); 
//設置時間持續時間為 3000毫秒 


--------------------------------------------------------------------- 
下面的小例子是利用RotateAnimation簡單展示一下兩種方法的用法,對於其他動畫,如ScaleAnimationAlphaAnimation,原理是一樣的。 
方法一:在xml中定義動畫: 

Xml代碼  

  1. <?xml version= encoding="utf-8"?>    
  2. <set xmlns:android=>   
  3.   
  4. <rotate            
  5.       android:interpolator="@android:anim/accelerate_decelerate_interpolator"    
  6.         android:fromDegrees="0"     
  7.         android:toDegrees="+360"    
  8.         android:duration="3000" />    
  9.             

10.  <!-- rotate 旋轉動畫效果    

  1.        屬性:interpolator 指定一個動畫的插入器,用來控制動畫的速度變化    
  2.         fromDegrees 屬性為動畫起始時物件的角度        
  3.         toDegrees   屬性為動畫結束時物件旋轉的角度,正代表順時針    
  4.         duration  屬性為動畫持續時間,以毫秒為單位    

15.  -->  

16.  lt;/set>     



使用動畫的Java代碼,程式的效果是點擊按鈕,TextView旋轉一周: 


 package com.ray.animation;  
   
 import android.app.Activity;  
 import android.os.Bundle;  
 import android.view.View;  
 import android.view.View.OnClickListener;  
 import android.view.animation.Animation;  
 import android.view.animation.AnimationUtils;  
 import android.widget.Button;  
 import android.widget.TextView;  
   
 public class TestAnimation extends Activity implements OnClickListener{  
     public void onCreate(Bundle savedInstanceState) {  
         super.onCreate(savedInstanceState);  
         setContentView(R.layout.main);  
         Button btn = (Button)findViewById(R.id.Button01);  
         btn.setOnClickListener(this);       
     }  
   
     @Override  
     public void onClick(View v) {  
         Animation anim = AnimationUtils.loadAnimation(this, R.anim.my_rotate_action);  
         findViewById(R.id.TextView01).startAnimation(anim);  
     }  
 }


方法二:直接在代碼中定義動畫(效果跟方法一類似): 


 package com.ray.animation;  
   
 import android.app.Activity;  
 import android.os.Bundle;  
 import android.view.View;  
 import android.view.View.OnClickListener;  
 import android.view.animation.AccelerateDecelerateInterpolator;  
 import android.view.animation.Animation;  
 import android.view.animation.RotateAnimation;  
 import android.widget.Button;  
   
 public class TestAnimation extends Activity implements OnClickListener{  
   
     public void onCreate(Bundle savedInstanceState) {  
         super.onCreate(savedInstanceState);  
         setContentView(R.layout.main);  
         Button btn = (Button)findViewById(R.id.Button);  
         btn.setOnClickListener(this);       
     }  
   
     public void onClick(View v) {  
         Animation anim = null;  
         anim = new RotateAnimation(0.0f,+360.0f);  
         anim.setInterpolator(new AccelerateDecelerateInterpolator());  
         anim.setDuration(3000);  
         findViewById(R.id.TextView01).startAnimation(anim);   
    }  
 } 



轉貼自:http://sunnyday55555.javaeye.com/blog/481685



arrow
arrow
    全站熱搜

    狼翔月影 發表在 痞客邦 留言(0) 人氣()