1. android怎麼實現播放gif
以下是關於Android如何播放Gif動畫的內容,此內容在網上已經很多。在《瘋狂Android講義》中李剛也有所介紹和相關的源代碼。
1.android實現播放gif動畫,如何將gif動畫打散,將gif圖片打散本文介紹兩個工具,可以幫我們打散圖片:
gifsplitter2.0 下載地址: http://yunpan.cn/QbvwWIYVeWrPE
easygifanimator 下載地址: http://yunpan.cn/QbvwWd8Zy3BnF
第一個軟體分割圖片都是bmp圖片,圖片比較大,這里不推薦使用,本站推薦使用第二種軟體
以上兩種軟體的使用方法本站不在詳細贅述,本站詳細介紹安卓開發中使用的代碼
2.android實現播放gif動畫,假設我們已經得到了gif動畫被打散的圖片,分別為:x0.png、x1.png、x2.png、 x3.png,
2. 安卓中如何添加顯示gif動態圖片
大家都知道,Android開發模擬器為了節省內存,一般不支持直接顯示gif圖片,即使你強制設置了,也只會顯示圖片的第一幀。看到網上也有許多的方法,來實現此功能,可都比較的繁瑣,需要修改android源代碼來實現或者用gif解析器來實現。在此文章中,這里教大家一種比較簡潔的一個方法,你可以把這個類當做是一種工具類。用的時候,直接搬到程序裡面,更改下圖片的資源,就可以非常輕松的顯示gif圖片了。
步驟1:看一下這個工具類的實例代碼:MyGifView.java
import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Movie;
import android.util.AttributeSet;
import android.view.View;
public class MyGifView extends View{
private long movieStart;
private Movie movie;
//此處必須重寫該構造方法
public MyGifView(Context context,AttributeSet attributeSet) {
super(context,attributeSet);
//以文件流(InputStream)讀取進gif圖片資源
movie=Movie.decodeStream(getResources().openRawResource(R.drawable.keyboard));
}
@Override
protected void onDraw(Canvas canvas) {
long curTime=android.os.SystemClock.uptimeMillis();
//第一次播放
if (movieStart == 0) {
movieStart = curTime;
}
if (movie != null) {
int raction = movie.ration();
int relTime = (int) ((curTime-movieStart)%raction);
movie.setTime(relTime);
movie.draw(canvas, 0, 0);
//強制重繪
invalidate();
}
super.onDraw(canvas);
}
}
此工具類中,只做了2件事情。1,構造方法;2,重寫了onDraw()方法。大家以後用的話,只需拷貝此類到你的工程下即可起作用。
步驟2:布局文件代碼 activity_main.xml
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<TextView
android:text="====Gif圖片測試布局===="
android:layout_height="wrap_content"
android:layout_width="wrap_content"
/>
<com.example.showgifimage.MyGifView
android:id="@+id/iv"
android:layout_height="wrap_content"
android:layout_width="wrap_content"
android:layout_margin="20dp"
/>
</LinearLayout>
布局文件中,注意的是:標簽的設置 <com.example.showgifimage.MyGifView/>.
格式:包名+.類名
步驟3:主activity中調用
public class MainActivity extends Activity {
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
}
就這么輕松的把Gif圖片顯示了~~
3. Android開發怎樣顯示gif格式的圖片
android中現在沒有直接顯示gif的view,只能通過mediaplay來顯示,且還常常不能正常顯示出來,為此寫了這個gifview,其用法和imageview一樣
使用方法:
1-把GifView.jar加入你的項目。
2-在xml中配置GifView的基本屬性,GifView繼承自View類,和Button、ImageView一樣是一個UI控制項。如:
<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" />
3-在代碼中配置常用屬性:
// 從xml中得到GifView的句柄
gf1 = (GifView) findViewById(R.id.gif1);
// 設置Gif圖片源
gf1.setGifImage(R.drawable.gif1);
// 添加監聽器
gf1.setOnClickListener(this);
// 設置顯示的大小,拉伸或者壓縮
gf1.setShowDimension(300, 300);
// 設置載入方式:先載入後顯示、邊載入邊顯示、只顯示第一幀再顯示
gf1.setGifImageType(GifImageType.COVER);