最近在專案中遇到了一個奇怪的問題,Glide和CircleImageView一起使用載入圓形頭像,發現第一次死活都載入出來,出來的是一張佔點陣圖,當你重新整理的時候或者第二次進入的時候才能載入出來。究其原因,CircleImageView 把位置佔了。這時候我們有如下4種解決方案,不管是哪一種都是可以解決的(親測可行)。
1. 不使用佔位符
註釋掉這兩句程式碼即可。
.placeholder(R.drawable.normal_photo)
.error(R.drawable.normal_photo)複製程式碼
Glide 載入時的程式碼:
Glide.with(mContext)
.load(datas.getUser_img())
.centerCrop()
.into(ivAvator);複製程式碼
此時XML中的還是CircleImageView,程式碼如下:
<de.hdodenhof.circleimageview.CircleImageView
android:id="@+id/iv_avator"
android:layout_width="130px"
android:layout_height="130px"
android:src="@drawable/normal_photo" />複製程式碼
2. 不使用預設動畫
新增一句程式碼即可:
.dontAnimate()//防止設定placeholder導致第一次不顯示網路圖片,只顯示預設圖片的問題複製程式碼
此時Glide載入時的完整程式碼:
Glide.with(mContext)
.load(datas.getUser_img())
.centerCrop()
.dontAnimate()//防止設定placeholder導致第一次不顯示網路圖片,只顯示預設圖片的問題
.error(R.drawable.normal_photo)
.placeholder(R.drawable.normal_photo)
.into(ivAvator);複製程式碼
此時XML中的依然是CircleImageView,這沒什麼好說的。程式碼如下:
<de.hdodenhof.circleimageview.CircleImageView
android:id="@+id/iv_avator"
android:layout_width="130px"
android:layout_height="130px"
android:src="@drawable/normal_photo" />複製程式碼
3. 使用glide本身的圓形載入方式
這裡就直接看下Glide載入時的程式碼,注意:
此時的ivAvator可以使用普通的ImageView,不必再引入CircleImageView第三方框架。(當然你依然可以寫成CircleImageView)
asBitmap() 這句不能少,否則下面的方法會報錯。
Glide.with(mContext) .load(datas.getUser_img()) .asBitmap() //這句不能少,否則下面的方法會報錯 .centerCrop() .into(new BitmapImageViewTarget(ivAvator) { @Override protected void setResource(Bitmap resource) { RoundedBitmapDrawable circularBitmapDrawable = RoundedBitmapDrawableFactory.create(getResources(), resource); circularBitmapDrawable.setCircular(true); ivAvator.setImageDrawable(circularBitmapDrawable); } });複製程式碼
此時xml中的程式碼修改成ImageView,程式碼如下:
<ImageView
android:id="@+id/iv_avator"
android:layout_width="130px"
android:layout_height="130px"
android:src="@drawable/normal_photo" />複製程式碼
4. 同樣使用Glide本身的圓形載入方式
這種方式和上面的基本類似。首先GlideCircleTransform繼承BitmapTransformation,程式碼如下:
//圓形圖片
public class GlideCircleTransform extends BitmapTransformation {
public GlideCircleTransform(Context context) {
super(context);
}
@Override protected 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 String getId() {
return getClass().getName();
}
}複製程式碼
Glide載入時的程式碼如下:
Glide.with(mContext)
.load(datas.getUser_img())
.centerCrop()
.error(R.drawable.normal_photo)
.placeholder(R.drawable.normal_photo)
.transform(new GlideCircleTransform(mContext))
.into(ivAvator);複製程式碼
注意:此時的ivAvator依然可以是ImageView(當然你依然可以寫成CircleImageView)。程式碼如下:
<ImageView
android:id="@+id/iv_avator"
android:layout_width="130px"
android:layout_height="130px"
android:src="@drawable/normal_photo" />複製程式碼