本文的glide使用的版本:
implementation ‘com.github.bumptech.glide:glide:3.7.0’
implementation ‘com.github.bumptech.glide:glide:4.10.0’
主要是介绍了几个显著的变化点,经常用到的,尤其是可以直接设置图片的圆角等方法
举例用的 url = http://duty-oss.adnonstop.com/217045801175425cae1f47dd2d23a38c-20191105-217045801175425cae1f47dd2d23a38c.jpeg
第一点:设置过渡动画
Glide3
Glide.with(this)
.load(img1)
.placeholder(R.drawable.ic_launcher_background)
.error(R.drawable.ic_launcher_background)
.diskCacheStrategy(DiskCacheStrategy.SOURCE)
.override(500, 500)
.crossFade(500)//设置渐变效果,默认时间300 修改为500
.into(mImgView);
Glide4
DrawableCrossFadeFactory drawableCrossFadeFactory = new DrawableCrossFadeFactory.Builder(500).setCrossFadeEnabled(true).build();
Glide.with(this).load(Constant.img1)
.placeholder(R.drawable.ic_launcher_background)
.error(R.drawable.ic_launcher_background)
.diskCacheStrategy(DiskCacheStrategy.DATA)
.override(500, 500)
.transition(DrawableTransitionOptions.with(drawableCrossFadeFactory))
.into(mImg);
第二点:例如修改图片的默认加载像素,默认是RGB_565
在4.0中不用像3.X需要在AndroidManifest.xml配置GlideModule,而是通过注解继承AppGlideModule的子类来配置
public class GlideConfiguration implements GlideModule {
@Override
public void applyOptions(Context context, GlideBuilder builder) {
builder.setDecodeFormat(DecodeFormat.PREFER_ARGB_8888)
}
@Override
public void registerComponents(Context context, Glide glide) {
}
}
//AndroidManifest.xml配置GlideModule
//com.example.glidepplication.GlideConfiguration 包名
<meta-data
android:name="com.example.glidepplication.GlideConfiguration"
android:value="GlideModule" />
Glide4
@GlideModule
public class GlideConfiguration extends AppGlideModule {
@Override
public void applyOptions(@NonNull Context context, @NonNull GlideBuilder builder) {
builder.setDefaultRequestOptions(new RequestOptions().format(DecodeFormat.PREFER_ARGB_8888));
}
}
第三点 :
Glide4 :asBitmap、asGif、asDrawable、asFile都要放到load之前
glide3.7.0都是要在load之后调用
第四点:将处理后的bitmap加载到view上
我这里将请求的bitmap按照宽高比显示在imageView 上
glide 3.x
Glide.with(this).load(Constant.img1).asBitmap().into(new SimpleTarget<Bitmap>() {
@Override
public void onResourceReady(Bitmap resource, GlideAnimation<? super Bitmap> glideAnimation) {
if (resource != null) {
int bitmapWidth = resource.getWidth();
int bitmapHeight = resource.getHeight();
float showWidth = getResources().getDisplayMetrics().widthPixels;
float showHeight = showWidth / bitmapWidth * bitmapHeight;
LinearLayout.LayoutParams params = (LinearLayout.LayoutParams) mImgView.getLayoutParams();
params.width = (int) showWidth;
params.height = (int) showHeight;
mImgView.setLayoutParams(params);
mImgView.setImageBitmap(resource);
}
}
});
glide 4.10
Glide.with(this).asBitmap().load(Constant.img1).into(new CustomTarget<Bitmap>() {
@Override
public void onResourceReady(@NonNull Bitmap resource, @Nullable Transition<? super Bitmap> transition) {
if (resource != null) {
int bitmapWidth = resource.getWidth();
int bitmapHeight = resource.getHeight();
float showWidth = getResources().getDisplayMetrics().widthPixels;
float showHeight = showWidth / bitmapWidth * bitmapHeight;
LinearLayout.LayoutParams params = (LinearLayout.LayoutParams) mImgView.getLayoutParams();
params.width = (int) showWidth;
params.height = (int) showHeight;
Log.i("measure", "onResourceReady: " + showWidth + " ssssheight : " + showHeight);
mImgView.setLayoutParams(params);
mImgView.setImageBitmap(resource);
}
}
@Override
public void onLoadCleared(@Nullable Drawable placeholder) {
}
});
第五点:直接设置圆角图片或者是圆形图片
Glide 4
Glide.with(this).load(url).apply(RequestOptions.bitmapTransform(new CircleCrop())).into(imageView);
或者
RequestOptions requestOptions = RequestOptions.circleCropTransform();
Glide.with(this).load(url).apply(requestOptions).into(imageView);
//设置图片圆角角度
RoundedCorners roundedCorners= new RoundedCorners(10);
//通过RequestOptions扩展功能,override:采样率,因为ImageView就这么大,可以压缩图片,降低内存消耗
RequestOptions options=RequestOptions.bitmapTransform(roundedCorners);
Glide.with(context).load(url).apply(options).into(image);