PopupWindow简化使用

 
 

使用builder模式设置popupwindow的显示属性

import android.app.Activity;
import android.content.Context;
import android.graphics.drawable.Drawable;;
import android.util.DisplayMetrics;
import android.view.Display;
import android.view.Gravity;
import android.view.View;
import android.view.WindowManager;
import android.widget.PopupWindow;

import java.lang.reflect.InvocationHandler;
import java.lang.reflect.Method;
import java.lang.reflect.Proxy;

/**
 * Created by Administrator on 2018/4/9.
 */
public class CustomWindow {

    /**
     *设置popwindow显示的位置
     */

    public static  final int    SHOW_TOP=0;

    public static  final int    SHOW_BOTTOM=1;

    public static  final  int    SHOW_LEFT=2;

    public static  final  int     SHOW_RIGHT=3;

    public static  final  int    SHOW_CENTER=4;

    public static  final  int   SHOW_ANCHOR_VIEW=5;


    public  static View  getViewByLayout(Context  context,int layoutId){
        return  ((Activity) context).getLayoutInflater().inflate(layoutId,null);
    }

    public final static   class   Build{
        public  PopupWindow  build( PopupWindowBuilder  builder){
            return builder.getMyPopWindow();
        }
    }


    public final static class   PopupWindowBuilder  {

        private PopupWindow mYPopupWindow;

        private  Context    mContext;

        //popwindow锚定控件
        private View       mAnchor;

        //基于锚定点pop在x轴 y轴的偏移量
        private int        offsetX,offsetY;

        private int         w,h;

        public  PopupWindowBuilder(Context context,View  layout_pop,View  anchor){
            this.mContext=context;
            this.mAnchor=anchor;
             mYPopupWindow  =new PopupWindow(layout_pop);
             w=getScreenHeight(mContext)/2;
             h=getScreenWidth(mContext);
             mYPopupWindow.setHeight(h);
             mYPopupWindow.setWidth(w);
        }


        PopupWindow  getMyPopWindow(){
            if (mYPopupWindow==null)
                mYPopupWindow=new PopupWindow();
            return  mYPopupWindow;
        }

        /**
            <p>
             设置popwindown宽高
            </p>
         */
       public PopupWindowBuilder builderWidth(int w) {
            this.w=w;
            mYPopupWindow.setWidth(w);
            return this;
        }

       public PopupWindowBuilder builderHeight(int h) {
           this.h=h;
           mYPopupWindow.setHeight(h);
           return this;
        }

       public PopupWindowBuilder builderFocusable(boolean f) {
                mYPopupWindow.setFocusable(f);
                return this;
        }


       public  PopupWindowBuilder builderOutsideTouchable(boolean f) {
                mYPopupWindow.setOutsideTouchable(f);
                return this;
        }


        /**
         * ctrl+t  查看当前类的子类
          <p>
           设置背景,在外部点击消失必须的属性
            Drawable  drawable=new ColorDrawable(Color.parseColor("#00ff00"));
           Drawable  drawable=new ShapeDrawable();
         </p>
         */
        public PopupWindowBuilder builderBackgroundDrawable(Drawable background) {
                  mYPopupWindow.setBackgroundDrawable(background);
                  return this;
        }

        /**
            <p>是否允滑出屏幕外部</p>
         */
       public PopupWindowBuilder builderClippingEnabled(boolean f) {
            mYPopupWindow.setClippingEnabled(f);
            return this;
        }

        /**
         * 设置显示与消失动画
         <p>
         <style name="AnimHorizontal" parent="@android:style/Animation">
         <item name="android:windowEnterAnimation">@anim/push_tran_bottom_in</item>
         <item name="android:windowExitAnimation">@anim/push_tran_top_out</item>
         </style>
         </p>
         */

       public PopupWindowBuilder builderAnimationStyle(int style) {
                   mYPopupWindow.setAnimationStyle(style);
                   return this;
        }


        /**
          <p>有edittext的时候弹起软键盘控件自动上移</p>
         */
       public PopupWindowBuilder builderSoftInputMode() {
                mYPopupWindow.setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_ADJUST_RESIZE);
                return this;
        }


        /**
         * @param a   背景透明度
         */

       public PopupWindowBuilder builderShowBackgroundAlpha(float a) {
                  setBackgroundAlpha(a,mContext);
                  return this;
        }


      public PopupWindowBuilder builderDismissBackgroundAlpha(final float a) {
               mYPopupWindow.setOnDismissListener(new PopupWindow.OnDismissListener() {
                   @Override
                   public void onDismiss() {
                       setBackgroundAlpha(a,mContext);
                   }
               });
               return this;
        }



        public PopupWindowBuilder  builderDismiss(){
           if (mYPopupWindow!=null&&mYPopupWindow.isShowing()){
               mYPopupWindow.dismiss();
           }
           return this;
        }


        public PopupWindowBuilder   builderXoffset(int offsetX){
            this.offsetX=offsetX;
            return this;
        }


        public  PopupWindowBuilder  builderYoffset(int offsetY){
            this.offsetY=offsetY;
            return this;
        }

        /**
         <p>
         默认x轴y轴的偏移量是0,如果需要设置便宜,需要提前调用偏移构建
         </p>
         */
       public PopupWindowBuilder builderLocation(int location) {
                if (location==SHOW_BOTTOM){
                    mYPopupWindow.showAtLocation(mAnchor, Gravity.BOTTOM,offsetX,offsetY);
                }else if(location==SHOW_TOP){
                    mYPopupWindow.showAtLocation(mAnchor,Gravity.TOP,offsetX,offsetY);
                }else  if(location==SHOW_LEFT){
                    mYPopupWindow.showAtLocation(mAnchor,Gravity.LEFT,offsetX,offsetY);
                }else  if(location==SHOW_RIGHT){
                    mYPopupWindow.showAtLocation(mAnchor,Gravity.RIGHT,offsetX,offsetY);
                }else  if(location==SHOW_CENTER){
                    mYPopupWindow.showAtLocation(mAnchor,Gravity.CENTER,offsetX,offsetY);
                } else if(location==SHOW_ANCHOR_VIEW){
                    mYPopupWindow.showAsDropDown(mAnchor,offsetX,offsetY);
                }
           return this;
        }

    }

    /**
      <p>设置屏幕的透明度</p>
    */
    private static void setBackgroundAlpha(float bgAlpha, Context  context) {
        WindowManager.LayoutParams lp = ((Activity) context).getWindow().getAttributes();
        lp.alpha = bgAlpha;
        ((Activity) context).getWindow().setAttributes(lp);
    }


    public static int getScreenWidth(Context context) {
        WindowManager manager = (WindowManager) context.getSystemService(Context.WINDOW_SERVICE);
        DisplayMetrics metrics = new DisplayMetrics();
        assert manager != null;
        Display defaultDisplay = manager.getDefaultDisplay();
        defaultDisplay.getMetrics(metrics);
        return metrics.widthPixels;
    }

    public static int getScreenHeight(Context context) {
        WindowManager manager = (WindowManager) context.getSystemService(Context.WINDOW_SERVICE);
        DisplayMetrics metrics = new DisplayMetrics();
        assert manager != null;
        manager.getDefaultDisplay().getMetrics(metrics);
        return metrics.heightPixels;
    }

}
 
 

使用方法:

private  void  showPop(){
   
    View viewByLayout = CustomWindow.getViewByLayout(this, R.layout.pop_layout);

    CustomWindow.PopupWindowBuilder builder=new CustomWindow.PopupWindowBuilder(this,viewByLayout,linearLayout);

    builder.builderLocation(CustomWindow.SHOW_BOTTOM);
    

}



猜你喜欢

转载自blog.csdn.net/fengchengwu2012/article/details/79867344