Android實(shí)現(xiàn)懸浮窗的簡(jiǎn)單方法實(shí)例
1. 前言
現(xiàn)在很多應(yīng)用都有小懸浮窗的功能,比如看直播的時(shí)候,通過Home鍵返回桌面,直播的小窗口仍可以在屏幕上顯示。下面將介紹下懸浮窗的的一種簡(jiǎn)單實(shí)現(xiàn)方式。
2.原理
Window我們應(yīng)該很熟悉,它是一個(gè)接口類,具體的實(shí)現(xiàn)類為PhoneWindow,它可以對(duì)View進(jìn)行管理。WindowManager是一個(gè)接口類,繼承自ViewManager,從名稱就知道它是用來管理Window的,它的實(shí)現(xiàn)類是WindowManagerImpl。如果我們想要對(duì)Window(View)進(jìn)行添加、更新和刪除操作就可以使用WindowManager,WindowManager會(huì)將具體的工作交由WindowManagerService處理。這里我們只需要知道WindowManager能用來管理Window就好。
WindowManager是一個(gè)接口類,繼承自ViewManager,ViewManager中定義了3個(gè)方法,分布用來添加、更新和刪除View,如下所示:
public interface ViewManager {
public void addView(View view, ViewGroup.LayoutParams params);
public void updateViewLayout(View view, ViewGroup.LayoutParams params);
public void removeView(View view);
}
WindowManager也繼承了這些方法,而這些方法傳入的參數(shù)都是View類型,說明了Window是以View的形式存在的。
3.具體實(shí)現(xiàn)
3.1浮窗布局
懸浮窗的簡(jiǎn)易布局如下的可參考下面的layout_floating_window.xml文件。頂層深色部分的FrameLayout布局是用來實(shí)現(xiàn)懸浮窗的拖拽功能的,點(diǎn)擊右上角ImageView可以實(shí)現(xiàn)關(guān)閉懸浮窗,剩下區(qū)域顯示內(nèi)容,這里只是簡(jiǎn)單地顯示文本內(nèi)容,不做復(fù)雜的東西,故只設(shè)置TextView。
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<FrameLayout
android:id="@+id/layout_drag"
android:layout_width="match_parent"
android:layout_height="15dp"
android:background="#dddddd">
<androidx.appcompat.widget.AppCompatImageView
android:id="@+id/iv_close"
android:layout_width="15dp"
android:layout_height="15dp"
android:layout_gravity="end"
android:src="@drawable/img_delete"/>
</FrameLayout>
<androidx.appcompat.widget.AppCompatTextView
android:id="@+id/tv_content"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_gravity="center_horizontal"
android:background="#eeeeee"
android:scrollbars="vertical"/>
</LinearLayout>
3.2 懸浮窗的實(shí)現(xiàn)
1. 使用服務(wù)Service
Service 是一種可在后臺(tái)執(zhí)行長(zhǎng)時(shí)間運(yùn)行操作而不提供界面的應(yīng)用組件,可由其他應(yīng)用組件啟動(dòng),而且即使用戶切換到其他應(yīng)用,仍將在后臺(tái)繼續(xù)運(yùn)行。要保證應(yīng)用在后臺(tái)時(shí),懸浮窗仍然可以正常顯示,所以這里可以使用Service。
2. 獲取WindowManager并設(shè)置LayoutParams
private lateinit var windowManager: WindowManager
private lateinit var layoutParams: WindowManager.LayoutParams
override fun onCreate() {
// 獲取WindowManager
windowManager = getSystemService(WINDOW_SERVICE) as WindowManager
layoutParams = WindowManager.LayoutParams().apply {
// 實(shí)現(xiàn)在其他應(yīng)用和窗口上方顯示浮窗
type = if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
WindowManager.LayoutParams.TYPE_APPLICATION_OVERLAY
} else {
WindowManager.LayoutParams.TYPE_PHONE
}
format = PixelFormat.RGBA_8888
// 設(shè)置浮窗的大小和位置
gravity = Gravity.START or Gravity.TOP
flags = WindowManager.LayoutParams.FLAG_NOT_TOUCH_MODAL or WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE
width = 600
height = 600
x = 300
y = 300
}
}
3. 創(chuàng)建View并添加到WindowManager
private lateinit var floatingView: View
override fun onStartCommand(intent: Intent?, flags: Int, startId: Int): Int {
if (Settings.canDrawOverlays(this)) {
floatingView = LayoutInflater.from(this).inflate(R.layout.layout_floating_window.xml, null)
windowManager.addView(floatingView, layoutParams)
}
return super.onStartCommand(intent, flags, startId)
}
4. 實(shí)現(xiàn)懸浮窗的拖拽和關(guān)閉功能
// 浮窗的坐標(biāo)
private var x = 0
private var y = 0
override fun onStartCommand(intent: Intent?, flags: Int, startId: Int): Int {
if (Settings.canDrawOverlays(this)) {
floatingView = LayoutInflater.from(this).inflate(R.layout.layout_floating_window.xml, null)
windowManager.addView(floatingView, layoutParams)
// 點(diǎn)擊浮窗的右上角關(guān)閉按鈕可以關(guān)閉浮窗
floatingView.findViewById<AppCompatImageView>(R.id.iv_close).setOnClickListener {
windowManager.removeView(floatingView)
}
// 實(shí)現(xiàn)浮窗的拖動(dòng)功能, 通過改變layoutParams來實(shí)現(xiàn)
floatingView.findViewById<AppCompatImageView>(R.id.layout_drag).setOnTouchListener { v, event ->
when (event.action) {
MotionEvent.ACTION_DOWN -> {
x = event.rawX.toInt()
y = event.rawY.toInt()
}
MotionEvent.ACTION_MOVE -> {
val currentX = event.rawX.toInt()
val currentY = event.rawY.toInt()
val offsetX = currentX - x
val offsetY = currentY - y
x = currentX
y = currentY
layoutParams.x = layoutParams.x + offsetX
layoutParams.y = layoutParams.y + offsetY
// 更新floatingView
windowManager.updateViewLayout(floatingView, layoutParams)
}
}
true
}
return super.onStartCommand(intent, flags, startId)
}
5. 利用廣播進(jìn)行通信
private var receiver: MyReceiver? = null
override fun onCreate() {
// 注冊(cè)廣播
receiver = MyReceiver()
val filter = IntentFilter()
filter.addAction("android.intent.action.MyReceiver")
registerReceiver(receiver, filter)
}
inner class MyReceiver : BroadcastReceiver() {
override fun onReceive(context: Context, intent: Intent) {
val content = intent.getStringExtra("content") ?: ""
// 通過Handler更新UI
val message = Message.obtain()
message.what = 0
message.obj = content
handler.sendMessage(message)
}
}
val handler = Handler(this.mainLooper) { msg ->
tvContent.text = msg.obj as String
false
}
可以在Activity中通過廣播給Service發(fā)送信息
fun sendMessage(view: View?) {
Intent("android.intent.action.MyReceiver").apply {
putExtra("content", "Hello, World!")
sendBroadcast(this)
}
}
6. 設(shè)置權(quán)限
懸浮窗的顯示需要權(quán)限,在AndroidManefest.xml中添加:
<uses-permission android:name="android.permission.SYSTEM_ALERT_WINDOW" />
此外,還要通過Settings.ACTION_MANAGE_OVERLAY_PERMISSION來讓動(dòng)態(tài)設(shè)置權(quán)限,在Activity中設(shè)置。
// MainActivity.kt
fun startWindow(view: View?) {
if (!Settings.canDrawOverlays(this)) {
startActivityForResult(Intent(Settings.ACTION_MANAGE_OVERLAY_PERMISSION, Uri.parse("package:$packageName")), 0)
} else {
startService(Intent(this@MainActivity, FloatingWindowService::class.java))
}
}
override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) {
super.onActivityResult(requestCode, resultCode, data)
if (requestCode == 0) {
if (Settings.canDrawOverlays(this)) {
Toast.makeText(this, "懸浮窗權(quán)限授權(quán)成功", Toast.LENGTH_SHORT).show()
startService(Intent(this@MainActivity, FloatingWindowService::class.java))
}
}
}
3.3 完整代碼
class FloatingWindowService : Service() {
private lateinit var windowManager: WindowManager
private lateinit var layoutParams: WindowManager.LayoutParams
private lateinit var tvContent: AppCompatTextView
private lateinit var handler: Handler
private var receiver: MyReceiver? = null
private var floatingView: View? = null
private val stringBuilder = StringBuilder()
private var x = 0
private var y = 0
// 用來判斷floatingView是否attached 到 window manager,防止二次removeView導(dǎo)致崩潰
private var attached = false
override fun onCreate() {
super.onCreate()
// 注冊(cè)廣播
receiver = MyReceiver()
val filter = IntentFilter()
filter.addAction("android.intent.action.MyReceiver")
registerReceiver(receiver, filter);
// 獲取windowManager并設(shè)置layoutParams
windowManager = getSystemService(WINDOW_SERVICE) as WindowManager
layoutParams = WindowManager.LayoutParams().apply {
type = if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
WindowManager.LayoutParams.TYPE_APPLICATION_OVERLAY
} else {
WindowManager.LayoutParams.TYPE_PHONE
}
format = PixelFormat.RGBA_8888
// format = PixelFormat.TRANSPARENT
gravity = Gravity.START or Gravity.TOP
flags = WindowManager.LayoutParams.FLAG_NOT_TOUCH_MODAL or WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE
width = 600
height = 600
x = 300
y = 300
}
handler = Handler(this.mainLooper) { msg ->
tvContent.text = msg.obj as String
// 當(dāng)文本超出屏幕自動(dòng)滾動(dòng),保證文本處于最底部
val offset = tvContent.lineCount * tvContent.lineHeight
floatingView?.apply {
if (offset > height) {
tvContent.scrollTo(0, offset - height)
}
}
false
}
}
override fun onBind(intent: Intent?): IBinder? {
return null
}
@SuppressLint("ClickableViewAccessibility")
override fun onStartCommand(intent: Intent?, flags: Int, startId: Int): Int {
if (Settings.canDrawOverlays(this)) {
floatingView = LayoutInflater.from(this).inflate(R.layout.layout_show_log, null)
tvContent = floatingView!!.findViewById(R.id.tv_log)
floatingView!!.findViewById<AppCompatImageView>(R.id.iv_close).setOnClickListener {
stringBuilder.clear()
windowManager.removeView(floatingView)
attached = false
}
// 設(shè)置TextView滾動(dòng)
tvContent.movementMethod = ScrollingMovementMethod.getInstance()
floatingView!!.findViewById<FrameLayout>(R.id.layout_drag).setOnTouchListener { v, event ->
when (event.action) {
MotionEvent.ACTION_DOWN -> {
x = event.rawX.toInt()
y = event.rawY.toInt()
}
MotionEvent.ACTION_MOVE -> {
val currentX = event.rawX.toInt()
val currentY = event.rawY.toInt()
val offsetX = currentX - x
val offsetY = currentY - y
x = currentX
y = currentY
layoutParams.x = layoutParams.x + offsetX
layoutParams.y = layoutParams.y + offsetY
windowManager.updateViewLayout(floatingView, layoutParams)
}
}
true
}
windowManager.addView(floatingView, layoutParams)
attached = true
}
return super.onStartCommand(intent, flags, startId)
}
override fun onDestroy() {
// 注銷廣播并刪除浮窗
unregisterReceiver(receiver)
receiver = null
if (attached) {
windowManager.removeView(floatingView)
}
}
inner class MyReceiver : BroadcastReceiver() {
override fun onReceive(context: Context, intent: Intent) {
val content = intent.getStringExtra("content") ?: ""
stringBuilder.append(content).append("\n")
val message = Message.obtain()
message.what = 0
message.obj = stringBuilder.toString()
handler.sendMessage(message)
}
}
}
4. 總結(jié)
以上就是Android懸浮窗的一個(gè)簡(jiǎn)單實(shí)現(xiàn)方式。如果需要實(shí)現(xiàn)其他復(fù)雜一點(diǎn)的功能,比如播放視頻,也可以在此基礎(chǔ)上完成。
到此這篇關(guān)于Android實(shí)現(xiàn)懸浮窗的簡(jiǎn)單方法的文章就介紹到這了,更多相關(guān)Android實(shí)現(xiàn)懸浮窗內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
- 使用Android實(shí)現(xiàn)跨頁(yè)面懸浮窗效果
- Android懸浮窗的實(shí)現(xiàn)步驟
- Android開發(fā)懸浮窗踩坑解決
- Android 無(wú)障礙全局懸浮窗實(shí)現(xiàn)示例
- Android實(shí)現(xiàn)懸浮窗效果
- Android應(yīng)用內(nèi)懸浮窗Activity的簡(jiǎn)單實(shí)現(xiàn)
- Android超簡(jiǎn)單懸浮窗使用教程
- Android創(chuàng)建懸浮窗的完整步驟
- Android?懸浮窗開發(fā)示例((動(dòng)態(tài)權(quán)限請(qǐng)求?|?前臺(tái)服務(wù)和通知?|?懸浮窗創(chuàng)建?)
相關(guān)文章
Android基于PhotoView實(shí)現(xiàn)的頭像/圓形裁剪控件
這篇文章主要給大家介紹了關(guān)于Android基于PhotoView實(shí)現(xiàn)的頭像/圓形裁剪控件的相關(guān)資料,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2018-07-07
android2.3.5 CDMA/EVDO撥號(hào)APN解決方案
google提供的android2.3里面,只能在GSM/WCDMA情況下才能從“設(shè)置”->“無(wú)線和網(wǎng)絡(luò)”->“移動(dòng)網(wǎng)絡(luò)”->“接入點(diǎn)名稱”中選擇不同的apn帳號(hào)進(jìn)行撥號(hào)連接,而CDMA/EVDO則沒有這個(gè)功能,接下來本文介紹一些方法實(shí)現(xiàn)這個(gè)功能,感興趣的朋友可以了解下2013-01-01
詳解Android如何實(shí)現(xiàn)好的彈層體驗(yàn)效果
當(dāng)前?App?的設(shè)計(jì)趨勢(shì)越來越希望給用戶沉浸式體驗(yàn),這種設(shè)計(jì)會(huì)讓用戶盡量停留在當(dāng)前的界面,而不需要太多的跳轉(zhuǎn),這就需要引入彈層。本篇我們就來講講彈層這塊需要注意哪些用戶體驗(yàn)2022-11-11
Android編輯框EditText與焦點(diǎn)變更監(jiān)視器及文本變化監(jiān)視器實(shí)現(xiàn)流程詳解
這篇文章主要介紹了Android編輯框EditText與焦點(diǎn)變更監(jiān)視器及文本變化監(jiān)視器實(shí)現(xiàn)流程,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)吧2022-09-09
android 版本檢測(cè) Android程序的版本檢測(cè)與更新實(shí)現(xiàn)介紹
做個(gè)網(wǎng)站的安卓客戶端,用戶安裝到自己手機(jī)上,如果我出了新版本怎么辦呢?要有版本更新功能,感興趣的朋友可以了解下2013-01-01
Android Studio去除界面默認(rèn)標(biāo)題欄的方法
這篇文章主要介紹了Android Studio去除界面默認(rèn)標(biāo)題欄的方法,本文通過圖文并茂的形式給大家介紹的非常詳細(xì),對(duì)大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2007-09-09
Android修改DatePicker字體顏色及分割線顏色詳細(xì)介紹
這篇文章主要介紹了Android修改DatePicker字體顏色及分割線顏色詳細(xì)介紹的相關(guān)資料,需要的朋友可以參考下2017-05-05
一個(gè)簡(jiǎn)單的Android圓弧刷新動(dòng)畫
這篇文章主要為大家詳細(xì)介紹了一個(gè)簡(jiǎn)單的Android圓弧刷新動(dòng)畫,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2019-09-09
神奇的listView實(shí)現(xiàn)自動(dòng)顯示隱藏布局Android代碼
這篇文章主要介紹了神奇的listView實(shí)現(xiàn)自動(dòng)顯示隱藏布局Android代碼實(shí)現(xiàn),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2016-09-09

