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);
參考
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);