zoukankan      html  css  js  c++  java
  • android项目浮窗的移动

    wm = (WindowManager) this.getSystemService(Context.WINDOW_SERVICE);
            final WindowManager.LayoutParams params = new WindowManager.LayoutParams();
            params.height = WindowManager.LayoutParams.WRAP_CONTENT;
            params.width = WindowManager.LayoutParams.WRAP_CONTENT;
            params.flags = WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE
                    | WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON;
            params.format = PixelFormat.TRANSLUCENT;
            params.type = WindowManager.LayoutParams.TYPE_PHONE;
            params.gravity = Gravity.LEFT+Gravity.TOP;
            params.setTitle("Toast");
            SharedPreferences sharedPreferences = getSharedPreferences("xy", 0);
            int backX = sharedPreferences.getInt("x", 0);
            int backY = sharedPreferences.getInt("y", 0);
            //是以默认的gravity为原点的,默认的是center这里我们要重新设置gravity
            params.x = backX;
            params.y = backY;
            view = new TextView(this);
            view.setText(address);
            view.setTextSize(30f);
            view.setTextColor(Color.WHITE);
            view.setOnTouchListener(new OnTouchListener() {
                
                private int startX;
                private int startY;
    
                @Override
                public boolean onTouch(View arg0, MotionEvent event) {
                    switch (event.getAction()) {
                    case MotionEvent.ACTION_DOWN:
                        startX = (int) event.getRawX();
                        startY = (int) event.getRawY();
                        break;
                    case MotionEvent.ACTION_MOVE:
                        int endX = (int) event.getRawX();
                        int endY = (int) event.getRawY();
    
                        // 计算移动偏移量
                        int dx = endX - startX;
                        int dy = endY - startY;
                        
                        // 更新浮窗位置
                        params.x += dx;
                        params.y += dy;
                        wm.updateViewLayout(view, params);
                        startX = (int) event.getRawX();
                        startY = (int) event.getRawY();
                        break;
                    case MotionEvent.ACTION_UP:
                        
                        break;
    
                    default:
                        break;
                    }
                    return true;
                }
            });
  • 相关阅读:
    6 原型模式
    10 观察者模式
    4 代理模式
    写错误日志
    C#事件的使用
    将int型数字转换成7位字符串,不足的时候,前面补0
    Excel 2010导数据到SQL SERVER 2008
    jquery checkbox
    修改注册表开启IE跨域访问功能
    存储过程一例
  • 原文地址:https://www.cnblogs.com/84126858jmz/p/5022033.html
Copyright © 2011-2022 走看看