91超碰碰碰碰久久久久久综合_超碰av人澡人澡人澡人澡人掠_国产黄大片在线观看画质优化_txt小说免费全本

溫馨提示×

溫馨提示×

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

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

詳解Android如何實現可任意拖動的懸浮窗功能

發布時間:2020-07-21 10:19:19 來源:億速云 閱讀:130 作者:小豬 欄目:移動開發

小編這次要給大家分享的是詳解Android如何實現可任意拖動的懸浮窗功能,文章內容豐富,感興趣的小伙伴可以來了解一下,希望大家閱讀完這篇文章之后能夠有所收獲。

最近開發項目中,有個在屏幕上任意拖動的懸浮窗功能,其實就是利用 WindowManager的api來完成這個需求,具體的實現的功能如下:
1.自定義view

import android.content.Context;
import android.content.Intent;
import android.os.Handler;
import android.os.Message;
import android.util.Log;
import android.util.TypedValue;
import android.view.MotionEvent;
import android.view.View;
import android.view.ViewConfiguration;
import android.view.WindowManager;
import android.widget.LinearLayout;
import com.xinrui.recordscreen.R;
import java.lang.reflect.Field;
/**
 *
 */
public class RecordScreenView extends LinearLayout implements View.OnClickListener{
  private WindowManager mWindowManager;
  private WindowManager.LayoutParams mLayoutParams;
  private long mLastDownTime;
  private float mLastDownX;
  private float mLastDownY;
  private boolean mIsLongTouch;
  private boolean mIsTouching;
  private float mTouchSlop;
  private final static long LONG_CLICK_LIMIT = 20;
  private final static int TIME_COUNT = 0;
  private int mStatusBarHeight;
  private int mCurrentMode,time=0;
  private final static int MODE_NONE = 0x000;
  private final static int MODE_MOVE = 0x001;
  private int mOffsetToParent;
  private int mOffsetToParentY;
  private Context mContext;
  public RecordScreenView(Context context) {
    super(context);
    this.mContext=context;
    mWindowManager = (WindowManager) getContext().getSystemService(Context.WINDOW_SERVICE);
    initView();
  }
  private void initView() {
    View view = inflate(getContext(), R.layout.layout_ball, this);
    mTouchSlop = ViewConfiguration.get(getContext()).getScaledTouchSlop();
    mCurrentMode = MODE_NONE;
    recordtime(0);
    mStatusBarHeight = getStatusBarHeight();
    mOffsetToParent = dip2px(25);
    mOffsetToParentY = mStatusBarHeight + mOffsetToParent;
    view.setOnTouchListener(new OnTouchListener() {
      @Override
      public boolean onTouch(View v, final MotionEvent event) {
        switch (event.getAction()) {
          case MotionEvent.ACTION_DOWN:
            mIsTouching = true;
            mLastDownTime = System.currentTimeMillis();
            mLastDownX = event.getX();
            mLastDownY = event.getY();
            postDelayed(new Runnable() {
              @Override
              public void run() {
                if (isLongTouch()) {
                  mIsLongTouch = true;
                }
              }
            }, LONG_CLICK_LIMIT);
            break;
          case MotionEvent.ACTION_MOVE:
            if (!mIsLongTouch && isTouchSlop(event)) {
              return true;
            }
            if (mIsLongTouch && (mCurrentMode == MODE_NONE || mCurrentMode == MODE_MOVE)) {
              mLayoutParams.x = (int) (event.getRawX() - mOffsetToParent);
              mLayoutParams.y = (int) (event.getRawY() - mOffsetToParentY);
              mWindowManager.updateViewLayout(RecordScreenView.this, mLayoutParams);//不斷刷新懸浮窗的位置
              mCurrentMode = MODE_MOVE;
            }
            break;
          case MotionEvent.ACTION_CANCEL:
          case MotionEvent.ACTION_UP:
            mIsTouching = false;
            if (mIsLongTouch) {
              mIsLongTouch = false;
            }
            mCurrentMode = MODE_NONE;
            break;
        }
        return true;
      }
    });
  }
  private boolean isLongTouch() {
    long time = System.currentTimeMillis();
    if (mIsTouching && mCurrentMode == MODE_NONE && (time - mLastDownTime >= LONG_CLICK_LIMIT)) {
      return true;
    }
    return false;
  }
  /**
   * 判斷是否是輕微滑動
   *
   * @param event
   * @return
   */
  private boolean isTouchSlop(MotionEvent event) {
    float x = event.getX();
    float y = event.getY();
    if (Math.abs(x - mLastDownX) < mTouchSlop && Math.abs(y - mLastDownY) < mTouchSlop) {
      return true;
    }
    return false;
  }
  public void setLayoutParams(WindowManager.LayoutParams params) {
    mLayoutParams = params;
  }
  /**
   * 獲取通知欄高度
   *
   * @return
   */
  private int getStatusBarHeight() {
    int statusBarHeight = 0;
    try {
      Class<&#63;> c = Class.forName("com.android.internal.R$dimen");
      Object o = c.newInstance();
      Field field = c.getField("status_bar_height");
      int x = (Integer) field.get(o);
      statusBarHeight = getResources().getDimensionPixelSize(x);
    } catch (Exception e) {
      e.printStackTrace();
    }
    return statusBarHeight;
  }
  public int dip2px(float dip) {
    return (int) TypedValue.applyDimension(
        TypedValue.COMPLEX_UNIT_DIP, dip, getContext().getResources().getDisplayMetrics()
    );
  }
}

2.添加windowManager添加view

import android.content.Context;
import android.graphics.PixelFormat;
import android.view.Gravity;
import android.view.WindowManager;
import android.view.WindowManager.LayoutParams;

/**
 * Created by wangxiandeng on 2016/11/25.
 */

public class FloatWindowManager {
  private static RecordScreenView mBallView;

  private static WindowManager mWindowManager;


  public static void addBallView(Context context) {
    if (mBallView == null) {
      WindowManager windowManager = getWindowManager(context);
      int screenWidth = windowManager.getDefaultDisplay().getWidth();
      int screenHeight = windowManager.getDefaultDisplay().getHeight();
      mBallView = new RecordScreenView(context);
      LayoutParams params = new LayoutParams();
      params.x = screenWidth/2;
      params.y = screenHeight/2+150;
      params.width = LayoutParams.WRAP_CONTENT;
      params.height = LayoutParams.WRAP_CONTENT;
      params.gravity = Gravity.LEFT | Gravity.TOP;
      params.type = LayoutParams.TYPE_APPLICATION_OVERLAY;
      params.format = PixelFormat.RGBA_8888;
      params.flags = LayoutParams.FLAG_NOT_TOUCH_MODAL
          | LayoutParams.FLAG_NOT_FOCUSABLE;
      mBallView.setLayoutParams(params);
      windowManager.addView(mBallView, params);
    }
  }

  public static void removeBallView(Context context) {
    if (mBallView != null) {
      WindowManager windowManager = getWindowManager(context);
      windowManager.removeView(mBallView);
      mBallView = null;
    }
  }

  private static WindowManager getWindowManager(Context context) {
    if (mWindowManager == null) {
      mWindowManager = (WindowManager) context.getSystemService(Context.WINDOW_SERVICE);
    }
    return mWindowManager;
  }
}

3.Acitivity中調用

import android.app.Activity;
import android.content.Intent;
import android.os.Build;
import android.os.Bundle;
import android.provider.Settings;
import android.util.Log;
import android.widget.Toast;

import com.xinrui.recordscreen.view.FloatWindowManager;

public class MainActivity extends Activity {

  @Override
  protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    if (Build.VERSION.SDK_INT >= 23) {
    //設置中請求開啟懸浮窗權限
      if (!Settings.canDrawOverlays(this)) {
        Intent intent = new Intent(Settings.ACTION_MANAGE_OVERLAY_PERMISSION);
        intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
        startActivity(intent);
        Toast.makeText(this, MainActivity.this.getResources().getString(R.string.open_float), Toast.LENGTH_SHORT).show();
      }else{
        initView();
      }
    }
  }

  private void initView() {
    FloatWindowManager.addBallView(MainActivity.this);
    finish();
  }
}

5.AndroidManifest.xml

<&#63;xml version="1.0" encoding="utf-8"&#63;>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
  package="com.xinrui.recordscreen">
  <uses-permission android:name="android.permission.INTERNET"/>
  <uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE"/>
  <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
  <uses-permission android:name="android.permission.SYSTEM_ALERT_WINDOW"/>//懸浮窗權限
  <application
    android:allowBackup="true"
    android:icon="@drawable/recording_screen_nor"
    android:label="@string/app_name"
    android:supportsRtl="true">
    <activity android:name="com.xinrui.recordscreen.MainActivity">
      <intent-filter>
        <action android:name="android.intent.action.MAIN" />

        <category android:name="android.intent.category.LAUNCHER" />
      </intent-filter>
    </activity>
</manifest>

看完這篇關于詳解Android如何實現可任意拖動的懸浮窗功能的文章,如果覺得文章內容寫得不錯的話,可以把它分享出去給更多人看到。

向AI問一下細節

免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。

AI

新宁县| 枣庄市| 齐河县| 沿河| 紫阳县| 军事| 汉川市| 遂昌县| 都兰县| 巴塘县| 双辽市| 遵义市| 麻阳| 颍上县| 平乐县| 大埔区| 屏南县| 鹿邑县| 麻城市| 云梦县| 聊城市| 定远县| 哈尔滨市| 竹山县| 建宁县| 嘉祥县| 临泉县| 阿克| 湄潭县| 饶平县| 尤溪县| 且末县| 岑溪市| 墨江| 罗源县| 永靖县| 色达县| 内丘县| 利川市| 水城县| 宝兴县|