zoukankan      html  css  js  c++  java
  • 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);
        }
    
    }
  • 相关阅读:
    PAT (Basic Level) Practise (中文)-1033. 旧键盘打字(20)
    PAT (Basic Level) Practise (中文)-1034. 有理数四则运算(20)
    PAT (Basic Level) Practise (中文)-1035. 插入与归并(25)
    PAT (Basic Level) Practise (中文)-1036. 跟奥巴马一起编程(15)
    PAT (Basic Level) Practise (中文)-1037. 在霍格沃茨找零钱(20)
    PAT (Basic Level) Practise (中文)-1038. 统计同成绩学生(20)
    PAT (Basic Level) Practise (中文)-1039. 到底买不买(20)
    PAT (Basic Level) Practise (中文)-1040. 有几个PAT(25)
    Git 笔记
    Object-c中的属性和成员变量的关系详解
  • 原文地址:https://www.cnblogs.com/jasonkent27/p/5076594.html
Copyright © 2011-2022 走看看