WindowManager管理窗口示例

来源:互联网 发布:欧洲域名注册 编辑:程序博客网 时间:2024/06/15 01:16
    public static boolean speedfvIsVisible = false; //定义一个标记位当添加到layout 后标记位设为true 去掉后设为falseprivate void createSpeedFloatView() {        Log.e("dy","createSpeedFloatView...");        // 创建之前先移除避免异�?        removeSpeedFloatView();        wmSpeedParams = new WindowManager.LayoutParams();        LayoutInflater inflater = LayoutInflater.from(getApplication());        // 获取浮动窗口视图所在布局        mSpeedFloatLayout = (RelativeLayout) inflater.inflate(                R.layout.speed_float_layout, null);        llWarn = (LinearLayout) mSpeedFloatLayout.findViewById(R.id.ll_img_dis);        llSpeed = (LinearLayout) mSpeedFloatLayout.findViewById(R.id.ll_speed);        fImage = (ImageView) mSpeedFloatLayout.findViewById(R.id.iv_png);        fDistance = (TextView) mSpeedFloatLayout.findViewById(R.id.tv_distance);        fSpeed = (TextView) mSpeedFloatLayout.findViewById(R.id.tv_speed);        createFloatView(wmSpeedParams, mSpeedFloatLayout, fImage, 400, 200);        TuzhiApplication.viewSpeedFlag=true;        speedfvIsVisible = true;    }private void removeSpeedFloatView() {        if (mSpeedFloatLayout != null && mWindowManager != null) {            // 移除悬浮窗口            mWindowManager.removeView(mSpeedFloatLayout);            mSpeedFloatLayout = null;            speedfvIsVisible = false;            System.out.println("speedfloatview is remove *****************");        }    }private void createFloatView(final WindowManager.LayoutParams wmParams,            final RelativeLayout layout, final View view, final int x, final int y) {        // 设置监听浮动窗口的触摸移�?        Log.e("dy","createFloatView");        // 获取的是WindowManagerImpl.CompatModeWrapper        mWindowManager = (WindowManager) getApplication().getSystemService(                WINDOW_SERVICE);        // 设置window type        wmParams.type = LayoutParams.TYPE_PHONE;        // 设置图片格式,效果为背景透明        wmParams.format = PixelFormat.RGBA_8888;        // 设置浮动窗口不可聚焦(实现操作除浮动窗口外的其他可见窗口的操作)        wmParams.flags = LayoutParams.FLAG_NOT_FOCUSABLE;        // 调整悬浮窗显示的停靠位置为左侧置�?        wmParams.gravity = Gravity.LEFT | Gravity.TOP;        // 以屏幕左上角为原点,设置x、y初始值,相对于gravity        wmParams.x = getSharedPreferences("floativew", 0).getInt("x", x);        wmParams.y = getSharedPreferences("floativew", 0).getInt("y", y);                           // 设置悬浮窗口长宽数据        wmParams.width = WindowManager.LayoutParams.WRAP_CONTENT;        wmParams.height = WindowManager.LayoutParams.WRAP_CONTENT;        // 添加mFloatLayout        mWindowManager.addView(layout, wmParams);        layout.setOnTouchListener(new OnTouchListener() {            @Override            public boolean onTouch(View v, MotionEvent event) {                // TODO Auto-generated method stub                x2 = event.getRawX() ;                                if( MainActivity.GetUSER_ID == 11)  x2 -= 100 ;                                y2 = event.getRawY();                switch (event.getAction()) {                case MotionEvent.ACTION_MOVE:                    if(!canMove && firstTouchTime!=0 && System.currentTimeMillis()-firstTouchTime>200){                        canMove = true;                    }                    if(canMove){                        wmParams.x = (int)( x2 - mTouchStartX   ) ;// view.getMeasuredWidth()/ 2;                        wmParams.y = (int)( y2 - mTouchStartY ); // view.getMeasuredHeight()/ 2;                        // 刷新                        getSharedPreferences("floativew", 0).edit().putInt("x", wmParams.x).putInt("y", y).apply();                        mWindowManager.updateViewLayout(layout, wmParams);                                            }                                                            break;                case MotionEvent.ACTION_UP:                    if (Math.abs(x1 - x2) < 20 && Math.abs(y1 - y2) < 20) {                        Intent intent = new Intent(TuzhiService.this,                                MainActivity.class);                        intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);                        intent.putExtra(MainActivity.SHOW_DIALOG_FLAG, false);// 如果是最小化,不弹框                        startActivity(intent);                    }                    canMove = false;                    break;                                        case MotionEvent.ACTION_DOWN:                    firstTouchTime = System.currentTimeMillis();                                                            x1 = event.getRawX() ;                    if( MainActivity.GetUSER_ID == 11)  x1 -= 100 ;                                                            y1 = event.getRawY();                                                                mTouchStartX =  event.getX();                             mTouchStartY =  event.getY();                                           break;                     }                return true; // 此处必须返回false,否则OnClickListener获取不到监听            }        });    }


0 0
原创粉丝点击