Форум для тех, кто программирует под Android
Anonymous
Добавление пользовательского просмотра в экран через Window_Manager с анимацией wrap_content
Сообщение
Anonymous » 26 сен 2025, 09:09
Добавление пользовательского Composeview в windowmanager :
Код: Выделить всё
private fun addCustomViewToWindow() {
windowManager = getSystemService(WINDOW_SERVICE) as WindowManager
customView = CustomViewToAttachToRoot(this, this)
val layoutParams = WindowManager.LayoutParams(
WindowManager.LayoutParams.MATCH_PARENT,
WindowManager.LayoutParams.WRAP_CONTENT,
WindowManager.LayoutParams.TYPE_APPLICATION,
WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE,
android.graphics.PixelFormat.TRANSLUCENT
).apply {
gravity = Gravity.BOTTOM
}
val customComposeView = ComposeView(this).apply {
setContent {
ChangingHeightComposable()
}
}
customView.addView(customComposeView)
windowManager.addView(customView, layoutParams)
}
< /code>
Custom Framelayout < /code>: < /h3>
class CustomViewToAttachToRoot(
context: Context,
private val lifecycleOwner: LifecycleOwner
) : FrameLayout(context) {
init {
setBackgroundColor(resources.getColor(R.color.purple_500))
setViewTreeLifecycleOwner(lifecycleOwner)
if (lifecycleOwner is SavedStateRegistryOwner) {
setViewTreeSavedStateRegistryOwner(lifecycleOwner)
}
}
}
< /code>
Composable с анимационной высотой:
Код: Выделить всё
@Composable
fun ChangingHeightComposable() {
val infiniteTransition = rememberInfiniteTransition(label = "heightTransition")
val animatedHeight by infiniteTransition.animateValue(
initialValue = 200.dp,
targetValue = 100.dp,
typeConverter = Dp.VectorConverter,
animationSpec = infiniteRepeatable(
animation = tween(durationMillis = 1000),
repeatMode = RepeatMode.Reverse
),
label = "animatedHeight"
)
Box(
modifier = Modifier
.fillMaxWidth()
.height(animatedHeight)
.background(Color.Cyan)
)
}
< /code>
💡 Примечания о мерцании: < /p>
Обычно происходит мерцание, потому что Windowmanager < /code> пытается решить вид каждую кадр при динамике. Измерение/макет Pass.>
Подробнее здесь:
https://stackoverflow.com/questions/797 ... ap-content
1758866956
Anonymous
Добавление пользовательского Composeview в windowmanager : [code]private fun addCustomViewToWindow() { windowManager = getSystemService(WINDOW_SERVICE) as WindowManager customView = CustomViewToAttachToRoot(this, this) val layoutParams = WindowManager.LayoutParams( WindowManager.LayoutParams.MATCH_PARENT, WindowManager.LayoutParams.WRAP_CONTENT, WindowManager.LayoutParams.TYPE_APPLICATION, WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE, android.graphics.PixelFormat.TRANSLUCENT ).apply { gravity = Gravity.BOTTOM } val customComposeView = ComposeView(this).apply { setContent { ChangingHeightComposable() } } customView.addView(customComposeView) windowManager.addView(customView, layoutParams) } < /code> Custom Framelayout < /code>: < /h3> class CustomViewToAttachToRoot( context: Context, private val lifecycleOwner: LifecycleOwner ) : FrameLayout(context) { init { setBackgroundColor(resources.getColor(R.color.purple_500)) setViewTreeLifecycleOwner(lifecycleOwner) if (lifecycleOwner is SavedStateRegistryOwner) { setViewTreeSavedStateRegistryOwner(lifecycleOwner) } } } < /code> Composable[/code] с анимационной высотой: [code]@Composable fun ChangingHeightComposable() { val infiniteTransition = rememberInfiniteTransition(label = "heightTransition") val animatedHeight by infiniteTransition.animateValue( initialValue = 200.dp, targetValue = 100.dp, typeConverter = Dp.VectorConverter, animationSpec = infiniteRepeatable( animation = tween(durationMillis = 1000), repeatMode = RepeatMode.Reverse ), label = "animatedHeight" ) Box( modifier = Modifier .fillMaxWidth() .height(animatedHeight) .background(Color.Cyan) ) } < /code> 💡 Примечания о мерцании: < /p> Обычно происходит мерцание, потому что Windowmanager < /code> пытается решить вид каждую кадр при динамике. Измерение/макет [/code] Pass.> Подробнее здесь: [url]https://stackoverflow.com/questions/79775525/adding-a-custom-view-to-screen-via-window-manager-with-animating-wrap-content[/url]