Created
September 20, 2024 18:54
-
-
Save elfefe/30931439e0f42c11898c1c002d62517c to your computer and use it in GitHub Desktop.
Utilitaires Android
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| \*** | |
| * Can be used from any context (Activity, Service, ...) | |
| *\ | |
| private fun createOverlay() { | |
| windowManager = getSystemService(WINDOW_SERVICE) as WindowManager | |
| dimView = View(this).apply { | |
| setBackgroundColor(Color.argb(brightnessAlpha, 0, 0, 0)) | |
| setTheme(android.R.style.Theme_Holo_NoActionBar_Fullscreen) | |
| } | |
| val params = WindowManager.LayoutParams( | |
| if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.R) | |
| windowManager?.maximumWindowMetrics?.bounds?.width() ?: | |
| WindowManager.LayoutParams.MATCH_PARENT | |
| else WindowManager.LayoutParams.MATCH_PARENT, | |
| if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.R) | |
| windowManager?.maximumWindowMetrics?.bounds?.height()?.let { it + 512 } ?: | |
| WindowManager.LayoutParams.MATCH_PARENT | |
| else WindowManager.LayoutParams.MATCH_PARENT, | |
| windowType(), | |
| WindowManager.LayoutParams.FLAG_NOT_TOUCHABLE or | |
| WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE or | |
| WindowManager.LayoutParams.FLAG_FULLSCREEN or | |
| WindowManager.LayoutParams.FLAG_LAYOUT_IN_SCREEN or | |
| WindowManager.LayoutParams.FLAG_LAYOUT_NO_LIMITS, | |
| PixelFormat.TRANSLUCENT | |
| ) | |
| params.gravity = Gravity.TOP or Gravity.START | |
| windowManager?.addView(dimView, params) | |
| } | |
| private fun removeOverlay() { | |
| dimView?.let { | |
| windowManager?.removeView(it) | |
| dimView = null | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment