100字范文,内容丰富有趣,生活中的好帮手!
100字范文 > android加载网络gif图片不显示不出来的 android显示网络gif图片

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

时间:2023-05-31 02:44:21

相关推荐

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

这功能源自负责app中要加一个显示gif广告图功能。

android自带控件不支持gif图片,网上很多通过扩展ImageView或View来实现支持gif图片,但在android4.0后,需要关闭硬件加速功能才能使用,而且也容易出现内存溢出问题。

网上找了两个开源包来实现显示Gif图

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

(如果访问不了,可以此这里下载)

用jni实现的,编译生成so库后直接xml定义view,据说性能比较好,也能比较好避免内存内存溢出问题。

在Android Studio项目添加使用:

build.gradle文件dependencies添加内容:

dependencies {

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

}

xUtils

包含了很多实用的android工具,这里主要用它下载文件

MainActivity.java

packagecom.penngo.gif;

importandroid.app.Activity;

importandroid.content.Context;

importandroid.os.Environment;

importandroid.os.Bundle;

importandroid.util.Log;

importcom.lidroid.xutils.HttpUtils;

importcom.lidroid.xutils.exception.HttpException;

importcom.lidroid.xutils.http.ResponseInfo;

importcom.lidroid.xutils.http.callback.RequestCallBack;

importjava.io.File;

importpl.droidsonroids.gif.GifDrawable;

importpl.droidsonroids.gif.GifImageView;

/**

*

*/koral--/android-gif-drawable

*/wyouflf/xUtils

*/

publicclassMainActivityextendsActivity{

privatefinalStringtag="MainActivity-->";

privateGifImageViewgif1;

privateGifImageViewgif2;

@Override

protectedvoidonCreate(BundlesavedInstanceState){

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

}

privatevoidinitGif(){

Stringurl1="http://img5./it/u=3026352344,1511311477&fm=21&gp=0.jpg";

Stringurl2="http://img5./it/u=808161139,2623525132&fm=21&gp=0.jpg";

FilesaveImgPath=this.getImageDir(this);

FilegifSavePath1=newFile(saveImgPath,"gif1");

FilegifSavePath2=newFile(saveImgPath,"gif2");

displayImage(url1,gifSavePath1,gif1);

displayImage(url2,gifSavePath2,gif2);

}

publicvoiddisplayImage(Stringurl,FilesaveFile,finalGifImageViewgifView){

HttpUtilshttp=newHttpUtils();

//下载图片

http.download(url,saveFile.getAbsolutePath(),newRequestCallBack(){

publicvoidonSuccess(ResponseInforesponseInfo){

try{

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

GifDrawablegifFrom=newGifDrawable(responseInfo.result.getAbsolutePath());

gifView.setImageDrawable(gifFrom);

}

catch(Exceptione){

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

}

}

publicvoidonFailure(HttpExceptionerror,Stringmsg){

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

}

});

}

publicFilegetFilesDir(Contextcontext,Stringtag){

if(isSdCardExist()==true){

returncontext.getExternalFilesDir(tag);

}

else{

returncontext.getFilesDir();

}

}

publicFilegetImageDir(Contextcontext){

Filefile=getFilesDir(context,"images");

returnfile;

}

publicbooleanisSdCardExist(){

if(Environment.getExternalStorageState().equals(

Environment.MEDIA_MOUNTED)){

returntrue;

}

returnfalse;

}

}

activity_main.xml

xmlns:tools="/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"

/>

运行效果:

本内容不代表本网观点和政治立场,如有侵犯你的权益请联系我们处理。
网友评论
网友评论仅供其表达个人看法,并不表明网站立场。