導航:首頁 > APP軟體 > 安卓開發gif

安卓開發gif

發布時間:2023-02-09 10:03:25

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);

閱讀全文

與安卓開發gif相關的資料

熱點內容
小白編程什麼最好學 瀏覽:205
qq回頭看頭像 瀏覽:338
蘋果換屏要多長時間 瀏覽:283
如何用平板電腦學編程 瀏覽:424
格式工廠怎麼轉換swf文件 瀏覽:817
怎麼做一個試用網站 瀏覽:953
哪裡有信息不對稱的app 瀏覽:59
win10的gpeditmsc文件 瀏覽:451
4399游戲盒20版本 瀏覽:349
有什麼找工作比較靠譜的app嗎 瀏覽:921
如何給網站掛黑鏈 瀏覽:282
360斷網急救箱網路異常修復不了 瀏覽:889
身份證驗證的js文件 瀏覽:994
什麼人經常換微信名字 瀏覽:707
網站源碼挖掘 瀏覽:944
荔枝fm和app哪個播放量好 瀏覽:535
樂秀音頻在哪個文件里 瀏覽:486
以數據說話什麼意思 瀏覽:319
java中對象 瀏覽:794
酷狗網路機頂盒如何設置 瀏覽:408

友情鏈接