上文說到啤挎,一個(gè)單獨(dú)的頁面實(shí)現(xiàn)毛玻璃
但是目前,這種方案放在了recyclerview中,就會(huì)有性能問題了,為什么脉顿?recyclerview因?yàn)槠浼虞d數(shù)據(jù)的緩存性質(zhì)糕档,會(huì)同時(shí)加載多個(gè)view熬的,如果所有view都在做動(dòng)態(tài)毛玻璃萍聊,那性能開銷可想而知。
T梦觥J俳啊!代碼地址在文末G看鳌Mっ!
環(huán)境:
1骑歹、win10 androidstudio4.4.0
2预烙、jdk 1.8
場景:
recyclerview使用了linearlayoutmanager,pagersnaphelper分頁滑動(dòng)道媚。
思路:
如果減少性能開銷扁掸?有一下方法:
(一)用戶滑動(dòng)recyclerview的時(shí)候,暫停動(dòng)態(tài)毛玻璃
(二)毛玻璃控件失去焦點(diǎn)的時(shí)候最域,暫停毛玻璃
(三)非當(dāng)前頁顯示時(shí)谴分,暫停毛玻璃
實(shí)現(xiàn):
(一)通過定義一些對(duì)象,分別記錄當(dāng)前用戶的滑動(dòng)位置镀脂,滑動(dòng)狀態(tài)(滑動(dòng)中牺蹄,停止滑動(dòng))等蝴悉。再結(jié)合毛玻璃在recyclerview中的位置船逮,進(jìn)行比對(duì)實(shí)現(xiàn)序无。核心代碼如下:
if(getmAdapterPos() == LibPicBlurDragConstant.getInstance().getDragPos()){
if(LibPicBlurDragConstant.getInstance().isDrag()){
return false;
}
}
上面代碼的意思就是仗岖,如果拖動(dòng)位置和當(dāng)前毛玻璃控件位置相等且拖動(dòng)中,就暫停毛玻璃行為最筒。
(二)失去監(jiān)聽的判斷晦鞋,通過以下代碼即可捕獲:
@Override
public void onWindowFocusChanged(boolean hasWindowFocus) {
super.onWindowFocusChanged(hasWindowFocus);
Log.d(TAG, "onWindowFocusChanged hasWindowFocus: " + hasWindowFocus + " mIdentify: " + mIdentify);
if (hasWindowFocus) {
setCanBlur(true);
} else {
setCanBlur(false);
}
}
(三)由于毛玻璃的位置谋币,是通過windowlocation方法獲取的暑竟,所以斋射,只要得出離屏位置,即可得出是否在當(dāng)前頁面光羞。注意<;炒蟆纱兑!
調(diào)用該庫中,需要自定義一個(gè)父類布局(recyclerview場景)化借,替換為原來的Window DecoreView,因?yàn)檫@里涉及的是一個(gè)動(dòng)態(tài)繪制顯示的問題潜慎,所以要替換。核心代碼如下:
離屏位置判斷:
final int[] locations = new int[2];
// parentView.getLocationOnScreen(locations);
// Log.d(TAG, "checkScreenLocation " + mIdentify + " parentSc: " + locations[1] + " parentHe: " + parentView.getMeasuredHeight());
getLocationOnScreen(locations);
int childScreenHeight = locations[1];
// Log.d(TAG, "checkScreenLocation " + mIdentify + " childSc: " + locations[1] + " childHe: " + getMeasuredHeight());
//離屏位置----------------------------------------------------------
if (childScreenHeight <= 0) {
//上一頁
return false;
}
if (childScreenHeight >= parentView.getMeasuredHeight()) {
return false;
}
//離屏位置----------------------------------------------------------
自定義decoreview設(shè)置:
protected View getActivityDecorView() {
if(mCusDecorView!=null){
return mCusDecorView;
}
Context ctx = getContext();
for (int i = 0; i < 4 && !(ctx instanceof Activity) && ctx instanceof ContextWrapper; i++) {
ctx = ((ContextWrapper) ctx).getBaseContext();
}
if (ctx instanceof Activity) {
return ((Activity) ctx).getWindow().getDecorView();
} else {
return null;
}
}
public void setmCusDecorView(View mCusDecorView) {
this.mCusDecorView = mCusDecorView;
}