您好,登錄后才能下訂單哦!
今天就跟大家聊聊有關怎么在Android中實現一個自由拖動并顯示文字的懸浮框,可能很多人都不太了解,為了讓大家更加了解,小編給大家總結了以下內容,希望大家根據這篇文章可以有所收獲。
實現步驟:
1.首先要設置該懸浮框的基本屬性:
/** * 顯示彈出框 * * @param context */ @SuppressWarnings("WrongConstant") public static void showPopupWindow(final Context context, String showtxt) { if (isShown) { return; } isShown = true; // 獲取WindowManager mWindowManager = (WindowManager) context .getSystemService(Context.WINDOW_SERVICE); mView = setUpView(context, showtxt); params = new WindowManager.LayoutParams(); // 類型,系統提示以及它總是出現在應用程序窗口之上。 params.type = WindowManager.LayoutParams.TYPE_SYSTEM_ALERT | WindowManager.LayoutParams.TYPE_SYSTEM_OVERLAY; // 設置flag int flags = canTouchFlags; // | WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE; // 如果設置了WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE,彈出的View收不到Back鍵的事件 params.flags = flags; // 不設置這個彈出框的透明遮罩顯示為黑色 params.format = PixelFormat.TRANSLUCENT; // FLAG_NOT_TOUCH_MODAL不阻塞事件傳遞到后面的窗口 // 設置 FLAG_NOT_FOCUSABLE 懸浮窗口較小時,后面的應用圖標由不可長按變為可長按 // 不設置這個flag的話,home頁的劃屏會有問題 params.width = LayoutParams.WRAP_CONTENT; params.height = LayoutParams.WRAP_CONTENT; params.gravity = Gravity.TOP; mWindowManager.addView(mView, params); }
比較重要的點是要注意設置flags,我這里提供了兩種flags以供切換:
private static int canTouchFlags = WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE | WindowManager.LayoutParams.FLAG_NOT_TOUCH_MODAL; private static int notTouchFlags = WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE| WindowManager.LayoutParams.FLAG_NOT_TOUCHABLE;
第一種是可觸摸不可聚焦模式,第二種是不可觸摸不可聚焦模式。其他的flags可以從api中查閱。
2.設置懸浮框的拖動監聽事件:
private static View setUpView(final Context context, String showtxt) { View view = LayoutInflater.from(context).inflate(R.layout.layout_popwindow, null); TextView showTv = (TextView) view.findViewById(R.id.tv_showinpop); showTv.setText(showtxt); rl_drag_showinpop = (RelativeLayout) view.findViewById(R.id.rl_drag_showinpop); rl_drag_showinpop.setOnTouchListener(new View.OnTouchListener() { private float lastX; //上一次位置的X.Y坐標 private float lastY; private float nowX; //當前移動位置的X.Y坐標 private float nowY; private float tranX; //懸浮窗移動位置的相對值 private float tranY; @Override public boolean onTouch(View v, MotionEvent event) { boolean ret = false; switch (event.getAction()) { case MotionEvent.ACTION_DOWN: // 獲取按下時的X,Y坐標 lastX = event.getRawX(); lastY = event.getRawY(); ret = true; break; case MotionEvent.ACTION_MOVE: // 獲取移動時的X,Y坐標 nowX = event.getRawX(); nowY = event.getRawY(); // 計算XY坐標偏移量 tranX = nowX - lastX; tranY = nowY - lastY; params.x += tranX; params.y += tranY; //更新懸浮窗位置 mWindowManager.updateViewLayout(mView, params); //記錄當前坐標作為下一次計算的上一次移動的位置坐標 lastX = nowX; lastY = nowY; break; case MotionEvent.ACTION_UP: break; } return ret; } });
這里要在down的時候記錄坐標,move事件中使用修改params坐標進行移動。
3.設置懸浮框文字屬性:
public static void setShowTxt(String txt) { try { TextView showTv = (TextView) mView.findViewById(R.id.tv_showinpop); showTv.setText(txt); mWindowManager.updateViewLayout(mView, params); }catch (Exception e){ Log.d(TAG, "setShowTxt: 更新懸浮框錯誤"); e.printStackTrace(); if(e.getMessage().contains("not attached to window manager")){ mWindowManager.addView(mView, params); } } }
4.更新懸浮框圖片顯示:
public static void setShowImg(Bitmap bitmap) { try { ImageView showImg = (ImageView) mView.findViewById(R.id.iv_showinpop); showImg.setImageBitmap(bitmap); mWindowManager.updateViewLayout(mView, params); }catch (Exception e){ Log.d(TAG, "setShowTxt: 更新懸浮框錯誤"); e.printStackTrace(); if(e.getMessage().contains("not attached to window manager")){ mWindowManager.addView(mView, params); } } }
介紹完畢,整個類都封裝好了,代碼如下:
/** * 懸浮窗工具類 * created by Pumpkin at 17/3/28 */ public class WindowsUitlity { private static String TAG = WindowsUitlity.class.getSimpleName(); private static WindowManager mWindowManager = null; private static WindowManager.LayoutParams params; public static Boolean isShown = false; private static View mView = null; /** * 顯示彈出框 * * @param context */ @SuppressWarnings("WrongConstant") public static void showPopupWindow(final Context context, String showtxt) { if (isShown) { return; } isShown = true; // 獲取WindowManager mWindowManager = (WindowManager) context .getSystemService(Context.WINDOW_SERVICE); mView = setUpView(context, showtxt); params = new WindowManager.LayoutParams(); // 類型,系統提示以及它總是出現在應用程序窗口之上。 params.type = WindowManager.LayoutParams.TYPE_SYSTEM_ALERT | WindowManager.LayoutParams.TYPE_SYSTEM_OVERLAY; // 設置flag int flags = canTouchFlags; // | WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE; // 如果設置了WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE,彈出的View收不到Back鍵的事件 params.flags = flags; // 不設置這個彈出框的透明遮罩顯示為黑色 params.format = PixelFormat.TRANSLUCENT; // FLAG_NOT_TOUCH_MODAL不阻塞事件傳遞到后面的窗口 // 設置 FLAG_NOT_FOCUSABLE 懸浮窗口較小時,后面的應用圖標由不可長按變為可長按 // 不設置這個flag的話,home頁的劃屏會有問題 params.width = LayoutParams.WRAP_CONTENT; params.height = LayoutParams.WRAP_CONTENT; params.gravity = Gravity.TOP; mWindowManager.addView(mView, params); } private static int canTouchFlags = WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE | WindowManager.LayoutParams.FLAG_NOT_TOUCH_MODAL; private static int notTouchFlags = WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE| WindowManager.LayoutParams.FLAG_NOT_TOUCHABLE; /** * 設置是否可響應點擊事件 * * @param isTouchable */ public static void setTouchable(boolean isTouchable) { if (isTouchable) { params.flags = canTouchFlags; } else { params.flags = notTouchFlags; } mWindowManager.updateViewLayout(mView, params); } /** * 隱藏彈出框 */ public static void hidePopupWindow() { if (isShown && null != mView) { mWindowManager.removeView(mView); isShown = false; } } public static void setShowTxt(String txt) { try { TextView showTv = (TextView) mView.findViewById(R.id.tv_showinpop); showTv.setText(txt); mWindowManager.updateViewLayout(mView, params); }catch (Exception e){ Log.d(TAG, "setShowTxt: 更新懸浮框錯誤"); e.printStackTrace(); if(e.getMessage().contains("not attached to window manager")){ mWindowManager.addView(mView, params); } } } public static void setShowImg(Bitmap bitmap) { try { ImageView showImg = (ImageView) mView.findViewById(R.id.iv_showinpop); showImg.setImageBitmap(bitmap); mWindowManager.updateViewLayout(mView, params); }catch (Exception e){ Log.d(TAG, "setShowTxt: 更新懸浮框錯誤"); e.printStackTrace(); if(e.getMessage().contains("not attached to window manager")){ mWindowManager.addView(mView, params); } } } static RelativeLayout rl_drag_showinpop; private static View setUpView(final Context context, String showtxt) { View view = LayoutInflater.from(context).inflate(R.layout.layout_popwindow, null); TextView showTv = (TextView) view.findViewById(R.id.tv_showinpop); showTv.setText(showtxt); rl_drag_showinpop = (RelativeLayout) view.findViewById(R.id.rl_drag_showinpop); rl_drag_showinpop.setOnTouchListener(new View.OnTouchListener() { private float lastX; //上一次位置的X.Y坐標 private float lastY; private float nowX; //當前移動位置的X.Y坐標 private float nowY; private float tranX; //懸浮窗移動位置的相對值 private float tranY; @Override public boolean onTouch(View v, MotionEvent event) { boolean ret = false; switch (event.getAction()) { case MotionEvent.ACTION_DOWN: // 獲取按下時的X,Y坐標 lastX = event.getRawX(); lastY = event.getRawY(); ret = true; break; case MotionEvent.ACTION_MOVE: // 獲取移動時的X,Y坐標 nowX = event.getRawX(); nowY = event.getRawY(); // 計算XY坐標偏移量 tranX = nowX - lastX; tranY = nowY - lastY; params.x += tranX; params.y += tranY; //更新懸浮窗位置 mWindowManager.updateViewLayout(mView, params); //記錄當前坐標作為下一次計算的上一次移動的位置坐標 lastX = nowX; lastY = nowY; break; case MotionEvent.ACTION_UP: break; } return ret; } }); return view; } }
看完上述內容,你們對怎么在Android中實現一個自由拖動并顯示文字的懸浮框有進一步的了解嗎?如果還想了解更多知識或者相關內容,請關注億速云行業資訊頻道,感謝大家的支持。
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。