• Android 悬浮窗口


    public class MainActivity extends Activity {
        private WindowManager mWindowManager;
        private WindowManager.LayoutParams mLayout;
        private DesktopLayout mDesktopLayout;
        private long startTime;
        // 声明屏幕的宽高
        float x, y;
        int top;
    
        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_main);        
            createWindowManager();
            createDesktopLayout();
            Button btn = (Button) findViewById(R.id.btn);
            btn.setOnClickListener(new OnClickListener() {
                public void onClick(View v) {
                    showDesk();
                }
            });
        }
        /**
         * 创建悬浮窗体
         */
        private void createDesktopLayout() {
            mDesktopLayout = new DesktopLayout(this);
            mDesktopLayout.setOnTouchListener(new OnTouchListener() {
                float mTouchStartX;
                float mTouchStartY;
    
                @Override
                public boolean onTouch(View v, MotionEvent event) {
                    // 获取相对屏幕的坐标,即以屏幕左上角为原点
                    x = event.getRawX();
                    y = event.getRawY() - top; // 25是系统状态栏的高度
                    Log.i("startP", "startX" + mTouchStartX + "====startY"
                            + mTouchStartY);
                    switch (event.getAction()) {
                    case MotionEvent.ACTION_DOWN:
                        // 获取相对View的坐标,即以此View左上角为原点
                        mTouchStartX = event.getX();
                        mTouchStartY = event.getY();
                        Log.i("startP", "startX" + mTouchStartX + "====startY"
                                + mTouchStartY);
                        long end = System.currentTimeMillis() - startTime;
                        // 双击的间隔在 300ms以下
                        if (end < 300) {
                            closeDesk();
                        }
                        startTime = System.currentTimeMillis();
                        break;
                    case MotionEvent.ACTION_MOVE:
                        // 更新浮动窗口位置参数
                        mLayout.x = (int) (x - mTouchStartX);
                        mLayout.y = (int) (y - mTouchStartY);
                        mWindowManager.updateViewLayout(v, mLayout);
                        break;
                    case MotionEvent.ACTION_UP:
    
                        // 更新浮动窗口位置参数
                        mLayout.x = (int) (x - mTouchStartX);
                        mLayout.y = (int) (y - mTouchStartY);
                        mWindowManager.updateViewLayout(v, mLayout);
    
                        // 可以在此记录最后一次的位置
    
                        mTouchStartX = mTouchStartY = 0;
                        break;
                    }
                    return true;
                }
            });
        }
    
        @Override
        public void onWindowFocusChanged(boolean hasFocus) {
            super.onWindowFocusChanged(hasFocus);
            Rect rect = new Rect();
            // /取得整个视图部分,注意,如果你要设置标题样式,这个必须出现在标题样式之后,否则会出错
            getWindow().getDecorView().getWindowVisibleDisplayFrame(rect);
            top = rect.top;//状态栏的高度,所以rect.height,rect.width分别是系统的高度的宽度
    
            Log.i("top",""+top);
        }
    
        /**
         * 显示DesktopLayout
         */
        private void showDesk() {
            mWindowManager.addView(mDesktopLayout, mLayout);
            finish();
        }
    
        /**
         * 关闭DesktopLayout
         */
        private void closeDesk() {
            mWindowManager.removeView(mDesktopLayout);
            finish();
        }
    
        /**
         * 设置WindowManager
         */
        private void createWindowManager() {
            // 取得系统窗体
            mWindowManager = (WindowManager) getApplicationContext()
                    .getSystemService("window");
    
            // 窗体的布局样式
            mLayout = new WindowManager.LayoutParams();
    
            // 设置窗体显示类型――TYPE_SYSTEM_ALERT(系统提示)
            mLayout.type = WindowManager.LayoutParams.TYPE_SYSTEM_ALERT;
    
            // 设置窗体焦点及触摸:
            // FLAG_NOT_FOCUSABLE(不能获得按键输入焦点)
            mLayout.flags = WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE;
    
            // 设置显示的模式
            mLayout.format = PixelFormat.RGBA_8888;
    
            // 设置对齐的方法
            mLayout.gravity = Gravity.TOP | Gravity.LEFT;
    
            // 设置窗体宽度和高度
            mLayout.width = WindowManager.LayoutParams.WRAP_CONTENT;
            mLayout.height = WindowManager.LayoutParams.WRAP_CONTENT;
    
        }
    
    }
    public class DesktopLayout extends LinearLayout {
    
        public DesktopLayout(Context context) {
            super(context);
            setOrientation(LinearLayout.VERTICAL);// 水平排列
            
    
            //设置宽高
            this.setLayoutParams( new LayoutParams(LayoutParams.WRAP_CONTENT,
                    LayoutParams.WRAP_CONTENT));
            
            View view = LayoutInflater.from(context).inflate(  
                    R.layout.desklayout, null); 
            this.addView(view);
        }
    
    }

    别忘了在AndroidManifest.xml中加入如下的权限:

    public class DesktopLayout extends LinearLayout {
    
        public DesktopLayout(Context context) {
            super(context);
            setOrientation(LinearLayout.VERTICAL);// 水平排列
            
    
            //设置宽高
            this.setLayoutParams( new LayoutParams(LayoutParams.WRAP_CONTENT,
                    LayoutParams.WRAP_CONTENT));
            
            View view = LayoutInflater.from(context).inflate(  
                    R.layout.desklayout, null); 
            this.addView(view);
        }
    
    }
  • 相关阅读:
    xyplorer设置备忘
    如何在CentOS 8上安装Python2 Python3
    为CentOS 8操作系统安装MySQL的方法,以安装MySQL 8为例
    SSH登录服务器报ECDSA host key "ip地址" for has changed and you have requested strict checking
    Linux常用命令大全
    转载:php的几种常用的数据交换格式
    转:GBK编码 VS UTF8编码
    转载:中文在UTF8和GBK编码中的范围
    转:SDL Specification and Description Language 简介
    转:Java中Split函数的用法技巧
  • 原文地址:https://www.cnblogs.com/jasonkent27/p/5076594.html
Copyright © 2020-2023  润新知