Glide依賴
dependencies {
api fileTree(include: ['*.jar'], dir: 'libs')
// 如果沒有升級androidx,使用4.9.0版本
api 'com.github.bumptech.glide:glide:4.11.0'
annotationProcessor 'com.github.bumptech.glide:compiler:4.11.0'
api "com.github.bumptech.glide:okhttp3-integration:4.11.0"
}
AppGlideModule配置
核心變換設(shè)置的地方
// 此處可以讀取一個開關(guān)莽红,來決定圖片是否用黑白模式
// requestOptions.bitmapTransform(new GrayscaleTransformation());
import android.content.Context;
import androidx.annotation.NonNull;
import com.bumptech.glide.Glide;
import com.bumptech.glide.GlideBuilder;
import com.bumptech.glide.Registry;
import com.bumptech.glide.annotation.GlideModule;
import com.bumptech.glide.load.engine.DiskCacheStrategy;
import com.bumptech.glide.load.engine.cache.ExternalCacheDiskCacheFactory;
import com.bumptech.glide.module.AppGlideModule;
import com.bumptech.glide.request.RequestOptions;
/**
* Created by kangyi on 2018/3/20.
*
* Glide全局配置膀藐,使用GlideModule注解執(zhí)行自動代碼生成荆永,生成GlideApp,后續(xù)的Glide
* 調(diào)用都需要替換為GlideApp.with(context).load(url).into(imageView) 的方式
*
*/
@GlideModule
public class GlobalGlideConfig extends AppGlideModule {
@Override
public void registerComponents(@NonNull Context context, @NonNull Glide glide, @NonNull Registry registry) {
super.registerComponents(context, glide, registry);
}
@Override
public boolean isManifestParsingEnabled() {
return false;
}
@Override
public void applyOptions(@NonNull Context context, @NonNull GlideBuilder builder) {
super.applyOptions(context, builder);
/**
* DiskCacheStrategy.NONE: 表示不緩存任何內(nèi)容。
* DiskCacheStrategy.DATA: 表示只緩存原始圖片孩饼。
* DiskCacheStrategy.RESOURCE: 表示只緩存轉(zhuǎn)換過后的圖片。
* DiskCacheStrategy.ALL : 表示既緩存原始圖片竹挡,也緩存轉(zhuǎn)換過后的圖片镀娶。
* DiskCacheStrategy.AUTOMATIC: 表示讓Glide根據(jù)圖片資源智能地選擇使用哪一種緩存策略(默認(rèn)選項)。
*/
builder.setDefaultRequestOptions(getRequestOptions());
/**
* 優(yōu)先外部存儲作為磁盤緩存目錄揪罕,防止內(nèi)部存儲文件過大
* 外部存儲目錄默認(rèn)地址為:/sdcard/Android/data/com.leo.kang.cetfour/cache/image_manager_disk_cache
*/
builder.setDiskCache(new ExternalCacheDiskCacheFactory(context));
}
/**
* 獲取Glide的全局配置信息
* @return
*/
private RequestOptions getRequestOptions() {
RequestOptions requestOptions = new RequestOptions();
requestOptions.diskCacheStrategy(DiskCacheStrategy.AUTOMATIC);
// 此處可以讀取一個開關(guān)梯码,來決定圖片是否用黑白模式
// requestOptions.bitmapTransform(new GrayscaleTransformation());
return requestOptions;
}
}
圖片灰化變換
import android.graphics.Bitmap;
import android.graphics.Canvas;
import android.graphics.ColorMatrix;
import android.graphics.ColorMatrixColorFilter;
import android.graphics.Paint;
import androidx.annotation.NonNull;
import com.bumptech.glide.load.engine.bitmap_recycle.BitmapPool;
import com.bumptech.glide.load.resource.bitmap.BitmapTransformation;
import com.weibo.imageloader.GlideConstants;
import java.security.MessageDigest;
public class GrayscaleTransformation extends BitmapTransformation {
private static final int VERSION = 1;
private static final String ID = "packagename.GrayscaleTransformation" + VERSION;
@Override
public String toString() {
return "GrayscaleTransformation()";
}
@Override
public boolean equals(Object o) {
return o instanceof GrayscaleTransformation;
}
@Override
public int hashCode() {
return ID.hashCode();
}
@Override
public void updateDiskCacheKey(@NonNull MessageDigest messageDigest) {
messageDigest.update((ID).getBytes(CHARSET));
}
@Override
protected Bitmap transform(@NonNull BitmapPool pool, @NonNull Bitmap toTransform, int outWidth, int outHeight) {
int width = toTransform.getWidth();
int height = toTransform.getHeight();
Bitmap.Config config =
toTransform.getConfig() != null ? toTransform.getConfig() : Bitmap.Config.ARGB_8888;
Bitmap bitmap = pool.get(width, height, config);
setCanvasBitmapDensity(toTransform, bitmap);
Canvas canvas = new Canvas(bitmap);
ColorMatrix saturation = new ColorMatrix();
saturation.setSaturation(0f);
Paint paint = new Paint();
paint.setColorFilter(new ColorMatrixColorFilter(saturation));
canvas.drawBitmap(toTransform, 0, 0, paint);
return bitmap;
}
void setCanvasBitmapDensity(@NonNull Bitmap toTransform, @NonNull Bitmap canvasBitmap) {
canvasBitmap.setDensity(toTransform.getDensity());
}
}