簡介
Glide 是一個(gè)快速高效的 Android 圖片加載庫,注重于平滑的滾動。
開始
- 在 app/build.gradle 文件當(dāng)中添加如下依賴:
dependencies {
compile 'com.github.bumptech.glide:glide:4.8.0';
}
- 在 AndroidManifest.xml 添加網(wǎng)絡(luò)權(quán)限:
<uses-permission android:name="android.permission.INTERNET" />
使用
- 基本用法
多數(shù)情況下,使用Glide加載圖片非常簡單,一行代碼足矣:
Glide.with(this) .load(url) .into(imageView);
解析一下這行代碼:首先,調(diào)用Glide.with()方法用于創(chuàng)建一個(gè)加載圖片的實(shí)例。with()方法可以接收Context、Activity或者Fragment類型的參數(shù)。
其次,load()方法用于指定待加載的圖片資源。Glide支持加載各種各樣的圖片資源,包括網(wǎng)絡(luò)圖片、本地圖片、應(yīng)用資源、二進(jìn)制流、Uri對象等等。
// 加載本地圖片
File file = new File(getExternalCacheDir() + "/image.jpg");
Glide.with(this).load(file).into(imageView);
// 加載應(yīng)用資源
int resource = R.drawable.image;
Glide.with(this).load(resource).into(imageView);
// 加載二進(jìn)制流
byte[] image = getImageBytes();
Glide.with(this).load(image).into(imageView);
// 加載Uri對象
Uri imageUri = getImageUri();
Glide.with(this).load(imageUri).into(imageView);
- 最后,into() 方法,我們希望讓圖片顯示在哪個(gè) ImageView 上,把這個(gè) ImageView 的實(shí)例傳進(jìn)去就可以了。
占位符
- 占位符(Placeholder)
占位符是當(dāng)請求正在執(zhí)行時(shí)被展示的 Drawable 。當(dāng)請求成功完成時(shí),占位符會被請求到的資源替換。如果被請求的資源是從內(nèi)存中加載出來的,那么占位符可能根本不會被顯示。如果請求失敗并且沒有設(shè)置 error Drawable ,則占位符將被持續(xù)展示。
Glide.with(this)
.load(url)
.placeholder(R.drawable.placeholder)
.into(view);
//或
Glide.with(this)
.load(url)
.placeholder(new ColorDrawable(Color.BLACK))
.into(view);
- 錯(cuò)誤符(Error)
error Drawable 在請求永久性失敗時(shí)展示。error Drawable 同樣也在請求的url/model為 null ,且并沒有設(shè)置 fallback Drawable 時(shí)展示。
Glide.with(this)
.load(url)
.error(R.drawable.error)
.into(view);
//或
Glide.with(this)
.load(url)
.error(new ColorDrawable(Color.RED))
.into(view);
- 后備回調(diào)符(Fallback)
fallback Drawable 在請求的url/model為 null 時(shí)展示。
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設(shè)置圖片4個(gè)角為圓角
在軟件開發(fā)過程中,經(jīng)常遇到設(shè)置圓角的過程,有時(shí)候設(shè)置四個(gè),有時(shí)候兩個(gè),我們就可以使用以下方法進(jìn)行設(shè)置。
效果圖:

注意:可以看到圖片的上面兩個(gè)角是圓角
二、代碼
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;
/**
* 除了那幾個(gè)角不需要圓角的
*
* @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; //輸出目標(biāo)的寬高或高寬比例
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();
//關(guān)聯(lián)畫筆繪制的原圖bitmap
BitmapShader shader = new BitmapShader(source, Shader.TileMode.CLAMP, Shader.TileMode.CLAMP);
//計(jì)算中心位置,進(jìn)行偏移
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或適配器中調(diào)用:
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);
還有一個(gè)方法:
public static int dip2px(Context context, float dpValue) {
final float scale = context.getResources().getDisplayMetrics().density;
return (int) (dpValue * scale + 0.5f);
}
特別注意:設(shè)置4個(gè)角為圓角的方法

————————————————
參考鏈接:https://blog.csdn.net/yijiaodingqiankun/article/details/80927281
其他框架
Glide
Glide是一款由 Bump Technologies 開發(fā)的圖片加載框架,使得我們可以在 Android 平臺上以極度簡單的方式加載和展示圖片。
參考文章:
Glide 最全解析
Glide 和 Picasso 的區(qū)別
深入理解Glide源碼
Fresco
Fresco 是 Facebook 提供的開源圖片加載庫,它能夠從網(wǎng)絡(luò),本地存儲和 Android 資源文件中加載圖片,且具有三級緩存設(shè)計(jì)(2級內(nèi)存,1級文件)。Fresco 中實(shí)現(xiàn)了各種加載過程以及加載后的圖片繪制,整體都很強(qiáng)大。
參考:
Fresco 的中文官網(wǎng)
Picasso
Picasso 是 Square 公司開源的 Android 端的圖片加載和緩存框架。
參考文章:
Picasso最詳細(xì)的使用指南