Glide4.0集成及使用注意事项
来源:互联网 发布:淘宝哪家买iphone靠谱 编辑:程序博客网 时间:2024/06/06 07:18
1、由于Glide4.0和Glide3.x的用法出入很大 在此总结一下。
在Glide3.x中 加载网络图片通常习惯默认显示预加载和加载失败的图片
String path= "https://inthecheesefactory.com/uploads/source/glidepicasso/cover.jpg";
Glide.with(activity).load(path).placeholder(R.mipmap.ic_launcher).error(R.mipmap.ic_launcher).into(imageView);
在4.0中就不能直接调用如上的方法了,需要使用RequestOption这个类,具体用法如下:
RequestOptions options = new RequestOptions()
.centerCrop()
.placeholder(R.mipmap.ic_launcher)
.error(R.mipmap.ic_launcher)
.priority(Priority.HIGH);
Glide.with(this).load(path).apply(options).into(imageView);
2、使用Glide4.0显示圆形图片
首先我们需要创建一个转换圆形的类,具体代码如下:
public class GlideCircleTransform extends BitmapTransformation {
public GlideCircleTransform() {
super();
}
@Override
public Bitmap transform(BitmapPool pool, Bitmap toTransform,
int outWidth, int outHeight) {
return circleCrop(pool, toTransform);
}
private static Bitmap circleCrop(BitmapPool pool, Bitmap source) {
if (source == null) return null;
int size = Math.min(source.getWidth(), source.getHeight());
int x = (source.getWidth() - size) / 2;
int y = (source.getHeight() - size) / 2;
// TODO this could be acquired from the pool too
Bitmap squared = Bitmap.createBitmap(source, x, y, size, size);
Bitmap result = pool.get(size, size, Bitmap.Config.ARGB_8888);
if (result == null) {
result = Bitmap.createBitmap(size, size, Bitmap.Config.ARGB_8888);
}
Canvas canvas = new Canvas(result);
Paint paint = new Paint();
paint.setShader(new BitmapShader(squared, BitmapShader.TileMode.CLAMP, BitmapShader.TileMode.CLAMP));
paint.setAntiAlias(true);
float r = size / 2f;
canvas.drawCircle(r, r, r, paint);
return result;
}
@Override
public void updateDiskCacheKey(MessageDigest messageDigest) {
}
}
然后定义RequestOptions:
RequestOptions options = new RequestOptions()
.centerCrop()
.placeholder(R.mipmap.ic_launcher)
.error(R.mipmap.ic_launcher)
.priority(Priority.HIGH)
.transform(new GlideCircleTransform());
调用glide显示图片:
Glide.with(this).load(path).apply(options).into(imageView);
3、使用Glide4.0显示带圆角的图片
一样需要创建一个可以显示圆角图片的转换类
public class GlideRoundTransform extends BitmapTransformation {
private static float radius = 0f;
public GlideRoundTransform() {
this(4);
}
public GlideRoundTransform(int dp) {
super();
this.radius = Resources.getSystem().getDisplayMetrics().density * dp;
}
@Override protected Bitmap transform(BitmapPool pool, Bitmap toTransform, int outWidth, int outHeight) {
return roundCrop(pool, toTransform);
}
private static Bitmap roundCrop(BitmapPool pool, Bitmap source) {
if (source == null) return null;
Bitmap result = pool.get(source.getWidth(), source.getHeight(), Bitmap.Config.ARGB_8888);
if (result == null) {
result = Bitmap.createBitmap(source.getWidth(), source.getHeight(), Bitmap.Config.ARGB_8888);
}
Canvas canvas = new Canvas(result);
Paint paint = new Paint();
paint.setShader(new BitmapShader(source, BitmapShader.TileMode.CLAMP, BitmapShader.TileMode.CLAMP));
paint.setAntiAlias(true);
RectF rectF = new RectF(0f, 0f, source.getWidth(), source.getHeight());
canvas.drawRoundRect(rectF, radius, radius, paint);
return result;
}
@Override
public void updateDiskCacheKey(MessageDigest messageDigest) {
}
}
然后定义RequestOptions:
RequestOptions options = new RequestOptions()
.centerCrop()
.placeholder(R.mipmap.ic_launcher)
.error(R.mipmap.ic_launcher)
.priority(Priority.HIGH)
.transform(new GlideRoundTransform());
调用glide显示图片:
Glide.with(this).load(path).apply(options).into(imageView);
以上就是总结出Glide4.0的部分新用法。
备注:定义的RequestOption是可以创建一次多次使用 不需要加载一次就去创建一次。
- Glide4.0集成及使用注意事项
- Glide4.0使用详解
- Glide4.0+使用的坑
- Glide4.x的使用
- Glide出现的相关错误,以及glide4.0的使用
- Android Glide4.2.0版本使用
- Glide4.0的基本用法
- Glide4.0加载https图片
- 极光推送集成及注意事项(AndroidStudio)
- ionic集成Crosswalk内核注意事项及步骤
- 极光推送集成步骤及注意事项
- Retrofit2.0使用总结及注意事项
- Retrofit2.0使用总结及注意事项
- Retrofit2.0使用总结及注意事项
- Retrofit2.0使用总结及注意事项
- Retrofit2.0使用总结及注意事项
- MagicAjax使用及注意事项
- MagicAjax使用及注意事项
- 51nod 1174区间中最大的数(线段树)
- enum 在c中的使用
- Inno Setup入门(十五)——Inno Setup类参考(1)
- iOS View自定义窍门——UIButton实现上显示图片,下显示文字
- 飞塔防火墙通过CLI底层刷入OS
- Glide4.0集成及使用注意事项
- NGUI 代码实现UISprite显示图片原尺寸的snap功能
- Mask R-CNN Keras Tensorflow实现
- 常用信息汇总
- 如何用ChemDraw Prime 绘制任意弧线箭头
- Francisco Partners收购Comodo CA业务
- 高阶函数与与匿名函数
- 触发器调用Java接口程序(一)
- 微服务之Swagger