一、概述
提到PopWindow應該每個App中都會看到他的身影,也廣受開發這的喜歡,配上動畫效果可以實現很好的體驗,本文簡單介紹基本的使用及分析,之后會寫到不使用PopWindow實現同樣的效果;
使用:
由于比較簡單,直接上代碼了
View popupView = getLayoutInflater().inflate(R.layout.pop_window_menu, null);
mPopupWindow = new PopupWindow(popupView, ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT, true);
mPopupWindow.setTouchable(true);
mPopupWindow.setOutsideTouchable(true);
mPopupWindow.setBackgroundDrawable(new BitmapDrawable(getResources(), (Bitmap) null));
backgroundAlpha(0.5f);
mPopupWindow.showAsDropDown(btn_menu_more);
popwindowd的使用基本就上述幾個步驟:倒入布局、設置LayoutParma屬性,設置一些PopWindow的屬性,最后顯示window
mPopupWindow.setTouchable(true);
mPopupWindow.setOutsideTouchable(true);
mPopupWindow.setBackgroundDrawable(new BitmapDrawable(getResources(), (Bitmap) null));
設置這三個方法可以實現點擊任何地方,popwindow會消失。主要看看最后一個設置背景,有的同學可能認為既然是設置為null為什么還要多此一舉呢?其實恰恰時這個多此一舉才起到效果,原因之后分析
public void backgroundAlpha(float bgAlpha)
{
WindowManager.LayoutParams lp = getWindow().getAttributes();
lp.alpha = bgAlpha; //0.0-1.0
getWindow().setAttributes(lp);
}
基本每個彈出的popwindow都會將背景設置為半透明模糊的狀態,之所以再次封裝成方法,是因為如果你只在彈出的時候設置背景半透明的話,你會發現在window消失后,界面還是半透明的狀態無法消失,所以這里封裝成方法可以在消失的時候設置界面的透明度,當然要想做到這點,必須要設置小時的監聽事件
mPopupWindow.setOnDismissListener(new PopupWindow.OnDismissListener() {
@Override
public void onDismiss() {
backgroundAlpha(1f);
}
});
源碼分析
設置背景
接著剛才的問題,為什么設置背景才能時點擊任何地方的時候window消失,相信有的童鞋會想到無非是誰執行了手勢監聽的問題:
如果有背景,則會在contentView外面包一層PopupViewContainer之后作為mPopupView,如果沒有背景,則直接用contentView作為mPopupView。而這個PopupViewContainer是一個內部私有類,它繼承了FrameLayout,在其中重寫了Key和Touch事件的分發處理,由PopupView本身并沒有重寫Key和Touch事件的處理,所以如果沒有包這個外層容器類,點擊Back鍵或者外部區域是不會導致彈框消失的,簡單看下處理機制:
public boolean dispatchKeyEvent(KeyEvent event) {
if (event.getKeyCode() == KeyEvent.KEYCODE_BACK) { if (getKeyDispatcherState() == null) { return super.dispatchKeyEvent(event); }
if (event.getAction() == KeyEvent.ACTION_DOWN && event.getRepeatCount() == 0) { KeyEvent.DispatcherState state = getKeyDispatcherState();
if (state != null) { state.startTracking(event, this); } return true;
} else if (event.getAction() == KeyEvent.ACTION_UP) { KeyEvent.DispatcherState state = getKeyDispatcherState();
if (state != null && state.isTracking(event) && !event.isCanceled()) { dismiss(); return true;
}
} return super.dispatchKeyEvent(event); }
else { return super.dispatchKeyEvent(event); } }
@Override public boolean dispatchTouchEvent(MotionEvent ev) {
if (mTouchInterceptor != null && mTouchInterceptor.onTouch(this, ev)) {
return true;
} return super.dispatchTouchEvent(ev);
} @Override public boolean onTouchEvent(MotionEvent event) {
final int x = (int) event.getX(); final int y = (int) event.getY();
if ((event.getAction() == MotionEvent.ACTION_DOWN) && ((x < 0) || (x >= getWidth()) || (y < 0) || (y >= getHeight()))) {
dismiss(); return true;
} else if (event.getAction() == MotionEvent.ACTION_OUTSIDE) {
dismiss(); return true; } else { return super.onTouchEvent(event);
}
}
顯示
顯示提供了兩種形式:
showAtLocation()顯示在指定位置,有兩個方法重載:
public void showAtLocation(View parent, int gravity, int x, int y)
public void showAtLocation(IBinder token, int gravity, int x, int y)
showAsDropDown()顯示在一個參照物View的周圍,有三個方法重載:
public void showAsDropDown(View anchor) ?
public void showAsDropDown(View anchor, int xoff, int yoff)
public void showAsDropDown(View anchor, int xoff, int yoff, int gravity)
大致介紹到這,對于源碼大家簡單了解就好,知道里面實現的大概思路就好