亚洲乱码中文字幕综合,中国熟女仑乱hd,亚洲精品乱拍国产一区二区三区,一本大道卡一卡二卡三乱码全集资源,又粗又黄又硬又爽的免费视频

Android實(shí)現(xiàn)系統(tǒng)級(jí)懸浮按鈕

 更新時(shí)間:2017年03月16日 16:11:50   作者:Tim0815  
這篇文章主要為大家詳細(xì)介紹了Android實(shí)現(xiàn)系統(tǒng)級(jí)懸浮按鈕的相關(guān)資料,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下

本文實(shí)例為大家分享了Android系統(tǒng)級(jí)懸浮按鈕的具體代碼,供大家參考,具體內(nèi)容如下

具體的需求

1、就是做一個(gè)系統(tǒng)級(jí)的懸浮按鈕,就像iPhone 桌面的那個(gè)懸浮按鈕效果一樣,能隨意拖動(dòng),并且手一放開(kāi),懸浮按鈕就自動(dòng)靠邊。
2、可以點(diǎn)擊并且可以隨意拖動(dòng)。
3、懸浮按鈕自動(dòng)靠邊的時(shí)候,或者移動(dòng)到邊上的時(shí)候,自動(dòng)隱藏半邊。
4、橫豎屏切換都兼容

1、就在WindowManager 里面添加View,這個(gè)View通過(guò)自定義控件來(lái)實(shí)現(xiàn)。
2、在onTouch里的MotionEvent.ACTION_MOVE事件里頭,通過(guò)控制懸浮按鈕的具體坐標(biāo)來(lái)實(shí)現(xiàn)隨意移動(dòng)。
3、在onTouch里的MotionEvent.ACTION_UP事件里頭,來(lái)控制懸浮按鈕自動(dòng)靠邊,并且自動(dòng)隱藏半邊,不過(guò)在這里onTouch和onClick這兩個(gè)事件是一起觸發(fā)的,不過(guò)這也有解決辦法,你可以在手放開(kāi)的瞬間,通過(guò)移動(dòng)的距離,來(lái)決定是否觸發(fā)點(diǎn)擊事件,,如果返回false,就會(huì)觸發(fā)點(diǎn)擊事件,如果返回true就會(huì)觸發(fā)點(diǎn)擊事件
4、通過(guò)自定義控件onLayout方法,來(lái)捕獲橫豎屏切換事件,
5、還有一個(gè)靠哪邊停靠的問(wèn)題,通過(guò)坐標(biāo)來(lái)判讀更靠近哪一邊。就靠哪邊停靠。
![以中間這個(gè)中心點(diǎn)為準(zhǔn),以更短的X軸畫(huà)一個(gè)正方形]

下面是具體實(shí)現(xiàn)代碼:

import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Point;
import android.graphics.Rect;
import android.util.AttributeSet;
import android.view.MotionEvent;
import android.view.View;
import android.view.WindowManager;
import android.widget.ImageView;

import com.iapppay.openid.channel.LoginResultCallback;
import com.iapppay.openid.channel.OpenIDApplication;
import com.iapppay.openid.channel.util.DisplayUtil;
import com.iapppay.openid.channel.util.LogUtil;
import com.iapppay.openid.channel.util.Res;

/**
 * Created by HuangTiebing 2017/2/14.
 */

public class DragFloatActionButton extends ImageView implements View.OnTouchListener, View.OnClickListener {

  public static String TAG = "DragFloatActionButton";
  private Context context;

  float lastX, lastY;
  float originX, originY;
  int screenWidth;
  int screenHeight;
  private int originWidth;

  private WindowManager windowManager;
  //  // 此windowManagerParams變量為獲取的全局變量,用以保存懸浮窗口的屬性
  private WindowManager.LayoutParams windowManagerParams;

  private LoginResultCallback resultCallback; //懸浮按鈕點(diǎn)擊回調(diào)

  public DragFloatActionButton(Context context, boolean isForceLogin, LoginResultCallback resultCallback) {
    this(context, null);
    OpenIDApplication.getInstance().setForceLogin(isForceLogin);
    this.resultCallback = resultCallback;
  }

  public DragFloatActionButton(Context context, AttributeSet attrs) {
    this(context, attrs, 0);
  }

  public DragFloatActionButton(Context context, AttributeSet attrs, int defStyleAttr) {
    super(context, attrs, defStyleAttr);
    this.context = context;

    Point screenSize = DisplayUtil.getScreenSize(context);
    screenWidth = screenSize.x;
    screenHeight = screenSize.y;
    setImageResource(Res.drawable(context, "ipay_float_btn_bg"));
    setOnTouchListener(this);
    setOnClickListener(this);

    windowManager = (WindowManager) getContext().getApplicationContext().getSystemService(Context.WINDOW_SERVICE);
  }

  public int getOriginWidth() {
    return originWidth;
  }

  public void setOriginWidth(int originWidth) {
    this.originWidth = originWidth;
  }

  @Override
  public boolean onTouch(View v, MotionEvent event) {
    windowManagerParams = (WindowManager.LayoutParams) this.getLayoutParams();
    //獲取到狀態(tài)欄的高度
    Rect frame = new Rect();
    getWindowVisibleDisplayFrame(frame);
    int ea = event.getAction();
    switch (ea) {
      case MotionEvent.ACTION_DOWN:
        lastX = event.getRawX();// 獲取觸摸事件觸摸位置的原始X坐標(biāo)
        lastY = event.getRawY();
        originX = lastX;
        originY = lastY;
        break;
      case MotionEvent.ACTION_MOVE:
        float dx = event.getRawX() - lastX;
        float dy = event.getRawY() - lastY;
        windowManagerParams.x += dx;
        windowManagerParams.y += dy;
        LogUtil.d(TAG, "移動(dòng)距離:dx=" + dx + ",dy=" + dy);
        showAllBtn();
        lastX = (int) event.getRawX();
        lastY = (int) event.getRawY();
        break;
      case MotionEvent.ACTION_UP:
        float lastMoveDx = Math.abs(event.getRawX() - originX);
        float lastMoveDy = Math.abs(event.getRawY() - originY);
        LogUtil.d(TAG, "松開(kāi)時(shí),移動(dòng)距離:lastMoveDx=" + lastMoveDx + ", lastMoveDy=" + lastMoveDy);
        if (lastMoveDx < 10 && lastMoveDy < 10) { //移動(dòng)距離太小,視為點(diǎn)擊,
          return false;
        } else {
          updateViewLayout(event);
          isFirstClick = true;
          return true;
        }
    }
    return false;
  }

  /**
   * 顯示整個(gè)圖標(biāo)
   */
  public void showAllBtn() {
    windowManagerParams.width = originWidth;
    windowManagerParams.height = originWidth;
    setImageResource(Res.drawable(context, "ipay_float_btn_bg"));
    windowManager.updateViewLayout(this, windowManagerParams); // 刷新顯示
  }

  /**
   * 懸浮按鈕顯示在左邊
   */
  private void showInLeft() {
    windowManagerParams.x = 0;
    windowManagerParams.width = originWidth / 2;
    windowManagerParams.height = originWidth;
    setImageResource(Res.drawable(context, "ipay_float_btn_left_hidden"));
    windowManager.updateViewLayout(this, windowManagerParams); // 刷新顯示
  }

  /**
   * 懸浮按鈕顯示在右邊
   */
  private void showInRight() {
    windowManagerParams.width = originWidth / 2;
    windowManagerParams.height = originWidth;
    windowManagerParams.x = screenWidth - windowManagerParams.width;
    setImageResource(Res.drawable(context, "ipay_float_btn_right_hidden"));
    windowManager.updateViewLayout(this, windowManagerParams); // 刷新顯示
  }

  /**
   * 懸浮按鈕顯示在上面
   */
  private void showInTop() {
    windowManagerParams.y = 0;
    windowManagerParams.width = originWidth;
    windowManagerParams.height = originWidth / 2;
    setImageResource(Res.drawable(context, "ipay_float_btn_top_hidden"));
    windowManager.updateViewLayout(this, windowManagerParams); // 刷新顯示
  }

  /**
   * 懸浮按鈕顯示在下面
   */
  private void showInBottom() {
    windowManagerParams.width = originWidth;
    windowManagerParams.height = originWidth / 2;
    windowManagerParams.y = screenHeight - windowManagerParams.width;
    setImageResource(Res.drawable(context, "ipay_float_btn_bottom_hidden"));
    windowManager.updateViewLayout(this, windowManagerParams); // 刷新顯示
  }

  /**
   * 更新懸浮圖標(biāo)
   *
   * @param event 手動(dòng)移動(dòng)事件
   */
  public void updateViewLayout(MotionEvent event) {
    Point center = new Point(screenWidth / 2, screenHeight / 2); //屏幕中心點(diǎn)
    float xOffset, yOffset;//以屏幕中心點(diǎn)為原點(diǎn),X軸和Y軸上的偏移量
    if (event != null) {//手動(dòng)移動(dòng)的
      xOffset = event.getRawX() - center.x;
      yOffset = event.getRawY() - center.y;
    } else {//自動(dòng)隱藏
      xOffset = lastX - center.x;
      yOffset = lastY - center.y;
    }
    if (Math.abs(xOffset) >= Math.abs(yOffset)) {//向左或向右縮進(jìn)隱藏
      if (xOffset <= 0) { //向左縮進(jìn)
        showInLeft();
      } else {
        showInRight();
      }
    } else {//向上或向下縮進(jìn)隱藏
      if (yOffset <= 0) {//向上縮進(jìn)
        showInTop();
      } else {
        showInBottom();
      }
    }
  }

  @Override
  protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
    super.onMeasure(widthMeasureSpec, heightMeasureSpec);
  }

  @Override
  protected void onLayout(boolean changed, int left, int top, int right, int bottom) {
    super.onLayout(changed, left, top, right, bottom);
    Point screenSize = DisplayUtil.getScreenSize(context);
    if (screenWidth != screenSize.x) {//屏幕旋轉(zhuǎn)切換
      screenWidth = screenSize.x;
      screenHeight = screenSize.y;
      lastY = windowManagerParams.x;
      lastX = windowManagerParams.y;
      windowManagerParams.x = (int) lastX;
      windowManagerParams.y = (int) lastY;
      updateViewLayout(null);
    }
  }

  private boolean isFirstClick = true;

  @Override
  protected void onDraw(Canvas canvas) {
    super.onDraw(canvas);
  }

  @Override
  public void onClick(View v) {
    LogUtil.d(TAG, "執(zhí)行點(diǎn)擊事件");
    if (!isFirstClick) {
      OpenIDApplication.getInstance().floatBtnClick(context, OpenIDApplication.getInstance().isForceLogin(), resultCallback);
    } else {//半隱藏狀態(tài),點(diǎn)擊顯示全部
      isFirstClick = false;
      showAllBtn();
    }
  }

}

調(diào)用實(shí)現(xiàn)代碼,這里注意有個(gè)問(wèn)題,彈出系統(tǒng)級(jí)的懸浮窗,需要配置權(quán)限:

并且Android 6.0以上的手機(jī),還要彈出對(duì)話框問(wèn)用戶是否運(yùn)行,如果這個(gè)用戶拒絕了,就不能彈出系統(tǒng)級(jí)的懸浮窗了,還有個(gè)別手機(jī)廠商修改了android源碼,還需要進(jìn)系統(tǒng)設(shè)置里去允許這個(gè)應(yīng)用彈出懸浮窗。這樣的話就體驗(yàn)感非常不好,不過(guò)這里有個(gè)小技巧,按下面方式設(shè)置為toast類型就完全解決,既不用配置權(quán)限,也不彈出窗來(lái)向用戶獲取權(quán)限,完全解決問(wèn)題。

WindowManager.LayoutParams windowManagerParams = new WindowManager.LayoutParams(WindowManager.LayoutParams.TYPE_TOAST, 
WindowManager.LayoutParams.FLAG_NOT_TOUCH_MODAL | WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE, 
PixelFormat.TRANSLUCENT);

具體實(shí)現(xiàn)代碼如下:

DragFloatActionButton floatBtn = new DragFloatActionButton(context, isForceLogin, mResultCallback);

   WindowManager windowManager = (WindowManager) context.getSystemService(Context.WINDOW_SERVICE);
   // 設(shè)置LayoutParams(全局變量)相關(guān)參數(shù)
   WindowManager.LayoutParams windowManagerParams = new WindowManager.LayoutParams(WindowManager.LayoutParams.TYPE_TOAST,
     WindowManager.LayoutParams.FLAG_NOT_TOUCH_MODAL | WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE,
     PixelFormat.TRANSLUCENT);
   /**
    * 注意,flag的值可以為:
    * 下面的flags屬性的效果形同“鎖定”。
    * 懸浮窗不可觸摸,不接受任何事件,同時(shí)不影響后面的事件響應(yīng)。
    * LayoutParams.FLAG_NOT_TOUCH_MODAL 不影響后面的事件
    * LayoutParams.FLAG_NOT_FOCUSABLE 不可聚焦
    * LayoutParams.FLAG_NOT_TOUCHABLE 不可觸摸
    */
   // 調(diào)整懸浮窗口至左上角,便于調(diào)整坐標(biāo)
   windowManagerParams.gravity = Gravity.LEFT | Gravity.TOP;
   // 以屏幕左上角為原點(diǎn),設(shè)置x、y初始值
   windowManagerParams.x = 0;
   windowManagerParams.y = 0;
   // 設(shè)置懸浮窗口長(zhǎng)寬數(shù)據(jù)
   floatBtn.measure(0, 0);
   floatBtn.setOriginWidth(floatBtn.getMeasuredWidth() - 50);
   windowManagerParams.width = floatBtn.getOriginWidth();
   windowManagerParams.height = windowManagerParams.width;
   // 顯示myFloatView圖像
   windowManager.addView(floatBtn, windowManagerParams);

以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。

相關(guān)文章

  • android控件Banner實(shí)現(xiàn)簡(jiǎn)單輪播圖效果

    android控件Banner實(shí)現(xiàn)簡(jiǎn)單輪播圖效果

    這篇文章主要為大家詳細(xì)介紹了android控件Banner實(shí)現(xiàn)簡(jiǎn)單輪播圖效果,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
    2021-05-05
  • Android LeakCanary的使用方法介紹

    Android LeakCanary的使用方法介紹

    在Android的性能優(yōu)化中,內(nèi)存優(yōu)化是必不可少的點(diǎn),而內(nèi)存優(yōu)化最重要的一點(diǎn)就是解決內(nèi)存泄漏的問(wèn)題,在Android的內(nèi)存泄漏分析工具也不少,比如PC端的有:AndroidStudio自帶的Android Profiler、MAT等工具;手機(jī)端也有,就是我們今天要介紹的LeakCanary
    2022-09-09
  • 詳解Flutter中的數(shù)據(jù)傳遞

    詳解Flutter中的數(shù)據(jù)傳遞

    這篇文章主要介紹了Flutter中的數(shù)據(jù)傳遞的相關(guān)資料,幫助大家更好的理解和學(xué)習(xí)使用Flutter,感興趣的朋友可以了解下
    2021-04-04
  • Android自定義雙向滑動(dòng)控件

    Android自定義雙向滑動(dòng)控件

    這篇文章主要為大家詳細(xì)介紹了Android自定義雙向滑動(dòng)控件,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
    2022-04-04
  • android自定義滾動(dòng)上下回彈scollView

    android自定義滾動(dòng)上下回彈scollView

    這篇文章主要為大家詳細(xì)介紹了android自定義滾動(dòng)上下回彈scollView,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
    2022-04-04
  • Android實(shí)現(xiàn)IM多人員組合的群組頭像

    Android實(shí)現(xiàn)IM多人員組合的群組頭像

    這篇文章主要為大家詳細(xì)介紹了Android實(shí)現(xiàn)IM多人員組合的群組頭像,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
    2018-10-10
  • Android?MPAndroidChart繪制原理

    Android?MPAndroidChart繪制原理

    這篇文章主要介紹了Android?MPAndroidChart繪制原理,文章圍繞主題展開(kāi)詳細(xì)的內(nèi)容介紹,具有一定的參考價(jià)值,需要的小伙伴可以參考一下
    2022-08-08
  • Android緩存之DiskLruCache磁盤(pán)緩存的使用

    Android緩存之DiskLruCache磁盤(pán)緩存的使用

    這篇文章主要介紹了Android緩存之DiskLruCache磁盤(pán)緩存的使用,小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧
    2018-08-08
  • Android fragment實(shí)現(xiàn)按鈕點(diǎn)擊事件的示例講解

    Android fragment實(shí)現(xiàn)按鈕點(diǎn)擊事件的示例講解

    下面小編就為大家分享一篇Android fragment實(shí)現(xiàn)按鈕點(diǎn)擊事件的示例講解,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧
    2018-01-01
  • Android下如何使用百度地圖sdk

    Android下如何使用百度地圖sdk

    百度地圖 Android SDK是一套基于Android 2.1(v1.3.5及以前版本支持android 1.5以上系統(tǒng))及以上版本設(shè)備的應(yīng)用程序接口
    2013-07-07

最新評(píng)論