加入收藏 | 设为首页 | 会员中心 | 我要投稿 安卓应用网 (https://www.0791zz.com/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 编程开发 > Java > 正文

Android显示网络gif图片

发布时间:2020-05-24 21:40:12 所属栏目:Java 来源:互联网
导读:Android显示网络gif图片

下面是脚本之家 jb51.cc 通过网络收集整理的代码片段。

脚本之家小编现在分享给大家,也给大家做个参考。

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;

/**
*
*https://github.com/koral--/android-gif-drawable
*https://github.com/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.imgtn.bdimg.com/it/u=3026352344,1511311477&fm=21&gp=0.jpg";
Stringurl2="http://img5.imgtn.bdimg.com/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<File>(){
publicvoidonSuccess(ResponseInfo<File>responseInfo){
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;
}
}
<RelativeLayoutxmlns: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"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">

<TextViewandroid:text="@string/label_info"
android:id="@+id/info"
android:layout_width="wrap_content"
android:layout_height="wrap_content"/>

<!--gif控件-->
<pl.droidsonroids.gif.GifImageView
android:id="@+id/info_gif1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:scaleType="fitXY"
android:layout_below="@+id/info"
/>

<pl.droidsonroids.gif.GifImageView
android:id="@+id/info_gif2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:scaleType="fitXY"
android:layout_below="@+id/info_gif1"
/>
</RelativeLayout>

以上是脚本之家(jb51.cc)为你收集整理的全部代码内容,希望文章能够帮你解决所遇到的程序开发问题。

如果觉得脚本之家网站内容还不错,欢迎将脚本之家网站推荐给程序员好友。

(编辑:安卓应用网)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读