前提
以前都是用CircleImageView來顯示圓形頭像,近來發(fā)現(xiàn)經(jīng)常用的Glide也可以顯示圓形頭像归榕,就在項(xiàng)目中試試
坑一
Glide中API中大致找了一下沒找到霞玄,后來在網(wǎng)上搜到
bitmapTransform(new CropCircleTransformation(getContext()))
這個(gè)API可以,但是CropCircleTransformation一直不識(shí)別這個(gè)東東谆焊,后來去Glide github找到要導(dǎo)入另一個(gè)包
compile 'jp.wasabeef:glide-transformations:2.0.0'
然后就可以了惠桃,愉快的玩耍~~~~
坑二
顯示頭像以后,要更換頭像了,但是所有操作以后返回圖片剪切的地址辜王,然后開始顯示
Glide.with(this)
.load(Uri.fromFile(new File(path)))
.bitmapTransform(new CropCircleTransformation(getContext()))
.into(userIcon);
發(fā)現(xiàn)不能重復(fù)設(shè)置頭像劈狐,設(shè)置以后還是顯示第一次的頭像,但是查看剪裁的圖片已經(jīng)更換了呐馆,但是就是不顯示肥缔,后來google了一下,找到Glide默認(rèn)是有緩存的汹来,然后地址都是同一個(gè)续膳,所以顯示的是緩存的圖片也就是第一次剪裁的圖片
坑三
找到前面的問題,然后開始解決收班,清除緩存就可以了嗎
Glide.get(getContext()).clearMemory();
Glide.get(getContext()).clearDiskCache();
然后悲劇發(fā)生了坟岔,直接崩潰,google一下發(fā)現(xiàn)原來
Glide.get(getContext()).clearDiskCache();
不能再UI線程操作摔桦,并且
Glide.get(getContext()).clearMemory();
能在UI線程操作社付,所以開始解決問題:
Glide.get(getContext())
.clearMemory();
Observable.create( subscriber1 -> {
Glide.get(getContext()).clearDiskCache();
subscriber1.onNext(1);
subscriber1.onCompleted();
})
.compose(RxUtils.applyIOToMainThreadSchedulers())
.subscribe(o -> {
Glide.with(this)
.load(Uri.fromFile(new File(path)))
.bitmapTransform(new CropCircleTransformation(getContext()))
.into(userIcon);
});
至此完美解決,然后刪除build中的
compile 'de.hdodenhof:circleimageview:2.0.0'