欢迎来到代码驿站!

Android代码

当前位置:首页 > 移动开发 > Android代码

Android快速实现触摸移动的悬浮窗

时间:2020-10-09 13:10:49|栏目:Android代码|点击:

本文实例为大家分享了Android实现触摸移动的悬浮窗的具体代码,供大家参考,具体内容如下

1.触摸移动的悬浮窗的快速实现 UI

2. 悬浮窗实现

2.1 悬浮窗权限

为了偷懒,我SDK选择比较低的17,如果不配置权限,则应用会运行报错

正规流程,查询是否有悬浮窗权限,如果没有需要提示,并跳转到悬浮窗设置界面“即是否允许运行在其他应用上面”

<uses-sdk
  android:minSdkVersion="17"
  android:targetSdkVersion="17" />

<uses-permission android:name="android.permission.SYSTEM_ALERT_WINDOW" />

2.2 悬浮窗配置初始化

这里主要关注 mParams.type 和 mParams.flags 这两个参数是核心,我们也可以使用View来加载一个布局,为了侧重悬浮窗界面,我直接使用TextView演示

private void initFloatWindow() {

    if (mShow == null) {
      mWm = (WindowManager) getSystemService(Context.WINDOW_SERVICE);

      mShow = new TextView(this);
      mShow.setText("I Love You");
      mShow.setTextColor(Color.RED);
      mShow.setTextSize(50);

      mShow.setOnTouchListener(new View.OnTouchListener() {
        int lastX = 0;
        int lastY = 0;
        int paramX = 0;
        int paramY = 0;

        @Override
        public boolean onTouch(View view, MotionEvent motionEvent) {
          switch (motionEvent.getAction()) {
          case MotionEvent.ACTION_DOWN:
            lastX = (int) motionEvent.getRawX();
            lastY = (int) motionEvent.getRawY();
            paramX = mParams.x;
            paramY = mParams.y;
            break;
          case MotionEvent.ACTION_MOVE:
            int dx = (int) motionEvent.getRawX() - lastX;
            int dy = (int) motionEvent.getRawY() - lastY;
            mParams.x = paramX + dx;
            mParams.y = paramY + dy;
            mWm.updateViewLayout(mShow, mParams);
            break;
          }
          return true;
        }
      });

      mParams = new WindowManager.LayoutParams();
      mParams.width = WindowManager.LayoutParams.WRAP_CONTENT;
      mParams.height = WindowManager.LayoutParams.WRAP_CONTENT;
      mParams.type = WindowManager.LayoutParams.TYPE_SYSTEM_ALERT;
      // 实现悬浮窗可以移动的属性
      mParams.flags = WindowManager.LayoutParams.FLAG_NOT_TOUCH_MODAL | WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE;
      mParams.format = PixelFormat.TRANSPARENT;

    }
  }

2.3 悬浮窗显示

mWm.addView(mShow, mParams);

2.4 悬浮窗隐藏

mWm.removeView(mShow);

2.5 完整示例代码

package com.wu.sufadi;

import android.app.Activity;
import android.content.Context;
import android.graphics.Color;
import android.graphics.PixelFormat;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.view.LayoutInflater;
import android.view.MotionEvent;
import android.view.View;
import android.view.WindowManager;
import android.widget.TextView;

public class MainActivity extends Activity {

  private TextView mShow;

  private WindowManager mWm;
  private WindowManager.LayoutParams mParams;
  private static final int MSG_SHOW_WINDOW = 1;
  private static final int MSG_HIDE_WINDOW = 2;

  @Override
  protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    initFloatWindow();
  }

  public void click_show(View view) {
    showFloatWindow();
  }

  public void click_hide(View view) {
    hideFloatWindow();
  }

  Handler handler = new Handler() {

    @Override
    public void handleMessage(Message msg) {
      super.handleMessage(msg);
      switch (msg.what) {
      case MSG_SHOW_WINDOW:
        mWm.addView(mShow, mParams);
        break;
      case MSG_HIDE_WINDOW:
        mWm.removeView(mShow);
        break;
      default:
        break;
      }
    }
  };

  private void showFloatWindow() {
    handler.sendEmptyMessage(MSG_SHOW_WINDOW);
  }

  private void hideFloatWindow() {
    handler.sendEmptyMessage(MSG_HIDE_WINDOW);
  }

  private void initFloatWindow() {

    if (mShow == null) {
      mWm = (WindowManager) getSystemService(Context.WINDOW_SERVICE);

      mShow = new TextView(this);
      mShow.setText("I Love You");
      mShow.setTextColor(Color.RED);
      mShow.setTextSize(50);

      mShow.setOnTouchListener(new View.OnTouchListener() {
        int lastX = 0;
        int lastY = 0;
        int paramX = 0;
        int paramY = 0;

        @Override
        public boolean onTouch(View view, MotionEvent motionEvent) {
          switch (motionEvent.getAction()) {
          case MotionEvent.ACTION_DOWN:
            lastX = (int) motionEvent.getRawX();
            lastY = (int) motionEvent.getRawY();
            paramX = mParams.x;
            paramY = mParams.y;
            break;
          case MotionEvent.ACTION_MOVE:
            int dx = (int) motionEvent.getRawX() - lastX;
            int dy = (int) motionEvent.getRawY() - lastY;
            mParams.x = paramX + dx;
            mParams.y = paramY + dy;
            mWm.updateViewLayout(mShow, mParams);
            break;
          }
          return true;
        }
      });

      mParams = new WindowManager.LayoutParams();
      mParams.width = WindowManager.LayoutParams.WRAP_CONTENT;
      mParams.height = WindowManager.LayoutParams.WRAP_CONTENT;
      mParams.type = WindowManager.LayoutParams.TYPE_SYSTEM_ALERT;
      // 实现悬浮窗可以移动的属性
      mParams.flags = WindowManager.LayoutParams.FLAG_NOT_TOUCH_MODAL | WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE;
      mParams.format = PixelFormat.TRANSPARENT;

    }
  }
}

上一篇:Android Studio项目中导入开源库的方法

栏    目:Android代码

下一篇:android layout 按比例布局的代码

本文标题:Android快速实现触摸移动的悬浮窗

本文地址:http://www.codeinn.net/misctech/8930.html

推荐教程

广告投放 | 联系我们 | 版权申明

重要申明:本站所有的文章、图片、评论等,均由网友发表或上传并维护或收集自网络,属个人行为,与本站立场无关。

如果侵犯了您的权利,请与我们联系,我们将在24小时内进行处理、任何非本站因素导致的法律后果,本站均不负任何责任。

联系QQ:914707363 | 邮箱:codeinn#126.com(#换成@)

Copyright © 2020 代码驿站 版权所有