Android 簡單通用PopupWindow

自定義popupwindow幾乎是每一個程序比不可少的一項工做,github上也有不少這樣的開源庫,功能非常強大,可是有時候須要修改就比較麻煩,這裏提供一種簡潔、比較經常使用的popupwindow供你們使用。CommonPopup代碼以下git

public class CommonPopup implements PopupWindow.OnDismissListener{

    private Context mContext;
    private int resLayoutId = -1;
    private View customerView;
    private int width;
    private int height;
    private boolean focusable = true;//焦點
    private boolean touchable = true;//是否可被點擊
    private boolean clippingEnable = true;//容許彈出窗口擴展到屏幕的邊界以外
    private boolean outsideTouchable = true;//外部是否消失
    private boolean darkEnable = false;//是否背景變暗
    private float darkValue = 0;//背景變暗的值,0-1
    private int animationStyle = -1;//動畫

    private PopupWindow mPopupWindow;
    private Window mWindow;
    private PopupWindow.OnDismissListener mOnDismissListener;

    private static final float DEFAULT_ALPHA = 0.7f;

    private CommonPopup(Context context){
        mContext = context;
    }

    private PopupWindow build(){

        setCustomerView();
        setConfig();
        setOutsideTouchable();
        setDark();
        setAnimationStyle();
        mPopupWindow.update();
        return mPopupWindow;
    }

    /**
     * 設置佈局
     */
    private void setCustomerView(){
        if(customerView == null){
            customerView = LayoutInflater.from(mContext).inflate(resLayoutId,null);
        }

        if(width != 0 && height != 0 ){
            mPopupWindow = new PopupWindow(customerView,width,height);
        }else{
            mPopupWindow = new PopupWindow(customerView, ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT);
        }

        if(width == 0 || height == 0){
            mPopupWindow.getContentView().measure(View.MeasureSpec.UNSPECIFIED, View.MeasureSpec.UNSPECIFIED);
            width = mPopupWindow.getContentView().getMeasuredWidth();
            height = mPopupWindow.getContentView().getMeasuredHeight();
        }
    }

    /**
     * 設置基本配置屬性
     */
    private void setConfig(){
        mPopupWindow.setTouchable(touchable);
        mPopupWindow.setFocusable(focusable);
        mPopupWindow.setClippingEnabled(clippingEnable);
        if(mOnDismissListener != null){
            mPopupWindow.setOnDismissListener(mOnDismissListener);
        }
        mPopupWindow.setOnDismissListener(this);
    }

    /**
     * 設置點擊外部監聽,兼容6.0 outsidetorchable 失效問題
     */
    private void setOutsideTouchable(){
        if(outsideTouchable){
            mPopupWindow.setFocusable(focusable);
            mPopupWindow.setBackgroundDrawable(new ColorDrawable(Color.TRANSPARENT));
            mPopupWindow.setOutsideTouchable(outsideTouchable);
        }else{
            mPopupWindow.setFocusable(true);
            mPopupWindow.setOutsideTouchable(outsideTouchable);
            mPopupWindow.setBackgroundDrawable(null);
            mPopupWindow.getContentView().setFocusable(true);
            mPopupWindow.getContentView().setFocusableInTouchMode(true);
            mPopupWindow.getContentView().setOnKeyListener(new View.OnKeyListener() {
                @Override
                public boolean onKey(View v, int keyCode, KeyEvent event) {
                    if (keyCode == KeyEvent.KEYCODE_BACK) {
                        mPopupWindow.dismiss();
                        return true;
                    }
                    return false;
                }
            });
            mPopupWindow.setTouchInterceptor(new View.OnTouchListener() {
                @Override
                public boolean onTouch(View v, MotionEvent event) {
                    final int x = (int) event.getX();
                    final int y = (int) event.getY();
                    if ((event.getAction() == MotionEvent.ACTION_DOWN)
                            && ((x < 0) || (x >= width) || (y < 0) || (y >= height))) {
                        return true;
                    } else if (event.getAction() == MotionEvent.ACTION_OUTSIDE) {
                        return true;
                    }
                    return false;
                }
            });
        }
    }

    /**
     * 設置背景
     */
    private void setDark(){
        if(mContext != null && darkEnable){
            float alpha = (darkValue > 0 && darkValue < 1) ? darkValue : DEFAULT_ALPHA;
            mWindow = ((Activity) mContext).getWindow();
            WindowManager.LayoutParams params = mWindow.getAttributes();
            params.alpha = alpha;
            mWindow.addFlags(WindowManager.LayoutParams.FLAG_DIM_BEHIND);
            mWindow.setAttributes(params);
        }
    }

    /**
     * 設置動畫
     */
    private void setAnimationStyle(){
        if(animationStyle != -1){
            mPopupWindow.setAnimationStyle(animationStyle);
        }
    }

    /**
     * 顯示
     * @param anchor
     * @return
     */
    public CommonPopup showAsDropDown(View anchor){
        if(mPopupWindow != null){
            mPopupWindow.showAsDropDown(anchor);
        }
        return this;
    }

    /**
     * 顯示
     * @param anchor
     * @param xOff 水平
     * @param yOff 豎直
     * @return
     */
    public CommonPopup showAsDropDown(View anchor, int xOff, int yOff){
        if(mPopupWindow != null){
            mPopupWindow.showAsDropDown(anchor,xOff,yOff);
        }
        return this;
    }

    /**
     * 顯示
     * @param anchor
     * @param xOff 水平
     * @param yOff 豎直
     * @param gravity 重心
     * @return
     */
    @RequiresApi(api = Build.VERSION_CODES.KITKAT)
    public CommonPopup showAsDropDown(View anchor, int xOff, int yOff, int gravity){
        if(mPopupWindow != null){
            mPopupWindow.showAsDropDown(anchor,xOff,yOff,gravity);
        }
        return this;
    }

    /**
     * 顯示
     * @param parent
     * @param gravity
     * @param x
     * @param y
     * @return
     */
    public CommonPopup showAtLocation(View parent, int gravity, int x, int y){
        if(mPopupWindow != null){
            mPopupWindow.showAtLocation(parent,gravity,x,y);
        }
        return this;
    }

    @Override
    public void onDismiss() {
        dismiss();
    }

    /**
     * 關閉
     */
    public void dismiss(){
        if(mOnDismissListener != null){
            mOnDismissListener.onDismiss();
        }
        if(mWindow != null){
            WindowManager.LayoutParams params = mWindow.getAttributes();
            params.alpha = 1.0f;
            mWindow.setAttributes(params);
        }
        if(mPopupWindow != null && mPopupWindow.isShowing()){
            mPopupWindow.dismiss();
        }
    }

    public static class Builder{

        private CommonPopup commonPopup;

        public Builder(Context context){
            commonPopup = new CommonPopup(context);
        }

        public Builder setView(@LayoutRes int resLayoutId){
            commonPopup.resLayoutId = resLayoutId;
            commonPopup.customerView = null;
            return this;
        }

        public Builder setView(View view){
            commonPopup.customerView = view;
            commonPopup.resLayoutId = -1;
            return this;
        }

        public Builder setSize(int width,int height){
            commonPopup.width = width;
            commonPopup.height = height;
            return this;
        }

        public Builder setFocusable(boolean focusable){
            commonPopup.focusable = focusable;
            return this;
        }

        public Builder setTouchable(boolean touchable){
            commonPopup.touchable = touchable;
            return this;
        }

        public Builder setClippingEnable(boolean clippingEnable){
            commonPopup.clippingEnable = clippingEnable;
            return this;
        }

        public Builder setOutsideTouchable(boolean outsideTouchable){
            commonPopup.outsideTouchable = outsideTouchable;
            return this;
        }

        public Builder setDarkEable(boolean darkEnable){
            commonPopup.darkEnable = darkEnable;
            return this;
        }

        public Builder setDarkValue(float darkValue){
            commonPopup.darkValue = darkValue;
            return this;
        }

        public Builder setAnimationStyle(int animationStyle){
            commonPopup.animationStyle = animationStyle;
            return this;
        }

        public Builder setOnDissmissListener(PopupWindow.OnDismissListener onDismissListener){
            commonPopup.mOnDismissListener = onDismissListener;
            return this;
        }

        public CommonPopup create(){
            commonPopup.build();
            return commonPopup;
        }
    }
}
複製代碼

CommonPopup 使用方法以下github

new CommonPopup.Builder(MainActivity.this)
                        .setView(customerView)
                        .setSize(ScreenUtils.dp2px(200),ScreenUtils.dp2px(100))
                        .setFocusable(true)
                        .setTouchable(true)
                        .setClippingEnable(true)
                        .setOutsideTouchable(false)
                        .setDarkEable(true)
                        .setDarkValue(0.5f)
                        .create()
                        .showAsDropDown(tvRead,50,-200);
複製代碼
相關文章
相關標籤/搜索