简介
Glide 是一个快速高效的 Android 图片加载库,注重于平滑的滚动。
开始
- 在 app/build.gradle 文件当中添加如下依赖:
dependencies {
compile 'com.github.bumptech.glide:glide:4.8.0';
}
- 在 AndroidManifest.xml 添加网络权限:
<uses-permission android:name="android.permission.INTERNET" />
使用
- 基本用法
多数情况下,使用Glide加载图片非常简单,一行代码足矣:
Glide.with(this) .load(url) .into(imageView);
解析一下这行代码:首先,调用Glide.with()方法用于创建一个加载图片的实例。with()方法可以接收Context、Activity或者Fragment类型的参数。
其次,load()方法用于指定待加载的图片资源。Glide支持加载各种各样的图片资源,包括网络图片、本地图片、应用资源、二进制流、Uri对象等等。
// 加载本地图片
File file = new File(getExternalCacheDir() + "/image.jpg");
Glide.with(this).load(file).into(imageView);
// 加载应用资源
int resource = R.drawable.image;
Glide.with(this).load(resource).into(imageView);
// 加载二进制流
byte[] image = getImageBytes();
Glide.with(this).load(image).into(imageView);
// 加载Uri对象
Uri imageUri = getImageUri();
Glide.with(this).load(imageUri).into(imageView);
- 最后,into() 方法,我们希望让图片显示在哪个 ImageView 上,把这个 ImageView 的实例传进去就可以了。
占位符
- 占位符(Placeholder)
占位符是当请求正在执行时被展示的 Drawable 。当请求成功完成时,占位符会被请求到的资源替换。如果被请求的资源是从内存中加载出来的,那么占位符可能根本不会被显示。如果请求失败并且没有设置 error Drawable ,则占位符将被持续展示。
Glide.with(this)
.load(url)
.placeholder(R.drawable.placeholder)
.into(view);
//或
Glide.with(this)
.load(url)
.placeholder(new ColorDrawable(Color.BLACK))
.into(view);
- 错误符(Error)
error Drawable 在请求永久性失败时展示。error Drawable 同样也在请求的url/model为 null ,且并没有设置 fallback Drawable 时展示。
Glide.with(this)
.load(url)
.error(R.drawable.error)
.into(view);
//或
Glide.with(this)
.load(url)
.error(new ColorDrawable(Color.RED))
.into(view);
- 后备回调符(Fallback)
fallback Drawable 在请求的url/model为 null 时展示。
Glide.with(this)
.load(url)
.fallback(R.drawable.fallback)
.into(view);
//Or:
Glide.with(this)
.load(url)
.fallback(new ColorDrawable(Color.GREY))
.into(view);
源码分析
Glide设置图片4个角为圆角
在软件开发过程中,经常遇到设置圆角的过程,有时候设置四个,有时候两个,我们就可以使用以下方法进行设置。
效果图:
image
注意:可以看到图片的上面两个角是圆角
二、代码
1. 依赖
// glide的使用
implementation 'com.github.bumptech.glide:glide:4.6.1'
annotationProcessor 'com.github.bumptech.glide:compiler:4.10.0'
2. xml布局:
<ImageView
android:id="@+id/iv_prc"
android:layout_width="match_parent"
android:layout_height="wrap_content"
/>
3. 工具类:
public class CornerTransform implements Transformation<Bitmap> {
private BitmapPool mBitmapPool;
private float radius;
private boolean exceptLeftTop, exceptRightTop, exceptLeftBottom, exceptRightBotoom;
/**
* 除了那几个角不需要圆角的
*
* @param leftTop
* @param rightTop
* @param leftBottom
* @param rightBottom
*/
public void setExceptCorner(boolean leftTop, boolean rightTop, boolean leftBottom, boolean rightBottom) {
this.exceptLeftTop = leftTop;
this.exceptRightTop = rightTop;
this.exceptLeftBottom = leftBottom;
this.exceptRightBotoom = rightBottom;
}
public CornerTransform(Context context, float radius) {
this.mBitmapPool = Glide.get(context).getBitmapPool();
this.radius = radius;
}
@Override
public Resource<Bitmap> transform(Resource<Bitmap> resource, int outWidth, int outHeight) {
Bitmap source = resource.get();
int finalWidth, finalHeight;
float ratio; //输出目标的宽高或高宽比例
if (outWidth > outHeight) { //输出宽度>输出高度,求高宽比
ratio = (float) outHeight / (float) outWidth;
finalWidth = source.getWidth();
finalHeight = (int) ((float) source.getWidth() * ratio); //固定原图宽度,求最终高度
if (finalHeight > source.getHeight()) { //求出的最终高度>原图高度,求宽高比
ratio = (float) outWidth / (float) outHeight;
finalHeight = source.getHeight();
finalWidth = (int) ((float) source.getHeight() * ratio);//固定原图高度,求最终宽度
}
} else if (outWidth < outHeight) { //输出宽度 < 输出高度,求宽高比
ratio = (float) outWidth / (float) outHeight;
finalHeight = source.getHeight();
finalWidth = (int) ((float) source.getHeight() * ratio);//固定原图高度,求最终宽度
if (finalWidth > source.getWidth()) { //求出的最终宽度 > 原图宽度,求高宽比
ratio = (float) outHeight / (float) outWidth;
finalWidth = source.getWidth();
finalHeight = (int) ((float) source.getWidth() * ratio);
}
} else { //输出宽度=输出高度
finalHeight = source.getHeight();
finalWidth = finalHeight;
}
//修正圆角
this.radius *= (float) finalHeight / (float) outHeight;
Bitmap outBitmap = this.mBitmapPool.get(finalWidth, finalHeight, Bitmap.Config.ARGB_8888);
if (outBitmap == null) {
outBitmap = Bitmap.createBitmap(finalWidth, finalHeight, Bitmap.Config.ARGB_8888);
}
Canvas canvas = new Canvas(outBitmap);
Paint paint = new Paint();
//关联画笔绘制的原图bitmap
BitmapShader shader = new BitmapShader(source, Shader.TileMode.CLAMP, Shader.TileMode.CLAMP);
//计算中心位置,进行偏移
int width = (source.getWidth() - finalWidth) / 2;
int height = (source.getHeight() - finalHeight) / 2;
if (width != 0 || height != 0) {
Matrix matrix = new Matrix();
matrix.setTranslate((float) (-width), (float) (-height));
shader.setLocalMatrix(matrix);
}
paint.setShader(shader);
paint.setAntiAlias(true);
RectF rectF = new RectF(0.0F, 0.0F, (float) canvas.getWidth(), (float) canvas.getHeight());
canvas.drawRoundRect(rectF, this.radius, this.radius, paint); //先绘制圆角矩形
if (exceptLeftTop) { //左上角不为圆角
canvas.drawRect(0, 0, radius, radius, paint);
}
if (exceptRightTop) {//右上角不为圆角
canvas.drawRect(canvas.getWidth() - radius, 0, radius, radius, paint);
}
if (exceptLeftBottom) {//左下角不为圆角
canvas.drawRect(0, canvas.getHeight() - radius, radius, canvas.getHeight(), paint);
}
if (exceptRightBotoom) {//右下角不为圆角
canvas.drawRect(canvas.getWidth() - radius, canvas.getHeight() - radius, canvas.getWidth(), canvas.getHeight(), paint);
}
return BitmapResource.obtain(outBitmap, this.mBitmapPool);
}
@Override
public String getId() {
return this.getClass().getName();
}
}
在activity或Fragment或适配器中调用:
CornerTransform transformation = new CornerTransform(context, dip2px(context, 10));
//只是绘制左上角和右上角圆角
transformation.setExceptCorner(false, false, false, false);
Glide.with(context)
.load(HttpContants.URL + list.get(position).getImage())
.asBitmap()
.skipMemoryCache(true)
.placeholder(R.drawable.background4)
.error(R.drawable.background4)
.transform(transformation)
.into(holder.ivImgShop);
还有一个方法:
public static int dip2px(Context context, float dpValue) {
final float scale = context.getResources().getDisplayMetrics().density;
return (int) (dpValue * scale + 0.5f);
}
特别注意:设置4个角为圆角的方法
image
————————————————
参考链接:https://blog.csdn.net/yijiaodingqiankun/article/details/80927281
其他框架
Glide
Glide是一款由 Bump Technologies 开发的图片加载框架,使得我们可以在 Android 平台上以极度简单的方式加载和展示图片。
参考文章:
Glide 最全解析
Glide 和 Picasso 的区别
深入理解Glide源码
Fresco
Fresco 是 Facebook 提供的开源图片加载库,它能够从网络,本地存储和 Android 资源文件中加载图片,且具有三级缓存设计(2级内存,1级文件)。Fresco 中实现了各种加载过程以及加载后的图片绘制,整体都很强大。
参考:
Fresco 的中文官网
Picasso
Picasso 是 Square 公司开源的 Android 端的图片加载和缓存框架。
参考文章:
Picasso最详细的使用指南