494_桌面悬浮窗







桌面悬浮窗




public class SmallFloatWindowView extends LinearLayout {


    public int viewWidth;
    public int viewHeight;
    //记录手指按下时在小悬浮窗的View上的横坐标的值
    private float xInView;
    //记录手指按下时在小悬浮窗的View上的纵坐标的值
    private float yInView;
    //记录当前手指位置在屏幕上的横坐标值
    private float xInScreen;
    //记录当前手指位置在屏幕上的纵坐标值
    private float yInScreen;
    //记录手指按下时在屏幕上的横坐标的值
    private float xDownInScreen;
    //记录手指按下时在屏幕上的纵坐标的值
    private float yDownInScreen;
    private Context ctx;
    private final WindowManager windowManager;


    public SmallFloatWindowView(Context context) {
        super(context);
        this.ctx = context;
        windowManager = (WindowManager) context.getSystemService(Context.WINDOW_SERVICE);
        LayoutInflater.from(context).inflate(R.layout.view_float_window_small, this);
        View view = findViewById(R.id.ll_window);
        viewWidth = view.getLayoutParams().width;
        viewHeight = view.getLayoutParams().height;
    }


    //==============================================================================================
    //onTouchEvent方法
    @Override
    public boolean onTouchEvent(MotionEvent event) {
        switch (event.getAction()) {
            case MotionEvent.ACTION_DOWN:
                // 手指按下时记录必要数据,纵坐标的值都需要减去状态栏高度
                xInView = event.getX();
                yInView = event.getY();
                xDownInScreen = event.getRawX();
                yDownInScreen = event.getRawY() - DensityUtil.getStatusBarHeight(ctx);
                xInScreen = event.getRawX();
                yInScreen = event.getRawY() - DensityUtil.getStatusBarHeight(ctx);
                break;
            case MotionEvent.ACTION_MOVE:
                xInScreen = event.getRawX();
                yInScreen = event.getRawY() - DensityUtil.getStatusBarHeight(ctx);
                // 更新小悬浮窗的位置
                updateViewPosition();
                break;
            case MotionEvent.ACTION_UP:
                // 如果手指离开屏幕时,xDownInScreen和xInScreen相等,且yDownInScreen和yInScreen相等,则视为触发了单击事件。
                if (xDownInScreen == xInScreen && yDownInScreen == yInScreen) {
//                    openBigWindow();
                }
                break;
            default:
                break;
        }
        return super.onTouchEvent(event);
    }


    //==============================================================================================
    //更新小悬浮窗的位置
    private void updateViewPosition() {
        WindowManager.LayoutParams layoutParams = (WindowManager.LayoutParams) getLayoutParams();
        layoutParams.x = (int) (xInScreen - xInView);
        layoutParams.y = (int) (yInScreen - yInView);
        windowManager.updateViewLayout(this, layoutParams);
    }
}








public class FloatWindowService extends Service {


    //==============================================================================================
    //复写onBind
    @Nullable
    @Override
    public IBinder onBind(Intent intent) {
        return null;
    }


    //==============================================================================================
    //在onStart方法里创建小浮窗
    @Override
    public int onStartCommand(Intent intent, int flags, int startId) {


        //创建小悬浮窗
        createSmallFloatWindow();


        return super.onStartCommand(intent, flags, startId);
    }


    //==============================================================================================
    //创建小浮窗
    private void createSmallFloatWindow() {
        //拿到WindowManager
        WindowManager windowManager = (WindowManager) this.getSystemService(Context.WINDOW_SERVICE);
        //拿到屏幕宽高
        int screenWidth = windowManager.getDefaultDisplay().getWidth();
        int screenHeight = windowManager.getDefaultDisplay().getHeight();
        //创建小浮窗
        SmallFloatWindowView smallFloatWindowView = new SmallFloatWindowView(this);
        //拿到LayoutParams,设置属性
        WindowManager.LayoutParams layoutParams = new WindowManager.LayoutParams();
        layoutParams.type = WindowManager.LayoutParams.TYPE_PHONE;
        layoutParams.format = PixelFormat.RGBA_8888;
        layoutParams.flags = WindowManager.LayoutParams.FLAG_NOT_TOUCH_MODAL
                | WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE;
        layoutParams.gravity = Gravity.LEFT | Gravity.TOP;
        layoutParams.width = smallFloatWindowView.viewWidth;
        layoutParams.height = smallFloatWindowView.viewHeight;
        layoutParams.x = screenWidth;
        layoutParams.y = screenHeight / 2;
        //把小浮窗添加到window里面
        windowManager.addView(smallFloatWindowView, layoutParams);
    }
}





评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值