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配置
核心变换设置的地方
// 此处可以读取一个开关,来决定图片是否用黑白模式
// 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注解执行自动代码生成,生成GlideApp,后续的Glide
* 调用都需要替换为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: 表示不缓存任何内容。
* DiskCacheStrategy.DATA: 表示只缓存原始图片。
* DiskCacheStrategy.RESOURCE: 表示只缓存转换过后的图片。
* DiskCacheStrategy.ALL : 表示既缓存原始图片,也缓存转换过后的图片。
* DiskCacheStrategy.AUTOMATIC: 表示让Glide根据图片资源智能地选择使用哪一种缓存策略(默认选项)。
*/
builder.setDefaultRequestOptions(getRequestOptions());
/**
* 优先外部存储作为磁盘缓存目录,防止内部存储文件过大
* 外部存储目录默认地址为:/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);
// 此处可以读取一个开关,来决定图片是否用黑白模式
// 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());
}
}
网友评论