android.graphics.Movie

如何在Android中顯示GIF動畫,有很多方法,比如可以使用J2ME平臺上那個解碼工具類,純java的,拿來即可。
但是其實Android還是爲我們提供了一個更爲方便的工具:android.graphics.Movie。

參考例子在ApiDemos中的BitmapDecode中。

下面我只是簡單地用它來實現一個自己的GIFView,以方便在各種需要使用GIF動畫的場合使用。

爲了簡單,我讓GIFView extends ImageView罷了。它在佈局中的描述如下:

  1. <cn.sharetop.android.view.GIFView  
  2.     android:id="@+id/gif"  
  3.     android:layout_gravity="center_horizontal"  
  4.     android:layout_width="278px"  
  5.     android:layout_height="183px"  
  6.     android:scaleType="fitXY"  
  7.     app:gif="@drawable/a"  
  8.     android:src="@drawable/a"  
  9.     />  


與ImageView唯一的區別在於我加了一個gif屬性,與src屬性的值是一樣的。不過它們需要同時存在,不可省略其中之一(後面我會說明爲什麼)。

注意因爲gif屬性,所以別忘了那個attr.xml中也要加上:
  1. <resources>  
  2.     <declare-styleable name="GIFView">  
  3.         <attr name="gif" format="reference"  />  
  4.     </declare-styleable>  
  5. </resources>  



然後是代碼,沒幾行的:

  1. public class GIFView extends ImageView {  
  2.     private static final String TAG="GIFView";  
  3.      
  4.     private Movie mMovie;     
  5.     private long mMovieStart;  
  6.      
  7.      
  8.     //此處省略幾個構造函數  
  9.     //......  
  10.     //主要的構造函數  
  11.     public GIFView(Context context, AttributeSet attrs, int defStyle) {  
  12.         super(context, attrs, defStyle);  
  13.         // TODO Auto-generated constructor stub  
  14.          
  15.         mMovie=null;  
  16.         mMovieStart=0;  
  17.          
  18.         //從描述文件中讀出gif的值,創建出Movie實例  
  19.         TypedArray a = context.obtainStyledAttributes(attrs,  
  20.                 R.styleable.GIFView, defStyle, 0);  
  21.          
  22.         int srcID=a.getResourceId(R.styleable.GIFView_gif, 0);  
  23.         if(srcID>0){  
  24.             InputStream is = context.getResources().openRawResource(srcID);  
  25.             mMovie = Movie.decodeStream(is);  
  26.         }  
  27.          
  28.         a.recycle();  
  29.     }  
  30.     //主要的工作是重載onDraw  
  31.     @Override  
  32.     protected void onDraw(Canvas canvas) {  
  33.         // TODO Auto-generated method stub  
  34.         //super.onDraw(canvas);  
  35.          
  36.         //當前時間  
  37.         long now = android.os.SystemClock.uptimeMillis();  
  38.         //如果第一幀,記錄起始時間  
  39.         if (mMovieStart == 0) {   // first time  
  40.               mMovieStart = now;  
  41.         }  
  42.         if (mMovie != null) {  
  43.                   //取出動畫的時長  
  44.             int dur = mMovie.duration();  
  45.                   if (dur == 0) {  
  46.                       dur = 1000;  
  47.                   }  
  48.                   //算出需要顯示第幾幀  
  49.             int relTime = (int)((now - mMovieStart) % dur);  
  50.            
  51.                   //Log.d(TAG,"---onDraw..."+mMovie.toString()+",,,,"+relTime);  
  52.              //設置要顯示的幀,繪製即可  
  53.                   mMovie.setTime(relTime);  
  54.             mMovie.draw(canvas,0,0);  
  55.                   invalidate();  
  56.         }         
  57.     }  
  58.          
  59. }  



代碼中已有註釋,就不多說了。我的理解是Movie其實管理着GIF動畫中的多個幀,只需要通過 setTime() 一下就可以讓它在draw()的時候繪出相應的那幀圖像。
通過當前時間與duration之間的換算關係,是很容易實現GIF動起來的效果。


最後,說一下爲什麼src與gif要同時存在了,因爲我這個GIFView很簡單,沒有自己去onMeasure,所以要藉助src讓ImageView去計算它的尺寸和佈局之類的事情。
只是在onDraw的時候,不顯示src而已。

如果感興趣的同學可以自己完善這個GIFView,比如以下兩點:
1. 只需要一個gif屬性,不要src了,或者直接使用src屬性?
2. 如果在xml中沒有指定gif/src的值,增加一些方法讓用戶可以通過代碼設置gif和src的值

[補充]

剛纔又覺得這段代碼有修正的必要:

1. 關於如何直接使用src這個屬性,仍是修改attr.xml中,這樣即可:

  1. <resources>  
  2.     <declare-styleable name="GIFView">  
  3.         <attr name="android:src" />  
  4.     </declare-styleable>  
  5. </resources>  

然後在main.xml中就不再需要gif這個屬性,直接用src就可以了。


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