Android實(shí)現(xiàn)系統(tǒng)級(jí)懸浮按鈕
本文實(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í)有所幫助,也希望大家多多支持腳本之家。
- Android開(kāi)發(fā)懸浮按鈕 Floating ActionButton的實(shí)現(xiàn)方法
- Android仿知乎懸浮功能按鈕FloatingActionButton效果
- Android自定義可拖拽的懸浮按鈕DragFloatingActionButton
- Android開(kāi)發(fā)之FloatingActionButton懸浮按鈕基本使用、字體、顏色用法示例
- android 應(yīng)用內(nèi)部懸浮可拖動(dòng)按鈕簡(jiǎn)單實(shí)現(xiàn)代碼
- Android懸浮按鈕的使用方法
- Android懸浮窗按鈕實(shí)現(xiàn)點(diǎn)擊并顯示/隱藏多功能列表
- Android自定義APP全局懸浮按鈕
- Android利用WindowManager生成懸浮按鈕及懸浮菜單
- Android自定義懸浮按鈕效果
相關(guān)文章
android控件Banner實(shí)現(xiàn)簡(jiǎn)單輪播圖效果
這篇文章主要為大家詳細(xì)介紹了android控件Banner實(shí)現(xiàn)簡(jiǎn)單輪播圖效果,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2021-05-05android自定義滾動(dòng)上下回彈scollView
這篇文章主要為大家詳細(xì)介紹了android自定義滾動(dòng)上下回彈scollView,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2022-04-04Android實(shí)現(xiàn)IM多人員組合的群組頭像
這篇文章主要為大家詳細(xì)介紹了Android實(shí)現(xiàn)IM多人員組合的群組頭像,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2018-10-10Android緩存之DiskLruCache磁盤(pán)緩存的使用
這篇文章主要介紹了Android緩存之DiskLruCache磁盤(pán)緩存的使用,小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧2018-08-08Android fragment實(shí)現(xiàn)按鈕點(diǎn)擊事件的示例講解
下面小編就為大家分享一篇Android fragment實(shí)現(xiàn)按鈕點(diǎn)擊事件的示例講解,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2018-01-01