溫馨提示×

溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊×
其他方式登錄
點(diǎn)擊 登錄注冊 即表示同意《億速云用戶服務(wù)條款》

Android快速實(shí)現(xiàn)觸摸移動的懸浮窗

發(fā)布時間:2020-09-05 15:46:27 來源:腳本之家 閱讀:284 作者:法迪 欄目:移動開發(fā)

本文實(shí)例為大家分享了Android實(shí)現(xiàn)觸摸移動的懸浮窗的具體代碼,供大家參考,具體內(nèi)容如下

1.觸摸移動的懸浮窗的快速實(shí)現(xiàn) UI

Android快速實(shí)現(xiàn)觸摸移動的懸浮窗

2. 懸浮窗實(shí)現(xiàn)

2.1 懸浮窗權(quán)限

為了偷懶,我SDK選擇比較低的17,如果不配置權(quán)限,則應(yīng)用會運(yùn)行報錯

正規(guī)流程,查詢是否有懸浮窗權(quán)限,如果沒有需要提示,并跳轉(zhuǎn)到懸浮窗設(shè)置界面“即是否允許運(yùn)行在其他應(yīng)用上面”

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

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

2.2 懸浮窗配置初始化

這里主要關(guān)注 mParams.type 和 mParams.flags 這兩個參數(shù)是核心,我們也可以使用View來加載一個布局,為了側(cè)重懸浮窗界面,我直接使用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;
      // 實(shí)現(xiàn)懸浮窗可以移動的屬性
      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;
      // 實(shí)現(xiàn)懸浮窗可以移動的屬性
      mParams.flags = WindowManager.LayoutParams.FLAG_NOT_TOUCH_MODAL | WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE;
      mParams.format = PixelFormat.TRANSPARENT;

    }
  }
}

以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持億速云。

向AI問一下細(xì)節(jié)

免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場,如果涉及侵權(quán)請聯(lián)系站長郵箱:is@yisu.com進(jìn)行舉報,并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。

AI