android懸浮功能實現,Android實現系統級懸浮按鈕

本文實例為大家分享了Android系統級懸浮按鈕的具體代碼,供大家參考,具體內容如下

具體的需求

1、就是做一個系統級的懸浮按鈕,就像iPhone 桌面的那個懸浮按鈕效果一樣,能隨意拖動,并且手一放開,懸浮按鈕就自動靠邊。

2、可以點擊并且可以隨意拖動。

3、懸浮按鈕自動靠邊的時候,或者移動到邊上的時候,自動隱藏半邊。

4、橫豎屏切換都兼容

1、就在WindowManager 里面添加View,這個View通過自定義控件來實現。

2、在onTouch里的MotionEvent.ACTION_MOVE事件里頭,通過控制懸浮按鈕的具體坐標來實現隨意移動。

3、在onTouch里的MotionEvent.ACTION_UP事件里頭,來控制懸浮按鈕自動靠邊,并且自動隱藏半邊,不過在這里onTouch和onClick這兩個事件是一起觸發的,不過這也有解決辦法,你可以在手放開的瞬間,通過移動的距離,來決定是否觸發點擊事件,,如果返回false,就會觸發點擊事件,如果返回true就會觸發點擊事件

4、通過自定義控件onLayout方法,來捕獲橫豎屏切換事件,

5、還有一個靠哪邊停靠的問題,通過坐標來判讀更靠近哪一邊。就靠哪邊停靠。

![以中間這個中心點為準,以更短的X軸畫一個正方形]

c5715eab5672da60fdf9c67bfcb5d10b.png

下面是具體實現代碼:

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; //懸浮按鈕點擊回調

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();

//獲取到狀態欄的高度

Rect frame = new Rect();

getWindowVisibleDisplayFrame(frame);

int ea = event.getAction();

switch (ea) {

case MotionEvent.ACTION_DOWN:

lastX = event.getRawX();// 獲取觸摸事件觸摸位置的原始X坐標

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, "移動距離: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, "松開時,移動距離:lastMoveDx=" + lastMoveDx + ", lastMoveDy=" + lastMoveDy);

if (lastMoveDx < 10 && lastMoveDy < 10) { //移動距離太小,視為點擊,

return false;

} else {

updateViewLayout(event);

isFirstClick = true;

return true;

}

}

return false;

}

/**

* 顯示整個圖標

*/

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); // 刷新顯示

}

/**

* 更新懸浮圖標

*

* @param event 手動移動事件

*/

public void updateViewLayout(MotionEvent event) {

Point center = new Point(screenWidth / 2, screenHeight / 2); //屏幕中心點

float xOffset, yOffset;//以屏幕中心點為原點,X軸和Y軸上的偏移量

if (event != null) {//手動移動的

xOffset = event.getRawX() - center.x;

yOffset = event.getRawY() - center.y;

} else {//自動隱藏

xOffset = lastX - center.x;

yOffset = lastY - center.y;

}

if (Math.abs(xOffset) >= Math.abs(yOffset)) {//向左或向右縮進隱藏

if (xOffset <= 0) { //向左縮進

showInLeft();

} else {

showInRight();

}

} else {//向上或向下縮進隱藏

if (yOffset <= 0) {//向上縮進

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) {//屏幕旋轉切換

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, "執行點擊事件");

if (!isFirstClick) {

OpenIDApplication.getInstance().floatBtnClick(context, OpenIDApplication.getInstance().isForceLogin(), resultCallback);

} else {//半隱藏狀態,點擊顯示全部

isFirstClick = false;

showAllBtn();

}

}

}

調用實現代碼,這里注意有個問題,彈出系統級的懸浮窗,需要配置權限:

并且Android 6.0以上的手機,還要彈出對話框問用戶是否運行,如果這個用戶拒絕了,就不能彈出系統級的懸浮窗了,還有個別手機廠商修改了android源碼,還需要進系統設置里去允許這個應用彈出懸浮窗。這樣的話就體驗感非常不好,不過這里有個小技巧,按下面方式設置為toast類型就完全解決,既不用配置權限,也不彈出窗來向用戶獲取權限,完全解決問題。

WindowManager.LayoutParams windowManagerParams = new WindowManager.LayoutParams(WindowManager.LayoutParams.TYPE_TOAST,

WindowManager.LayoutParams.FLAG_NOT_TOUCH_MODAL | WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE,

PixelFormat.TRANSLUCENT);

具體實現代碼如下:

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

WindowManager windowManager = (WindowManager) context.getSystemService(Context.WINDOW_SERVICE);

// 設置LayoutParams(全局變量)相關參數

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屬性的效果形同“鎖定”。

* 懸浮窗不可觸摸,不接受任何事件,同時不影響后面的事件響應。

* LayoutParams.FLAG_NOT_TOUCH_MODAL 不影響后面的事件

* LayoutParams.FLAG_NOT_FOCUSABLE 不可聚焦

* LayoutParams.FLAG_NOT_TOUCHABLE 不可觸摸

*/

// 調整懸浮窗口至左上角,便于調整坐標

windowManagerParams.gravity = Gravity.LEFT | Gravity.TOP;

// 以屏幕左上角為原點,設置x、y初始值

windowManagerParams.x = 0;

windowManagerParams.y = 0;

// 設置懸浮窗口長寬數據

floatBtn.measure(0, 0);

floatBtn.setOriginWidth(floatBtn.getMeasuredWidth() - 50);

windowManagerParams.width = floatBtn.getOriginWidth();

windowManagerParams.height = windowManagerParams.width;

// 顯示myFloatView圖像

windowManager.addView(floatBtn, windowManagerParams);

以上就是本文的全部內容,希望對大家的學習有所幫助,也希望大家多多支持腳本之家。

本文來自互聯網用戶投稿,該文觀點僅代表作者本人,不代表本站立場。本站僅提供信息存儲空間服務,不擁有所有權,不承擔相關法律責任。
如若轉載,請注明出處:http://www.pswp.cn/news/539374.shtml
繁體地址,請注明出處:http://hk.pswp.cn/news/539374.shtml
英文地址,請注明出處:http://en.pswp.cn/news/539374.shtml

如若內容造成侵權/違法違規/事實不符,請聯系多彩編程網進行投訴反饋email:809451989@qq.com,一經查實,立即刪除!

相關文章

oracle decode_錯過血虧!一文搞懂Oracle鎖相關視圖及相關操作

本文主要研究鎖的相關視圖&#xff0c;以及鎖的相關操作&#xff0c;通過視圖查鎖的問題。 一、v$transaction視圖第一個視圖是v$transaction&#xff0c;就是Oracle數據庫所有活動的事務數&#xff0c;所有活動的事務每一個活動的事務在這里有一行。v$transactionXIDUSN表示當…

Linux文件系統與命令行

什么是命令行? 接收鍵盤命令并將其傳給操作系統執行的程序(用于輸入和管理命令的程序),統稱命令行,也叫: Shell&#xff0c;幾乎所有Linux發行版都提供了一個 Shell 程序,叫做: Bash (Bourne-Again Shell, 因為最初的 Shell 是由 Steve Bourne 編寫的原始 Unix 程序, Again 表…

freeMarker 遍歷 list,map,listmap

List List<String> clientSourceDatanew ArrayList<String>();clientSourceData.add("field字段");clientSourceData.add("title標題");ftl&#xff1a; <#if clientSourceData?exists><#list clientSourceData as key> <tr&g…

qtableview不選中_如何選中/取消選中QTableView并觸發setData()

我有一個自定義的QTableModel&#xff0c;我在PyQt中使用QTableView顯示它。我有一些字段設置為可檢查&#xff0c;我想添加“全部檢查”和“不檢查”按鈕。我覺得應該有一種方法可以使setData()從代碼中被調用&#xff0c;這樣檢查狀態就會改變&#xff0c;就像我已經用setDat…

android 自定義菜單欄,GitHub - earthWo/AndroidBottomNavigation: android 底部菜單欄,自定義樣式,自定義菜單數量,添加滾動動畫和水波紋動畫...

AndroidBottomNavigation截圖使用方法gradle:compile com.whitelife.library:library:1.0.1maven:com.whitelife.librarylibrary1.0pomandroid:id"id/bottom_navigation"android:layout_width"match_parent"android:layout_height"56dp"android:…

windows怎么打開python_windows怎么打開Python

Windows中運行Python的兩種運行方式認識編程環境 1 直接命令行啟用Python。當然&#xff0c;如果直接在cmd中輸入python&#xff0c;需要在windows中的path環境變量中做好設置。 此時&#xff0c;cmd中運行python就可以出現 “>>>” 符號。意味著python進入了交互運行…

sqldeveloper創建賬號_用oralce 自帶工具sql developer 創建表空間,用戶,權限

用oralce 自帶工具sql developer 創建/*第1步&#xff1a;創建臨時表空間 */create temporary tablespace hudongtemptablespacetempfile E:\worksubject\WY-Honda_Ess\Oracle11g\hudongtemptablespace.dbfsize 50mautoextend onnext 50m maxsize 20480mextent management loc…

AOE網與關鍵路徑簡介

前面我們說過的拓撲排序主要是為解決一個工程能否順序進行的問題&#xff0c;但有時我們還需要解決工程完成需要的最短時間問題。如果我們要對一個流程圖獲得最短時間&#xff0c;就必須要分析它們的拓撲關系&#xff0c;并且找到當中最關鍵的流程&#xff0c;這個流程的時間就…

Java 集合體系詳解——List體系有序集合

引言 面向對象語言對事物的體現必然是以對象的形式&#xff0c;Java工程師為了方便多多個對象的操作&#xff0c;就對對象進行存儲&#xff0c;集合就是存儲對象的一種方式&#xff0c;他們的底層都是基于不同的數據結構。當然集合和數組一樣都是容器&#xff0c;數組也是可以存…

android 定義固定數組,Android 圖片數組定義和讀取

位置&#xff1a;packages/apps/Launcher21、圖片數組定義、資源讀取如果有多張圖片&#xff0c;這些圖片的使用與順序無關&#xff0c;可以采取這種方式。drawable-nodpi中有3張圖片&#xff0c;wallpaper_1.jpg、wallpaper_2.jpg、wallpaper_3.jpgXML中定義數組IDwallpaper_1…

alert閃一下就沒了_尾部貫穿式鍍鉻銀飾條除了丑,還能閃瞎眼

尾部貫穿式鍍鉻銀飾條&#xff0c;在2010年代成為諸多汽車品牌車型爭相采用的新世紀新標配&#xff0c;配以雙邊排氣&#xff0c;讓整個車尾看起來層次感強烈&#xff0c;視覺收窄&#xff0c;幾十萬的奧迪A8L有&#xff0c;十幾萬的斯柯達速派有&#xff0c;A級車有&#xff0…

docker 指定網卡_Docker | Docker技術基礎梳理(五) Docker網絡管理

為什么需要容器的網絡管理&#xff1f;容器的網絡默認與宿主機、與其他容器相互隔離&#xff0c;且容器中可以運行一些網絡應用&#xff0c;比如nginx、web應用、數據庫等&#xff0c;如果需要讓外部也可以訪問這些容器中運行的網絡應用&#xff0c;那么就需要配置網絡來實現。…

java.net.URLEncode編碼 與 URLDecode解碼問題

原文&#xff1a;http://blog.csdn.net/luojian520025/article/details/9139293 -------------------------------------------------------------------------------------------- String mytext java.net.URLEncoder.encode("中國", "utf-8")…

Android安裝兩次才成功,Android應用從市場安裝完成打開與桌面打開,被啟動兩次的問題...

問題描述&#xff1a;1、從Android應用市場下載并安裝應用&#xff0c;安裝完成后&#xff0c;當前界面下方會出現“打開”按鈕&#xff0c;這時候我們點擊“打開”&#xff0c;會啟動應用&#xff0c;進入到應用的啟動頁面&#xff0c;然后進入應用的主界面&#xff0c;這個時…

事務保存點

在SQL Server中使用rollback會回滾所有的未提交事務狀態&#xff0c;但是有些時候我們只需要回滾部分語句&#xff0c;把不需要回滾的語句提到事務外面來&#xff0c;雖然是個方法&#xff0c;但是卻破壞了事務的ACID。 SQL中使用事務保存點 即可解決這個問題. 一.SQL 事務中存…

鼎信諾審計前端取數工具_給2019前端的5個建議

2019 農歷新年即將到來&#xff0c;是時候總結一下團隊過去一年的技術沉淀。過去一年我們支撐的數據相關業務突飛猛進&#xff0c;其中兩個核心平臺級產品代碼量分別達到30萬行和80萬行&#xff0c;TS 模塊數均超過1000個&#xff0c;協同開發人員增加到20人。由于歷史原因&…

Hadoop HA

HA概念&#xff1a; high avalability 高可用性。 hadoop 1.x非ha設計 Secondnode對元數據的可靠性有了保障&#xff0c;但服務的可用性不高。 即&#xff1a;當Namenode節點宕機了&#xff0c;整個hadoop就不能使用了&#xff0c;影響了client的使用。 hadoop 2.x的ha設計 新…

紫光展銳處理器有那些手機用_酷派將發千元5G手機,國產紫光展銳加持,主打性價比...

↑↑↑點擊上方藍字訂閱每日最新熱點手機資訊數年之前&#xff0c;“中華酷聯”是國產智能手機的四大代表。不過隨著越來越多的強力競爭者入局&#xff0c;中興、酷派、聯想漸漸衰敗&#xff0c;僅剩華為屹立手機行業頂端。但是酷派似乎從未想過放棄&#xff0c;最近便要發布一…

jelly bean android,Jelly Bean占Android系統份額突破10%

Android系統份額圖(騰訊科技配圖)騰訊科技訊(清雨)北京時間1月4日消息&#xff0c;據國外媒體報道&#xff0c;微博)周四發布最新數據顯示&#xff0c;Android 4.1版本和Android 4.2版本的Jelly Bean在Android系統中的份額超過了10%&#xff0c;另外Android 4.0版本的ICS的份額…

Unable to load native-hadoop library for your platform

警告&#xff1a; 16/08/04 19:21:36 WARN util.NativeCodeLoader: Unable to load native-hadoop library for your platform... using builtin-java classes where applicable 原因&#xff1a; 沒有配置好環境變量&#xff0c;hadoop的native沒有配置進去 解決方法&#x…