Android遊戲開發學習筆記(一):tweened animation自定義動畫的實現

android中的自定義動畫有兩種模式:tweened animation和frame by frame。這裏介紹一種通過xml實現tweened animation的方法。

tweened animation(漸變動畫),有四種動畫類型:alpha(透明度)、scale(尺寸伸縮)、translate(位置變換)和rotate(圖形旋轉)。

首先,建立一個叫Animation的項目,在res下新建一個anim的目錄,在目錄中新建一個動畫設置的xml文件myanim.xml,代碼如下:

  1. <?xml version="1.0" encoding="utf-8"?> 
  2. <set xmlns:android="http://schemas.android.com/apk/res/android"> 
  3.     <alpha 
  4.         android:fromAlpha="0.1" 
  5.         android:toAlpha="1.0" 
  6.         android:duration="2000" 
  7.     />         <!-- 透明度的變換 --> 
  8.     <!-- fromAlpha屬性爲動畫起始時透明度,toAlpha屬性爲動畫結束時的透明度,  
  9.         duration爲動畫持續的時間 --> 
  10.     <scale 
  11.         android:interpolator="@android:anim/accelerate_decelerate_interpolator" 
  12.         android:fromXScale="0.0" 
  13.         android:toXScale="1.4" 
  14.         android:fromYScale="0.0" 
  15.         android:toYScale="1.4" 
  16.         android:pivotX="50%" 
  17.         android:pivotY="50%" 
  18.         android:fillAfter="false" 
  19.         android:duration="3000" 
  20.     />         <!-- 尺寸的變換 --> 
  21.     <!-- interpolator指定一個動畫的插入器,fromXScale屬性爲動畫起始時x座標上的  
  22.         伸縮尺寸,toCScale屬性爲動畫結束時x座標上的伸縮尺寸,fromYScale屬性爲動畫  
  23.         起始時y座標上的伸縮尺寸,toYScale屬性爲動畫結束時y座標上的伸縮尺寸,pivotX  
  24.         和pivotY設置動畫相對於自身的位置,fillAfter表示動畫的轉換在動畫結束後是否  
  25.         被應用 --> 
  26.     <translate 
  27.         android:fromXDelta="30" 
  28.         android:toXDelta="0" 
  29.         android:fromYDelta="30" 
  30.         android:toYDelta="50" 
  31.         android:duration="3000" 
  32.     />         <!-- 位置的變換 --> 
  33.     <!-- fromXDelta屬性爲動畫起始時x座標上的位置,toXDelta屬性爲動畫結束時x座標  
  34.     上的位置,fromYDelta屬性爲動畫起始時y座標上的位置,toYDelta屬性爲動畫結束時y  
  35.     座標上的位置 --> 
  36.     <rotate 
  37.         android:interpolator="@android:anim/accelerate_decelerate_interpolator" 
  38.         android:fromDegrees="0" 
  39.         android:toDegrees="+350" 
  40.         android:pivotX="50%" 
  41.         android:pivotY="50%" 
  42.         android:duration="3000" 
  43.     />         <!-- 旋轉變換 --> 
  44.     <!-- interpolator同樣爲一個動畫的插入器,fromDegrees屬性爲動畫起始時物件  
  45.     的角度,toDegrees屬性爲動畫結束時物件旋轉的角度 --> 
  46. </set> 

然後編寫佈局文件main.xml,代碼如下: 

  1. <?xml version="1.0" encoding="utf-8"?> 
  2. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
  3.     android:orientation="vertical" 
  4.     android:layout_width="fill_parent" 
  5.     android:layout_height="fill_parent" 
  6.     > 
  7. <ImageView 
  8.     android:id="@+id/myImageView"    
  9.     android:layout_width="fill_parent"   
  10.     android:layout_height="fill_parent"   
  11.     android:src="@drawable/preview" 
  12.     /> 
  13. </LinearLayout> 

其中,preview是放入res/drawable-mdpi的一張圖片,我們以此圖片作爲動畫演示。

最後,在MainActivity.java中編寫加載動畫的代碼:

  1. package game.test;  
  2.  
  3. import android.app.Activity;  
  4. import android.os.Bundle;  
  5. import android.view.animation.Animation;  
  6. import android.view.animation.AnimationUtils;  
  7. import android.widget.ImageView;  
  8.  
  9. public class MainActivity extends Activity {  
  10.     Animation myAnimation;  
  11.     ImageView myImageView;  
  12.     /** Called when the activity is first created. */ 
  13.     @Override 
  14.     public void onCreate(Bundle savedInstanceState) {  
  15.         super.onCreate(savedInstanceState);  
  16.         setContentView(R.layout.main);  
  17.         myAnimation=AnimationUtils.loadAnimation(this, R.anim.myanim);  
  18.         myImageView=(ImageView)this.findViewById(R.id.myImageView);  
  19.         myImageView.startAnimation(myAnimation);  
  20.     }  

 

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