ImageLoader壤玫、Picasso精绎、Fresco祟霍、Glide

ImageLoader的使用

依賴? compile 'com.nostra13.universalimageloader:universal-image-loader:1.9.5'

首先在當前程序的Application中調用ImageLoader的初始化init()方法

private?void?initImageLoader()?{??

ImageLoaderConfiguration?config?=new?ImageLoaderConfiguration.Builder(this).imageDownloader(??

new?BaseImageDownloader(this,?60?*?1000,?60?*?1000))?//?connectTimeout超時時間??

????????????????.build();??

????????ImageLoader.getInstance().init(config);??

????} ?

使用:

imageview.displayImage();


Picasso:

1.添加依賴

compile 'com.squareup.picasso:picasso:2.5.2'


2.加載顯示圖片

Picasso.with(this)

? ? ? ? ? ? ? ? .load(......)

? ? ? ? ? ? ? ? .into(view);

load路徑: ?? 1)網(wǎng)絡圖片url ? ? ? ? ? 2)file路徑

? ? ? ? ? ? ? ? ? ? ? 3)content資源 ? ? ? ?? 4)Android Resoure


3.設置圖片

尺寸resize()杏头、縮放Scale()、旋轉Rotation()沸呐、

居中裁剪centerCrop()醇王、fit()拉伸


4.轉換器Transformation

可以做高斯模糊、圓角崭添、度灰處理寓娩、圓形圖片等


//高斯模糊

class BlurTransformationimplements Transformation{

RenderScriptrs;

public BlurTransformation(Context context) {

super();

rs = RenderScript.create(context);

}

@Override

? ? public Bitmap transform(Bitmap bitmap) {

// Create another bitmap that will hold the results of the filter.

? ? ? ? Bitmap blurredBitmap = bitmap.copy(Bitmap.Config.ARGB_8888,true);

// Allocate memory for Renderscript to work with

? ? ? ? Allocation input = Allocation.createFromBitmap(rs, blurredBitmap, Allocation.MipmapControl.MIPMAP_FULL, Allocation.USAGE_SHARED);

Allocation output = Allocation.createTyped(rs, input.getType());

// Load up an instance of the specific script that we want to use.

? ? ? ? ScriptIntrinsicBlur script = ScriptIntrinsicBlur.create(rs, Element.U8_4(rs));

script.setInput(input);

// Set the blur radius

? ? ? ? script.setRadius(25);

// Start the ScriptIntrinisicBlur

? ? ? ? script.forEach(output);

// Copy the output to the blurred bitmap

? ? ? ? output.copyTo(blurredBitmap);

bitmap.recycle();

return blurredBitmap;

}

@Override

? ? public String key() {

return "blur";

}

}


//度灰處理

class GrayTransformationimplements Transformation{

@Override

? ? public Bitmap transform(Bitmap source) {

int width, height;

height = source.getHeight();

width = source.getWidth();

Bitmap bmpGrayscale = Bitmap.createBitmap(width, height, Bitmap.Config.RGB_565);

Canvas c =new Canvas(bmpGrayscale);

Paint paint =new Paint();

ColorMatrix cm =new ColorMatrix();

cm.setSaturation(0);

ColorMatrixColorFilter f =new ColorMatrixColorFilter(cm);

paint.setColorFilter(f);

c.drawBitmap(source,0,0, paint);

if(source!=null && source!=bmpGrayscale){

source.recycle();

}

return bmpGrayscale;

}

@Override

? ? public String key() {

return "gray";

}

}


5.請求優(yōu)先級

? ? ? ? ? ? ? ? LOW、NORMAL(默認)、HIGH


6.Tag管理類

cancelTag(Object tag) 取消設置了給定tag的所有請求

pauseTag(Object tag)? 暫停設置了給定tag 的所有請求

resumeTag(Object tag) resume 被暫停的給定tag的所有請求


7.同步/異步加載圖片

同步:同步加載使用get() 方法棘伴,返回一個Bitmap 對象

注:使用同步方式加載寞埠,不能放在主線程中操作

try{

????????????Bitmap bitmap =? Picasso.with(this).load(URL).get();

}catch(IOException e) {

? ? ? ? ? ? e.printStackTrace();

?}

異步:fetch(Callback callback) 異步方式加載圖片并給一個回調接口

Picasso.with(this).load(URL).fetch(newCallback() {

????????@Override

????????public void onSuccess() {

????????????????????//加載成功

?????????}

????????@Override

????????public void onError() {

????????????????//加載失敗

?????????}

?});


8.緩存

Picasso 有內存緩存(Memory)和磁盤緩存( Disk)


9.Debug和日志

? ? ? ? 1)緩存指示器

????????????????Picasso.with(this)

????????????????????????????????.setIndicatorsEnabled(true);//顯示指示器

? ? ? ? 2)日志

????????????????Picasso.with(this)

????????????????????????????????.setLoggingEnabled(true);//開啟日志打印


10.Picasso擴展

? ? ? ? ? ? ? ? 1)配置緩存

????//配置緩存

????LruCache cache =newLruCache(5*1024*1024);// 設置緩存大小

? ? builder.memoryCache(cache);

? ? ? ? ? ? ? ? ?? 2)配置線程池

? ? ? //配置線程池

? ? ? ExecutorService executorService = Executors.newFixedThreadPool(8);

? ? ? builder.executor(executorService);


參考

Picasso官網(wǎng)

Picasso — Getting Started



Fresco:

????????參考鏈接:

????????????????fresco



Glide

implementation 'com.github.bumptech.glide:glide:4.5.0'

annotationProcessor 'com.github.bumptech.glide:compiler:4.5.0'


使用

Glide.with(Context context).load(Strint url).into(ImageView imageView);


加載動態(tài)圖片

????????Glide.with(this) ? ?

????????????????.load(url) ? ?

????????????????.asGif()//加載動態(tài)圖片,若現(xiàn)有圖片為非gif圖片焊夸,則直接加載錯誤占位圖仁连。

????????????????.placeholder(R.drawable.loading) ? ?

????????????????.error(R.drawable.error) ??

?????????????????.diskCacheStrategy(DiskCacheStrategy.NONE) ??

?????????????????.into(imageView);


加載指定大小的圖片

????????Glide.with(this) ??

?????????????????.load(url) ? ?

? ? ? ? ? ? ? ?? .placeholder(R.drawable.loading) ? ?

? ? ? ? ? ? ? ?? .error(R.drawable.error) ? ?

????????????????.diskCacheStrategy(DiskCacheStrategy.NONE) ? ?

????????????????.override(100,100)//指定圖片大小

????????????????.into(imageView);


關閉框架的內存緩存機制

????????Glide.with(this) ? ?

????????????????.load(url) ? ?

????????????????.skipMemoryCache(true)//傳入?yún)?shù)為false時,則關閉內存緩存阱穗。

????????????????.into(imageView);

關閉硬盤的緩存

? ? ? ? ?? Glide.with(this) ? ?

????????????????.load(url) ? ?

????????????????.diskCacheStrategy(DiskCacheStrategy.NONE)//關閉硬盤緩存操作

????????????????.into(imageView);


復雜的圖像變換

dependencies {

??????? implementation 'jp.wasabeef:glide-transformations:3.3.0' ?

?????????Filters? ? implementation 'jp.co.cyberagent.android.gpuimage:gpuimage-library:1.4.1'

}

圖片虛化

????????Glide.with(this)

????????????????.load(url)

????????????????.bitmapTransform(newBlurTransformation(this))

????????????????.into(imageView);

圖片黑白化

????????Glide.with(this)

? ? ? ? ? ? ? ? .load(url)

????????????????.bitmapTransform(newGrayscaleTransformation(this))

????????????????.into(imageView);

多個屬性同時使用

????????Glide.with(this) ? ?

? ? ? ? ? ? ? ?? .load(url) ??

?????????????????.bitmapTransform(newBlurTransformation(this)

? ? ? ? ? ? ? ? .newGrayscaleTransformation(this)) ? ?

????????????????.into(imageView);


Fresco饭冬,Glide,Picasso對比分析

區(qū)別

最后編輯于
?著作權歸作者所有,轉載或內容合作請聯(lián)系作者
  • 序言:七十年代末揪阶,一起剝皮案震驚了整個濱河市昌抠,隨后出現(xiàn)的幾起案子,更是在濱河造成了極大的恐慌鲁僚,老刑警劉巖扰魂,帶你破解...
    沈念sama閱讀 218,858評論 6 508
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件,死亡現(xiàn)場離奇詭異蕴茴,居然都是意外死亡劝评,警方通過查閱死者的電腦和手機,發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 93,372評論 3 395
  • 文/潘曉璐 我一進店門倦淀,熙熙樓的掌柜王于貴愁眉苦臉地迎上來蒋畜,“玉大人,你說我怎么就攤上這事撞叽∫龀桑” “怎么了?”我有些...
    開封第一講書人閱讀 165,282評論 0 356
  • 文/不壞的土叔 我叫張陵愿棋,是天一觀的道長科展。 經(jīng)常有香客問我,道長糠雨,這世上最難降的妖魔是什么才睹? 我笑而不...
    開封第一講書人閱讀 58,842評論 1 295
  • 正文 為了忘掉前任,我火速辦了婚禮甘邀,結果婚禮上琅攘,老公的妹妹穿的比我還像新娘。我一直安慰自己松邪,他們只是感情好坞琴,可當我...
    茶點故事閱讀 67,857評論 6 392
  • 文/花漫 我一把揭開白布。 她就那樣靜靜地躺著逗抑,像睡著了一般剧辐。 火紅的嫁衣襯著肌膚如雪寒亥。 梳的紋絲不亂的頭發(fā)上,一...
    開封第一講書人閱讀 51,679評論 1 305
  • 那天荧关,我揣著相機與錄音护盈,去河邊找鬼。 笑死羞酗,一個胖子當著我的面吹牛腐宋,可吹牛的內容都是我干的。 我是一名探鬼主播檀轨,決...
    沈念sama閱讀 40,406評論 3 418
  • 文/蒼蘭香墨 我猛地睜開眼胸竞,長吁一口氣:“原來是場噩夢啊……” “哼!你這毒婦竟也來了参萄?” 一聲冷哼從身側響起卫枝,我...
    開封第一講書人閱讀 39,311評論 0 276
  • 序言:老撾萬榮一對情侶失蹤,失蹤者是張志新(化名)和其女友劉穎讹挎,沒想到半個月后校赤,有當?shù)厝嗽跇淞掷锇l(fā)現(xiàn)了一具尸體,經(jīng)...
    沈念sama閱讀 45,767評論 1 315
  • 正文 獨居荒郊野嶺守林人離奇死亡筒溃,尸身上長有42處帶血的膿包…… 初始之章·張勛 以下內容為張勛視角 年9月15日...
    茶點故事閱讀 37,945評論 3 336
  • 正文 我和宋清朗相戀三年马篮,在試婚紗的時候發(fā)現(xiàn)自己被綠了。 大學時的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片怜奖。...
    茶點故事閱讀 40,090評論 1 350
  • 序言:一個原本活蹦亂跳的男人離奇死亡浑测,死狀恐怖,靈堂內的尸體忽然破棺而出歪玲,到底是詐尸還是另有隱情迁央,我是刑警寧澤,帶...
    沈念sama閱讀 35,785評論 5 346
  • 正文 年R本政府宣布滥崩,位于F島的核電站岖圈,受9級特大地震影響,放射性物質發(fā)生泄漏钙皮。R本人自食惡果不足惜蜂科,卻給世界環(huán)境...
    茶點故事閱讀 41,420評論 3 331
  • 文/蒙蒙 一、第九天 我趴在偏房一處隱蔽的房頂上張望株灸。 院中可真熱鬧崇摄,春花似錦擎值、人聲如沸慌烧。這莊子的主人今日做“春日...
    開封第一講書人閱讀 31,988評論 0 22
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽屹蚊。三九已至厕氨,卻和暖如春,著一層夾襖步出監(jiān)牢的瞬間汹粤,已是汗流浹背命斧。 一陣腳步聲響...
    開封第一講書人閱讀 33,101評論 1 271
  • 我被黑心中介騙來泰國打工, 沒想到剛下飛機就差點兒被人妖公主榨干…… 1. 我叫王不留嘱兼,地道東北人国葬。 一個月前我還...
    沈念sama閱讀 48,298評論 3 372
  • 正文 我出身青樓,卻偏偏與公主長得像芹壕,于是被迫代替她去往敵國和親汇四。 傳聞我的和親對象是個殘疾皇子,可洞房花燭夜當晚...
    茶點故事閱讀 45,033評論 2 355

推薦閱讀更多精彩內容