WindowManager.addView()是Android中的一個(gè)方法,用于在屏幕上添加一個(gè)窗口。它允許你在應(yīng)用程序的上下文之外創(chuàng)建一個(gè)窗口,并將其顯示在其他應(yīng)用程序或系統(tǒng)界面上。
-
新建一個(gè)自定義View用于顯示
class MyView @JvmOverloads constructor(context: Context?,attrs: AttributeSet? = null, defStyleAttr: Int = 0) : View(context, attrs, defStyleAttr) { var myImage:Bitmap?=null var pX = 0f var pY = 0f init { myImage = BitmapFactory.decodeResource(resources,R.mipmap.image) } fun setPos(x:Float,y:Float){ this.pX = x; this.pY = y invalidate() } override fun onDraw(canvas: Canvas?) { super.onDraw(canvas) if (pX != 0f||pY != 0f){ myImage?.let { canvas?.drawBitmap(it,pX,pY,null) } } } }
-
使用windowManager.addView()顯示文章來(lái)源:http://www.zghlxwxcb.cn/news/detail-801904.html
var myView :MyView?=null var randomCount = 0 private fun show() { myView = MyView(mConext) val layoutParams = WindowManager.LayoutParams( WindowManager.LayoutParams.WRAP_CONTENT, WindowManager.LayoutParams.WRAP_CONTENT, WindowManager.LayoutParams.TYPE_APPLICATION_OVERLAY,// 設(shè)置窗口類型為懸浮窗口,需要懸浮窗權(quán)限 WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE or WindowManager.LayoutParams.FLAG_NOT_TOUCHABLE,// 窗口不需要獲取焦點(diǎn) PixelFormat.TRANSLUCENT // 設(shè)置窗口背景為透明 ) val windowManager = getSystemService(Context.WINDOW_SERVICE) as WindowManager windowManager.addView(myView , layoutParams) CoroutineScope(Dispatchers.IO).launch { while (randomCount < 20){ delay(1000) withContext(Dispatchers.Main){ myView?.let { it.setPos((0 until 500).random().toFloat(), (0 until 500).random().toFloat()) } } randomCount ++ } withContext(Dispatchers.Main){ windowManager.removeView(myView) } } }
首先創(chuàng)建了一個(gè)WindowManager對(duì)象,在這個(gè)對(duì)象上進(jìn)行操作。然后創(chuàng)建了一個(gè)自定義的MyView對(duì)象,作為要添加的窗口的內(nèi)容。接下來(lái),創(chuàng)建了WindowManager.LayoutParams對(duì)象,用于指定窗口的各種屬性,比如寬度、高度、位置等。最后,通過(guò)windowManager.addView()方法將自定義的View添加到WindowManager中,從而將其顯示在屏幕上。文章來(lái)源地址http://www.zghlxwxcb.cn/news/detail-801904.html
到了這里,關(guān)于Android在系統(tǒng)界面上添加窗口的文章就介紹完了。如果您還想了解更多內(nèi)容,請(qǐng)?jiān)谟疑辖撬阉鱐OY模板網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章,希望大家以后多多支持TOY模板網(wǎng)!