詳解Android中Glide與CircleImageView加載圓形圖片的問(wèn)題
最近在項(xiàng)目中遇到了一個(gè)奇怪的問(wèn)題,Glide和CircleImageView一起使用加載圓形頭像,發(fā)現(xiàn)第一次死活都加載出來(lái),出來(lái)的是一張占位圖,當(dāng)你刷新的時(shí)候或者第二次進(jìn)入的時(shí)候才能加載出來(lái)。究其原因,CircleImageView 把位置占了。這時(shí)候我們有如下4種解決方案,不管是哪一種都是可以解決的(親測(cè)可行)。
1. 不使用占位符
注釋掉這兩句代碼即可。
.placeholder(R.drawable.normal_photo)
.error(R.drawable.normal_photo)
Glide 加載時(shí)的代碼:
Glide.with(mContext)
.load(datas.getUser_img())
.centerCrop()
.into(ivAvator);
此時(shí)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. 不使用默認(rèn)動(dòng)畫
添加一句代碼即可:
.dontAnimate()//防止設(shè)置placeholder導(dǎo)致第一次不顯示網(wǎng)絡(luò)圖片,只顯示默認(rèn)圖片的問(wèn)題
此時(shí)Glide加載時(shí)的完整代碼:
Glide.with(mContext)
.load(datas.getUser_img())
.centerCrop()
.dontAnimate()//防止設(shè)置placeholder導(dǎo)致第一次不顯示網(wǎng)絡(luò)圖片,只顯示默認(rèn)圖片的問(wèn)題
.error(R.drawable.normal_photo)
.placeholder(R.drawable.normal_photo)
.into(ivAvator);
此時(shí)XML中的依然是CircleImageView,這沒(méi)什么好說(shuō)的。代碼如下:
<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加載時(shí)的代碼,注意:
此時(shí)的ivAvator可以使用普通的ImageView,不必再引入CircleImageView第三方框架。(當(dāng)然你依然可以寫成CircleImageView)
asBitmap() 這句不能少,否則下面的方法會(huì)報(bào)錯(cuò)。
Glide.with(mContext)
.load(datas.getUser_img())
.asBitmap() //這句不能少,否則下面的方法會(huì)報(bào)錯(cuò)
.centerCrop()
.into(new BitmapImageViewTarget(ivAvator) {
@Override
protected void setResource(Bitmap resource) {
RoundedBitmapDrawable circularBitmapDrawable =
RoundedBitmapDrawableFactory.create(getResources(), resource);
circularBitmapDrawable.setCircular(true);
ivAvator.setImageDrawable(circularBitmapDrawable);
}
});
此時(shí)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加載時(shí)的代碼如下:
Glide.with(mContext)
.load(datas.getUser_img())
.centerCrop()
.error(R.drawable.normal_photo)
.placeholder(R.drawable.normal_photo)
.transform(new GlideCircleTransform(mContext))
.into(ivAvator);
注意: 此時(shí)的ivAvator依然可以是ImageView(當(dāng)然你依然可以寫成CircleImageView) 。代碼如下:
<ImageView
android:id="@+id/iv_avator"
android:layout_width="130px"
android:layout_height="130px"
android:src="@drawable/normal_photo" />
以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
- Android裁剪圖片為圓形圖片的實(shí)現(xiàn)原理與代碼
- Android中Glide加載圓形圖片和圓角圖片實(shí)例代碼
- Android實(shí)現(xiàn)圓形圖片的兩種方式
- 分享一個(gè)Android設(shè)置圓形圖片的特別方法
- Android編程繪制圓形圖片的方法
- Android自定義View實(shí)現(xiàn)旋轉(zhuǎn)的圓形圖片
- android繪制圓形圖片的兩種方式示例
- Android實(shí)現(xiàn)圓形圖片或者圓角圖片
- Android使用自定義ImageView實(shí)現(xiàn)圓形圖片效果
- Android實(shí)現(xiàn)圓形圖片效果
相關(guān)文章
kotlin協(xié)程之coroutineScope函數(shù)使用詳解
這篇文章主要為大家介紹了kotlin協(xié)程之coroutineScope函數(shù)使用詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2022-09-09
Android編程實(shí)現(xiàn)TextView垂直自動(dòng)滾動(dòng)功能【附demo源碼下載】
這篇文章主要介紹了Android編程實(shí)現(xiàn)TextView垂直自動(dòng)滾動(dòng)功能,詳細(xì)分析了Android TextView垂直自動(dòng)滾動(dòng)功能的實(shí)現(xiàn)步驟與布局、功能相關(guān)技巧,并附帶了demo源碼供讀者下載,需要的朋友可以參考下2017-02-02
Android RecyclerView的Item自定義動(dòng)畫及DefaultItemAnimator源碼分析
這篇文章主要介紹了Android RecyclerView的Item自定義動(dòng)畫及DefaultItemAnimator源碼,感興趣的小伙伴們可以參考一下2016-07-07
Android編程之Application設(shè)置全局變量及傳值用法實(shí)例分析
這篇文章主要介紹了Android編程之Application設(shè)置全局變量及傳值用法,結(jié)合實(shí)例形式較為詳細(xì)的分析了全局變量及傳值的相關(guān)技巧,具有一定參考借鑒價(jià)值,需要的朋友可以參考下2015-12-12
Android CheckBox中設(shè)置padding無(wú)效解決辦法
這篇文章主要介紹了Android CheckBox中設(shè)置padding無(wú)效解決辦法的相關(guān)資料,希望通過(guò)本文能幫助到大家,讓大家解決這樣類似的問(wèn)題,需要的朋友可以參考下2017-10-10
android自定義view之實(shí)現(xiàn)日歷界面實(shí)例
本篇文章主要介紹了android自定義view之實(shí)現(xiàn)日歷界面實(shí)例,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下。2017-03-03
詳解Flutter中網(wǎng)絡(luò)框架dio的二次封裝
其實(shí)dio框架已經(jīng)封裝的很好了,但是在實(shí)戰(zhàn)項(xiàng)目中,為了項(xiàng)目可以統(tǒng)一管理,還是需要對(duì)dio框架進(jìn)行二次封裝。本文將詳細(xì)講解一下dio如何二次封裝,需要的可以參考一下2022-04-04
Android實(shí)現(xiàn)簡(jiǎn)易的柱狀圖和曲線圖表實(shí)例代碼
柱狀圖是統(tǒng)計(jì)圖表中經(jīng)常用到的一種圖表,比如降雨量之類的統(tǒng)計(jì)展示。這篇文章主要給大家介紹了關(guān)于利用Android如何實(shí)現(xiàn)簡(jiǎn)易的柱狀圖和曲線圖表的相關(guān)資料,文中通過(guò)示例代碼介紹的非常詳細(xì),需要的朋友可以參考下。2017-12-12

