WindowManager管理窗口示例


    public static boolean speedfvIsVisible = false; //定义一个标记位当添加到layout 后标记位设为true 去掉后设为false
private 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
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值