国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁(yè) > 系統(tǒng) > Android > 正文

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

2019-12-12 03:21:29
字體:
來(lái)源:轉(zhuǎn)載
供稿:網(wǎng)友

本文實(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í)有所幫助,也希望大家多多支持武林網(wǎng)。

發(fā)表評(píng)論 共有條評(píng)論
用戶名: 密碼:
驗(yàn)證碼: 匿名發(fā)表
主站蜘蛛池模板: 伊春市| 新津县| 苍山县| 荔波县| 上杭县| 赫章县| 开化县| 海口市| 淳安县| 安福县| 永德县| 卢湾区| 汉阴县| 峨眉山市| 尉犁县| 顺义区| 高尔夫| 青神县| 庆元县| 垫江县| 玛曲县| 沂源县| 盈江县| 田阳县| 保亭| 义乌市| 贵港市| 湾仔区| 铜山县| 西丰县| 汽车| 龙胜| 保德县| 和平区| 平果县| 托里县| 肇源县| 故城县| 新巴尔虎右旗| 左权县| 五台县|