在網上找了好多關于播放gif的資料。有一個是用流方式讀入資料,強制重新整理的,我試了,效果不怎麼理想,有花紋。後來找到這篇文章Android開發第三方類庫。決定采用android-gif-drawable方式,項目位址有demo源程式,可以參考下。
步驟:
1. 下載下傳jar包:https://github.com/koral–/android-gif-drawable/
2. 将jar包拖放到libs目錄。
3. 在JAVA檔案中導入該包。
import com.ant.liao.GifView;
import com.ant.liao.GifView.GifImageType;
布局檔案gif.xml:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="horizontal" android:layout_width="fill_parent" android:layout_height="fill_parent">
<com.ant.liao.GifView
android:id="@+id/gif1"
android:layout_height="wrap_content" android:layout_width="wrap_content"
android:paddingRight="14px" android:enabled="false" />
<TextView android:id="@+id/tsxt" android:layout_height="wrap_content" android:layout_width="wrap_content"
android:paddingRight="4px" android:enabled="false"
android:text="click the Angel" />
<com.ant.liao.GifView
android:id="@+id/gif2"
android:layout_height="wrap_content" android:layout_width="wrap_content"
android:paddingTop="4px" android:paddingLeft="14px" android:enabled="false" />
</LinearLayout>
package com.TestGif;
import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import com.ant.liao.GifView;
import com.ant.liao.GifView.GifImageType;
public class MainActivity extends Activity implements OnClickListener{
private GifView gf1;
private GifView gf2;
private boolean f = true;
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.gif);
gf1 = (GifView)findViewById(R.id.gif1);
gf1.setGifImage(R.drawable.gif1);
gf1.setOnClickListener(this);
gf2 = (GifView)findViewById(R.id.gif2);
gf2.setGifImageType(GifImageType.COVER);// 設定加載方式:先加載後顯示、邊加載邊顯示、隻顯示第一幀再顯示 gf2.setShowDimension(300, 300); // 設定顯示的大小,拉伸或者壓縮
gf2.setGifImage(R.drawable.a);
}
public void onClick(View v) {
if(f){
gf1.showCover();
f = false;
}else{
gf1.showAnimation();
f = true;
}
}
}