Android開發(fā)中ImageLoder進行圖片加載和緩存
更新時間:2016年04月10日 16:10:43 作者:zenglongfei
這篇文章主要介紹了Android開發(fā)中ImageLoder進行圖片加載和緩存的相關(guān)資料,需要的朋友可以參考下
圖片處理類:
package com.longfei.admin.imageloder_text;
import android.app.Application;
import android.graphics.Bitmap;
import android.os.Environment;
import com.nostra13.universalimageloader.cache.disc.impl.UnlimitedDiscCache;
import com.nostra13.universalimageloader.cache.disc.naming.Md5FileNameGenerator;
import com.nostra13.universalimageloader.cache.memory.impl.UsingFreqLimitedMemoryCache;
import com.nostra13.universalimageloader.core.DisplayImageOptions;
import com.nostra13.universalimageloader.core.ImageLoader;
import com.nostra13.universalimageloader.core.ImageLoaderConfiguration;
import com.nostra13.universalimageloader.core.assist.ImageScaleType;
import com.nostra13.universalimageloader.core.assist.QueueProcessingType;
import com.nostra13.universalimageloader.core.display.FadeInBitmapDisplayer;
import com.nostra13.universalimageloader.core.display.RoundedBitmapDisplayer;
import com.nostra13.universalimageloader.core.download.BaseImageDownloader;
import java.io.File;
/**
* Created by admin on 2016/4/9.
*/
public class ImageLoading extends Application {
@Override
public void onCreate() {
// TODO Auto-generated method stub
super.onCreate();
ImageLoaderConfiguration config = new ImageLoaderConfiguration.Builder(
this)
.memoryCacheExtraOptions(480, 800)
// max width, max height,即保存的每個緩存文件的最大長寬
.discCacheExtraOptions(480, 800, null)
// Can slow ImageLoader, use it carefully (Better don't use
// it)/設(shè)置緩存的詳細信息,最好不要設(shè)置這個
.threadPoolSize(3)
// 線程池內(nèi)加載的數(shù)量
.threadPriority(Thread.NORM_PRIORITY - 2)
.denyCacheImageMultipleSizesInMemory()
.memoryCache(new UsingFreqLimitedMemoryCache(2 * 1024 * 1024))
// You can pass your own memory cache
// implementation/你可以通過自己的內(nèi)存緩存實現(xiàn)
.memoryCacheSize(2 * 1024 * 1024)
.discCacheSize(50 * 1024 * 1024)
.discCacheFileNameGenerator(new Md5FileNameGenerator())
// 將保存的時候的URI名稱用MD5 加密
.tasksProcessingOrder(QueueProcessingType.LIFO)
.discCacheFileCount(100)
// 緩存的文件數(shù)量
.discCache(
new UnlimitedDiscCache(new File(Environment
.getExternalStorageDirectory()
+ "/myApp/imgCache")))
// 自定義緩存路徑
.defaultDisplayImageOptions(getDisplayOptions())
.imageDownloader(
new BaseImageDownloader(this, 5 * 1000, 30 * 1000))
.writeDebugLogs() // Remove for release app
.build();// 開始構(gòu)建
ImageLoader.getInstance().init(config);
}
private DisplayImageOptions getDisplayOptions() {
DisplayImageOptions options;
options = new DisplayImageOptions.Builder()
.showImageOnLoading(R.drawable.ic_launcher) // 設(shè)置圖片在下載期間顯示的圖片
.showImageForEmptyUri(R.drawable.ic_launcher)// 設(shè)置圖片Uri為空或是錯誤的時候顯示的圖片
.showImageOnFail(R.drawable.ic_launcher) // 設(shè)置圖片加載/解碼過程中錯誤時候顯示的圖片
.cacheInMemory(true)// 設(shè)置下載的圖片是否緩存在內(nèi)存中
.cacheOnDisc(true)// 設(shè)置下載的圖片是否緩存在SD卡中
.considerExifParams(true) // 是否考慮JPEG圖像EXIF參數(shù)(旋轉(zhuǎn),翻轉(zhuǎn))
.imageScaleType(ImageScaleType.EXACTLY_STRETCHED)// 設(shè)置圖片以如何的編碼方式顯示
.bitmapConfig(Bitmap.Config.RGB_565)// 設(shè)置圖片的解碼類型//
// .delayBeforeLoading(int delayInMillis)//int
// delayInMillis為你設(shè)置的下載前的延遲時間
// 設(shè)置圖片加入緩存前,對bitmap進行設(shè)置
// .preProcessor(BitmapProcessor preProcessor)
.resetViewBeforeLoading(true)// 設(shè)置圖片在下載前是否重置,復(fù)位
.displayer(new RoundedBitmapDisplayer(20))// 是否設(shè)置為圓角,弧度為多少
.displayer(new FadeInBitmapDisplayer(100))// 是否圖片加載好后漸入的動畫時間
.build();// 構(gòu)建完成
return options;
}
}
package com.longfei.admin.imageloder_text;
import android.app.Activity;
import android.graphics.Bitmap;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.ImageView;
import com.nostra13.universalimageloader.core.ImageLoader;
import com.nostra13.universalimageloader.core.assist.FailReason;
import com.nostra13.universalimageloader.core.listener.ImageLoadingListener;
/**
* 1、 Universal-ImageLoader的配置
*
* 2、用Universal-ImageLoader加載網(wǎng)絡(luò)圖片和本地圖片
*
* @author Administrator
*/
public class MainActivity extends Activity {
private ImageLoader loader;
private ImageView iv_img;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
loader=ImageLoader.getInstance();//實例化ImageLoder
iv_img=(ImageView)findViewById(R.id.iv_img);
String url="file:///"+"加載本地圖片";
//加載網(wǎng)絡(luò)圖片,第一個參數(shù)為路徑
// loader.displayImage("http://static.oschina.net/uploads/user/494/988131_100.jpg?t=1419303093000",iv_img);
loader.displayImage("http://static.oschina.net/uploads/user/494/988131_100.jpg?t=1419303093000", iv_img, new ImageLoadingListener() {
@Override
public void onLoadingStarted(String s, View view) {
Log.i("info","加載開始");
}
@Override
public void onLoadingFailed(String s, View view, FailReason failReason) {
Log.i("info","加載失敗");
}
@Override
public void onLoadingComplete(String s, View view, Bitmap bitmap) {
Log.i("info","加載完畢");
}
@Override
public void onLoadingCancelled(String s, View view) {
Log.i("info","加載取消");
}
});
}
}
相關(guān)文章
Android使用自定義View實現(xiàn)橫行時間軸效果
這篇文章主要給大家介紹了關(guān)于Android使用自定義View實現(xiàn)橫行時間軸效果的相關(guān)資料,文中通過示例代碼介紹的非常詳細,對大家學習或者使用Android具有一定的參考學習價值,需要的朋友們下面來一起學習學習吧2019-12-12
android檢測網(wǎng)絡(luò)連接狀態(tài)示例講解
網(wǎng)絡(luò)的時候,并不是每次都能連接到網(wǎng)絡(luò),因此在程序啟動中需要對網(wǎng)絡(luò)的狀態(tài)進行判斷,如果沒有網(wǎng)絡(luò)則提醒用戶進行設(shè)置2014-02-02
android實現(xiàn)緩存圖片等數(shù)據(jù)
本文給大家分享的是Android采用LinkedHashMap自帶的LRU 算法緩存數(shù)據(jù)的方法和示例,有需要的小伙伴可以參考下。2015-07-07
Android開發(fā)之ClipboardManager剪貼板功能示例
這篇文章主要介紹了Android開發(fā)之ClipboardManager剪貼板功能,結(jié)合簡單實例形式分析了Android使用ClipboardManager實現(xiàn)剪貼板功能的相關(guān)操作技巧,需要的朋友可以參考下2017-03-03
Android Studio 實現(xiàn)將support庫改成Androidx
這篇文章主要介紹了Android Studio 實現(xiàn)將support庫改成Androidx,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧2020-04-04
android?scrollview頂部漸漸消失實現(xiàn)實例詳解
這篇文章主要為大家介紹了android?scrollview頂部漸漸消失實現(xiàn)實例詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進步,早日升職加薪2023-11-11

