日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 运维知识 > Android >内容正文

Android

android如何添加gif,Android加载Gif和ImageView的通用解决方案:android-gif-drawable(1)...

發布時間:2025/5/22 Android 17 豆豆
生活随笔 收集整理的這篇文章主要介紹了 android如何添加gif,Android加载Gif和ImageView的通用解决方案:android-gif-drawable(1)... 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.



Android加載Gif和ImageView的通用解決方案:android-gif-drawable(1)

Android自己的ImageView或者View不能直接加載運行Gif圖片,如果要在一個Android的ImageView中加載一個gif圖片資源,則需要通過其他途徑實現,我之前寫了一些關于如何在Android中加載gif圖片的文章:

文章1,《基于開源框架Glide加載Gif資源圖到Android ImageView中》鏈接地址:http://blog.csdn.net/zhangphil/article/details/45561983

文章2,《Android加載Gif圖片的一般方法:Movie實現》鏈接地址:http://blog.csdn.net/zhangphil/article/details/50441944

文章1,2雖然解決了如何加載gif圖片資源的問題,但仍存在一個問題:需要事先知道該資源是何圖片資源,并假定該資源就是gif圖片資源。

這在有些需求開發中或許不恰當,因為有些時候,僅僅一個View容器,需要它呈現和裝載多種圖片類型不管是gif或者png,而不需要事先知道它是何種圖片類型。

android-gif-drawable就是這樣一種通用的gif加載解決方案。android-gif-drawable在github上的官方主頁地址:

針對Eclipse,android-gif-drawable提供了專門的包(包里含有需要的庫資源和demo代碼)。頁面鏈接地址:https://github.com/koral--/android-gif-drawable-eclipse-sample ,將該代碼整體全部下載,下載后是一個完整的eclipse項目,編譯器如果報錯則可能需要導入相關的support-v4包,已經把jdk切換到1.7及以上。然后就可以直接運行這個demo項目工程了。

android-gif-drawable使用簡單,把GifImageView當作普通的View寫進布局文件中,然后加載gif動圖資源或者普通的png、jpeg圖資源裝載進去即可。

簡單給出一個用GifImageView加載gif動圖以及加載普通的png圖片的例子。

先寫布局文件activity_main.xml:

xmlns:tools="http://schemas.android.com/tools"

android:layout_width="match_parent"

android:layout_height="match_parent"

android:background="@android:color/white"

android:orientation="vertical" >

android:id="@+id/gif1"

android:layout_width="match_parent"

android:layout_height="wrap_content" />

android:id="@+id/gif2"

android:layout_width="match_parent"

android:layout_height="wrap_content" />

上層Java代碼:

package zhangphil.gif;

import android.app.Activity;

import android.os.Bundle;

import pl.droidsonroids.gif.GifDrawable;

import pl.droidsonroids.gif.GifImageView;

public class MainActivity extends Activity {

@Override

protected void onCreate(Bundle savedInstanceState) {

super.onCreate(savedInstanceState);

setContentView(R.layout.activity_main);

GifImageView gifImageView1 = (GifImageView) findViewById(R.id.gif1);

GifImageView gifImageView2 = (GifImageView) findViewById(R.id.gif2);

try {

// 如果加載的是gif動圖,第一步需要先將gif動圖資源轉化為GifDrawable

// 將gif圖資源轉化為GifDrawable

GifDrawable gifDrawable = new GifDrawable(getResources(), R.drawable.loading);

// gif1加載一個動態圖gif

gifImageView1.setImageDrawable(gifDrawable);

// 如果是普通的圖片資源,就像Android的ImageView set圖片資源一樣簡單設置進去即可。

// gif2加載一個普通的圖片(如png,bmp,jpeg等等)

gifImageView2.setImageResource(R.drawable.ic_launcher);

} catch (Exception e) {

e.printStackTrace();

}

}

}

運行結果如圖所示(上面是一個不停旋轉的加載gif動圖,下面是一個普通的png圖片,即小機器人):

由于https://github.com/koral--/android-gif-drawable-eclipse-sample這個頁面下載到的代碼針對eclipse推出的項目包中是一個混在一起的項目,實際開發過程中,最好的做法是把依賴的核心代碼及資源分離出來,這樣達到復用和工程代碼結構清晰,我把android-gif-drawable for Eclipse的關鍵代碼抽取分離出來,單獨作成一個lib,需要的時候直接導入然后引用即可。

android-gif-drawable for Eclipse在github上的lib包頁面地址:

https://github.com/zhangphil/android-gif-drawable-for-Eclipse

使用時候,從這個頁面下載完整的lib項目,作為lib導入到eclipse里面,在自己需要的項目中加以引用即可。

附上loading.gif動圖:

總結

以上是生活随笔為你收集整理的android如何添加gif,Android加载Gif和ImageView的通用解决方案:android-gif-drawable(1)...的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。