肖志康
09/04/2023, 9:57 AMvide
09/04/2023, 10:19 AM• Composable functions can execute in any order.
• Composable functions can execute in parallel.
• Recomposition skips as many composable functions and lambdas as possible.
• Recomposition is optimistic and may be canceled.
• A composable function might be run quite frequently, as often as every frame of an animation.https://developer.android.com/jetpack/compose/mental-model So no guarantees about execution order. You can control drawing order with Box and zIndex (at least). Not sure about measuring • https://developer.android.com/reference/kotlin/androidx/compose/foundation/layout/package-summary#Box(androidx.compose.ui[…]oolean,kotlin.Function1) • https://developer.android.com/reference/kotlin/androidx/compose/ui/Modifier#(androidx.compose.ui.Modifier).zIndex(kotlin.Float)
肖志康
09/04/2023, 11:02 AMBox( modifier = Modifier.fillMaxSize().padding(it)) {
Box(contentAlignment = Alignment.Center) {
var ticks by remember {
mutableStateOf(0)
}
val color1 by remember {
derivedStateOf { ticks % 4 in 1..2 }
}
val color2 by remember {
derivedStateOf { ticks % 4 < 2 }
}
LaunchedEffect(Unit) {
while (true) {
delay(500L)
ticks++
}
}
ColorBox(100.dp, if (color1) {Color.Blue} else {Color.Red})
ColorBox(50.dp, if (color2) {Color.Green} else {Color.Yellow})
}
}
@Composable
fun ColorBox(size: Dp, color: Color) {
Box(
modifier = Modifier.size(size).background(color)
)
SideEffect {
Log.i("ColorBox", "ColorBox size $size color ${color.toArgb()}")
}
}
On tick's increatment, only one ColorBox is updated and recomposed (and called), but the 50.dp box is always on the top of 100.dp box. Maybe I can draw a conclusion that drawing order of composable widgets is exactly the same as their statement order (in the most common case)?efemoney
09/04/2023, 11:25 AMefemoney
09/04/2023, 11:27 AM肖志康
09/04/2023, 11:38 AM肖志康
09/04/2023, 12:06 PMLayout
block, found that drawing order is the same as the order container calls Placeable.place
, and statement order is the order of measurables passed to MeasurePolicy.measureefemoney
09/04/2023, 1:04 PMZach Klippenstein (he/him) [MOD]
09/05/2023, 6:09 PM