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

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

android加载网络gif图片不显示不出来的,android显示网络gif图片

發(fā)布時間:2023/12/20 编程问答 21 豆豆
生活随笔 收集整理的這篇文章主要介紹了 android加载网络gif图片不显示不出来的,android显示网络gif图片 小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.

這功能源自負責app中要加一個顯示gif廣告圖功能。

android自帶控件不支持gif圖片,網(wǎng)上很多通過擴展ImageView或View來實現(xiàn)支持gif圖片,但在android4.0后,需要關(guān)閉硬件加速功能才能使用,而且也容易出現(xiàn)內(nèi)存溢出問題。

網(wǎng)上找了兩個開源包來實現(xiàn)顯示Gif圖

android-gif-drawable 支持gif顯示的view控件

(如果訪問不了,可以此這里下載)

用jni實現(xiàn)的,編譯生成so庫后直接xml定義view,據(jù)說性能比較好,也能比較好避免內(nèi)存內(nèi)存溢出問題。

在Android Studio項目添加使用:

build.gradle文件dependencies添加內(nèi)容:

dependencies {

compile 'pl.droidsonroids.gif:android-gif-drawable:1.1.+' /* 添加gif控件庫引用 */

}

xUtils

包含了很多實用的android工具,這里主要用它下載文件

MainActivity.java

package?com.penngo.gif;

import?android.app.Activity;

import?android.content.Context;

import?android.os.Environment;

import?android.os.Bundle;

import?android.util.Log;

import?com.lidroid.xutils.HttpUtils;

import?com.lidroid.xutils.exception.HttpException;

import?com.lidroid.xutils.http.ResponseInfo;

import?com.lidroid.xutils.http.callback.RequestCallBack;

import?java.io.File;

import?pl.droidsonroids.gif.GifDrawable;

import?pl.droidsonroids.gif.GifImageView;

/**

*

*?https://github.com/koral--/android-gif-drawable

*?https://github.com/wyouflf/xUtils

*/

public?class?MainActivity?extends?Activity?{

private?final?String?tag?=?"MainActivity-->";

private?GifImageView?gif1;

private?GifImageView?gif2;

@Override

protected?void?onCreate(Bundle?savedInstanceState)?{

super.onCreate(savedInstanceState);

setContentView(R.layout.activity_main);

gif1?=?(GifImageView)this.findViewById(R.id.info_gif1);

gif2?=?(GifImageView)this.findViewById(R.id.info_gif2);

initGif();

}

private?void?initGif(){

String?url1?=?"http://img5.imgtn.bdimg.com/it/u=3026352344,1511311477&fm=21&gp=0.jpg";

String?url2?=?"http://img5.imgtn.bdimg.com/it/u=808161139,2623525132&fm=21&gp=0.jpg";

File?saveImgPath?=?this.getImageDir(this);

File?gifSavePath1?=?new?File(saveImgPath,?"gif1");

File?gifSavePath2?=?new?File(saveImgPath,?"gif2");

displayImage(url1,?gifSavePath1,?gif1);

displayImage(url2,?gifSavePath2,?gif2);

}

public?void?displayImage(String?url,?File?saveFile,?final?GifImageView?gifView){

HttpUtils?http?=?new?HttpUtils();

//?下載圖片

http.download(url,?saveFile.getAbsolutePath(),?new?RequestCallBack()?{

public?void?onSuccess(ResponseInfo?responseInfo)?{

try?{

Log.e(tag,?"onSuccess========"?+?responseInfo.result.getAbsolutePath());

GifDrawable?gifFrom?=?new?GifDrawable(?responseInfo.result.getAbsolutePath()?);

gifView.setImageDrawable(gifFrom);

}

catch(Exception?e){

Log.e(tag,?e.getMessage());

}

}

public?void?onFailure(HttpException?error,?String?msg)?{

Log.e(tag,?"onFailure========"?+?msg);

}

});

}

public?File?getFilesDir(Context?context,?String?tag){

if(isSdCardExist()?==?true){

return?context.getExternalFilesDir(tag);

}

else{

return?context.getFilesDir();

}

}

public?File?getImageDir(Context?context){

File?file?=?getFilesDir(context,?"images");

return?file;

}

public?boolean?isSdCardExist()?{

if?(Environment.getExternalStorageState().equals(

Environment.MEDIA_MOUNTED))?{

return?true;

}

return?false;

}

}

activity_main.xml

xmlns:tools="http://schemas.android.com/tools"?android:layout_width="match_parent"

android:layout_height="match_parent"?android:paddingLeft="@dimen/activity_horizontal_margin"

android:paddingRight="@dimen/activity_horizontal_margin"

android:paddingTop="@dimen/activity_vertical_margin"

android:paddingBottom="@dimen/activity_vertical_margin"?tools:context=".MainActivity">

android:id="@+id/info"

android:layout_width="wrap_content"

android:layout_height="wrap_content"?/>

android:id="@+id/info_gif1"

android:layout_width="wrap_content"

android:layout_height="wrap_content"

android:scaleType="fitXY"

android:layout_below="@+id/info"

/>

android:id="@+id/info_gif2"

android:layout_width="wrap_content"

android:layout_height="wrap_content"

android:scaleType="fitXY"

android:layout_below="@+id/info_gif1"

/>

運行效果:

總結(jié)

以上是生活随笔為你收集整理的android加载网络gif图片不显示不出来的,android显示网络gif图片的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網(wǎng)站內(nèi)容還不錯,歡迎將生活随笔推薦給好友。