Android顯示網絡gif圖片
這功能源自負責app中要加一個顯示gif廣告圖功能。
android自帶控件不支持gif圖片,網上很多通過擴展ImageView或View來實現支持gif圖片,但在android4.0后,需要關閉硬件加速功能才能使用,而且也容易出現內存溢出問題。
網上找了兩個開源包來實現顯示Gif圖
android-gif-drawable 支持gif顯示的view控件
項目地址:https://github.com/koral--/android-gif-drawable
用jni實現的,編譯生成so庫后直接xml定義view,據說性能比較好,也能比較好避免內存內存溢出問題。
在Android Studio項目添加使用:
build.gradle文件dependencies添加內容:
dependencies {
compile 'pl.droidsonroids.gif:android-gif-drawable:1.1.+' /* 添加gif控件庫引用 */
}
xUtils
項目地址:https://github.com/wyouflf/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<File>() { public void onSuccess(ResponseInfo<File> 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
<RelativeLayout 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" 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"> <TextView android: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>
本文由用戶 jopen 自行上傳分享,僅供網友學習交流。所有權歸原作者,若您的權利被侵害,請聯系管理員。
轉載本站原創文章,請注明出處,并保留原始鏈接、圖片水印。
本站是一個以用戶分享為主的開源技術平臺,歡迎各類分享!