android开发PopupWindow和WindowManager的使用

来源:互联网 发布:淘宝延迟收货在哪里 编辑:程序博客网 时间:2024/05/15 13:36

由于本人工作上要写一个调节屏幕参数的界面用到了windowmanager,为避免每次都要去找代码还是自己总结下来好了,有需要的同学自己下载源码,修改一下界面即可。可以用来做类似360,腾讯管家悬浮窗口菜单效果。

这些基本视图的使用在android的APIdemos里面也有,只是没那么详细而已。
SDK APIdemos路径:E:\eclipse_adt_bundlece\sdk\samples\android-19\legacy\ApiDemos\src\com\example\android\apis\view
效果图:
这里写图片描述
这里写图片描述
这里写图片描述
关键代码类:

public class PopWindowTools {    private static PopupWindow popupWindow;    public static void showPopUp(View v, Context ctx) {        LinearLayout layout = new LinearLayout(ctx);        layout.setBackgroundColor(Color.GRAY);        TextView tv = new TextView(ctx);        tv.setLayoutParams(new LayoutParams(LayoutParams.WRAP_CONTENT,                LayoutParams.WRAP_CONTENT));        tv.setText("I'm a pop -----------------------------!");        tv.setTextColor(Color.WHITE);        layout.addView(tv);        popupWindow = new PopupWindow(layout, 120, 120);        popupWindow.setFocusable(true);        popupWindow.setOutsideTouchable(true);        popupWindow.setBackgroundDrawable(new BitmapDrawable());        int[] location = new int[2];        v.getLocationOnScreen(location);        // 上边        popupWindow.showAtLocation(v, Gravity.NO_GRAVITY, location[0],        location[1] - popupWindow.getHeight());        // 下方:        // popupWindow.showAsDropDown(v);        // popupWindow.showAsDropDown(v,200,0);        // 左边        // popupWindow.showAtLocation(v, Gravity.NO_GRAVITY, location[0]        // - popupWindow.getWidth(), location[1]);        // 右边        // popupWindow.showAtLocation(v, Gravity.NO_GRAVITY,        // location[0] + v.getWidth(), location[1]);    }//   LayoutInflater mLayoutInflater = (LayoutInflater) getSystemService(LAYOUT_INFLATER_SERVICE);//   ViewGroup menuView = (ViewGroup) mLayoutInflater.inflate(R.layout.tips, null, true);//   pw = new PopupWindow(menuView, LayoutParams.WRAP_CONTENT,//   LayoutParams.WRAP_CONTENT, true);//   // 设置点击返回键使其消失,且不影响背景,此时setOutsideTouchable函数即使设置为false//   // 点击PopupWindow 外的屏幕,PopupWindow依然会消失;相反,如果不设置BackgroundDrawable//   // 则点击返回键PopupWindow不会消失,同时,即时setOutsideTouchable设置为true//   // 点击PopupWindow 外的屏幕,PopupWindow依然不会消失//   pw.setBackgroundDrawable(new ColorDrawable(Color.TRANSPARENT));//   pw.setOutsideTouchable(false); // 设置是否允许在外点击使其消失,到底有用没?//   pw.setAnimationStyle(R.style.PopupAnimation); // 设置动画//  //   // 计算x轴方向的偏移量,使得PopupWindow在Title的正下方显示,此处的单位是pixels//   int xoffInPixels =//   ScreenTools.getInstance(PopDemoActivity.this).getWidth() / 2 -//   titleName.getWidth() / 2;//   // 将pixels转为dip//   int xoffInDip =//   ScreenTools.getInstance(PopDemoActivity.this).px2dip(xoffInPixels);//   pw.showAsDropDown(titleName, -xoffInDip, 0);//   //pw.showAsDropDown(titleName);//   pw.update();//  //   TextView tv = (TextView) menuView.findViewById(R.id.tips_ok);//   tv.setOnClickListener(new View.OnClickListener() {//  //   public void onClick(View v) {//   pw.dismiss();//   }//   });}
/** * <uses-permission android:name="android.permission.SYSTEM_ALERT_WINDOW" /> */public class FloatControl implements OnClickListener {    private Context mContext;    // 单例模式    private static FloatControl mWindowUIService = null;    // 镜像    private WindowManager mWindowManager;    private LayoutParams mWindowParams;    // view    private ViewGroup mFloatViewGroup;    private FloatControl(Context mContext) {        this.mContext = mContext;    }    public static FloatControl getInstance(Context ctx) {        if (mWindowUIService == null)            mWindowUIService = new FloatControl(ctx);        return mWindowUIService;    }    private void initData() {        SpUtil sSpUtil = SpUtil.getInstance(mContext);        if (!sSpUtil.getBoolean("firstInit")) {// 没init过 就是 false            sSpUtil.setBoolean("firstInit", true);        }    }    public void initView(int positionX, int positionY, int layoutView) {        initData();        mFloatViewGroup = (ViewGroup) LayoutInflater.from(mContext).inflate(layoutView, null);        // 创建内容悬浮视图        initWindowView(positionX, positionY, LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT, true,                mFloatViewGroup);        InitSetVideoParametersView();    }    private void initWindowView(int positionX, int positionY, int width, int height, boolean touchable,            ViewGroup viewGroup) {        mWindowParams = new LayoutParams();        mWindowParams.gravity = Gravity.TOP | Gravity.LEFT;        // 调整悬浮窗显示的停靠位置为左侧置顶        mWindowParams.x = positionX;        mWindowParams.y = positionY;        mWindowParams.width = width;        mWindowParams.height = height;        /*         * 悬浮窗不可触摸,不接受任何事件,同时不影响后面的事件响应。 LayoutParams.FLAG_NOT_TOUCH_MODAL         * 不影响后面的事件 LayoutParams.FLAG_NOT_FOCUSABLE 不可聚焦         * LayoutParams.FLAG_NOT_TOUCHABLE 不可触摸         */        if (touchable) {            mWindowParams.flags = LayoutParams.FLAG_NOT_FOCUSABLE | LayoutParams.FLAG_NOT_TOUCH_MODAL;        } else {            // 不可处理click事件            mWindowParams.flags = LayoutParams.FLAG_NOT_FOCUSABLE | LayoutParams.FLAG_NOT_TOUCH_MODAL                    | LayoutParams.FLAG_NOT_TOUCHABLE;        }        // mWindowParams.type = LayoutParams.TYPE_APPLICATION;        mWindowParams.type = LayoutParams.TYPE_SYSTEM_ERROR;// error权限最大,悬浮于最上层窗口        mWindowParams.format = PixelFormat.RGBA_8888;        // mWindowParams.windowAnimations = R.anim.enter_zoom_in;        mWindowManager = (WindowManager) mContext.getSystemService(Context.WINDOW_SERVICE);        mWindowManager.addView(viewGroup, mWindowParams);    }    public void updateWindowView(int positionX, int positionY, int width, ViewGroup viewGroup) {        if (mWindowManager != null && mWindowParams != null) {            mWindowParams.x = positionX;            mWindowParams.y = positionY;            mWindowParams.width = width;            // mWindowParams.height = 0;            mWindowManager.updateViewLayout(viewGroup, mWindowParams);        }    }    public void recycleWindowSource() {        if (mFloatViewGroup != null && mWindowManager != null) {            mWindowManager.removeView(mFloatViewGroup);            mFloatViewGroup = null;            mWindowManager = null;        }    }    private int[] YUV = { R.id.btn_set_y_add, R.id.btn_set_y_sub, R.id.btn_set_u_add, R.id.btn_set_u_sub,            R.id.btn_set_v_add, R.id.btn_set_v_sub, R.id.btn_set_hue_add, R.id.btn_set_hue_sub,            R.id.btn_set_Brightness_add, R.id.btn_set_Brightness_sub, R.id.btn_set_Saturation_add,            R.id.btn_set_Saturation_sub, R.id.btn_set_Contrast_add, R.id.btn_set_Contrast_sub, R.id.btn_atc_reset,            R.id.btn_atc_close };    private TextView tvY;    private TextView tvU;    private TextView tvV;    private TextView tvHue;    private TextView tvBrightness;    private TextView tvSaturation;    private TextView tvContrast;    private void InitSetVideoParametersView() {        if (mFloatViewGroup == null) {            return;        }        Button Bt = null;        for (int i = 0; i < YUV.length; i++) {            Bt = (Button) mFloatViewGroup.findViewById(YUV[i]);            if (Bt != null) {                Bt.setOnClickListener(this);            }        }        tvY = (TextView) mFloatViewGroup.findViewById(R.id.TextView_y);        tvU = (TextView) mFloatViewGroup.findViewById(R.id.TextView_u);        tvV = (TextView) mFloatViewGroup.findViewById(R.id.TextView_v);        tvHue = (TextView) mFloatViewGroup.findViewById(R.id.TextView_set_hue);        tvBrightness = (TextView) mFloatViewGroup.findViewById(R.id.TextView_set_Brightness);        tvSaturation = (TextView) mFloatViewGroup.findViewById(R.id.TextView_set_Saturation);        tvContrast = (TextView) mFloatViewGroup.findViewById(R.id.TextView_set_Contrast);    }    @Override    public void onClick(View v) {        if (mFloatViewGroup == null) {            return;        }        int id = v.getId();        switch (id) {        case R.id.btn_set_y_add: {            break;        }        case R.id.btn_set_y_sub: {            break;        }        case R.id.btn_set_u_add: {            break;        }        case R.id.btn_set_u_sub: {            break;        }        case R.id.btn_set_v_add: {            break;        }        case R.id.btn_set_v_sub: {            break;        }        case R.id.btn_set_hue_add: {            break;        }        case R.id.btn_set_hue_sub: {            break;        }        case R.id.btn_set_Brightness_add: {            break;        }        case R.id.btn_set_Brightness_sub: {            break;        }        case R.id.btn_set_Saturation_add: {            break;        }        case R.id.btn_set_Saturation_sub: {            break;        }        case R.id.btn_set_Contrast_add: {            break;        }        case R.id.btn_set_Contrast_sub: {            break;        }        case R.id.btn_atc_reset: {            SpUtil sSpUtil = SpUtil.getInstance(mContext);            break;        }        case R.id.btn_atc_close: {            recycleWindowSource();            mContext.stopService(new Intent(mContext, TouchListenerService.class));            break;        }        }    }}

注意添加使用权限以及这两种使用区分,pop菜单是寄生于activity界面中的,受activity的生命周期影响,而windowmanager是悬浮于activity之上的。

源码点击下载