Como puedes reordenar mi codigo jetpack compose para eliminar los repintados…
- User
Como puedes reordenar mi codigo jetpack compose para eliminar los repintados innecesarios, centrate como deberias de agruparse los componentes y la logica para evitar recomposisiones : class MainActivity : ComponentActivity() { override fun onCreate(savedInstanceState: Bundle?) { super.onCreate(savedInstanceState) enableEdgeToEdge() setContent { MyLyricsTheme { Surface( modifier = Modifier.fillMaxSize(), //color = MaterialTheme.colors.background // Color de fondo del tema ) { // Contenido principal de tu aplicación Column( modifier = Modifier.padding(16.dp), // Agrega padding //verticalArrangement = Arrangement.Center, horizontalAlignment = Alignment.CenterHorizontally ) { AnimationExample() // Más contenido aquí } } } } } }
@Composable fun AnimationExample() { val coroutineScope = rememberCoroutineScope()
// Solo necesitamos recordar arreglos animables y opacidades una vez val grupArr = remember { List(5) { index -> Animatable(100f + index * 100f) } }// val arrOpac = remember { // List(5) { Animatable(0.2f + it * 0.2f) } // } val initialOpacities = listOf(0f, 0.3f, 1f, 0.35f, 0.01f) val arrOpac = remember { List(5) { Animatable(initialOpacities[it]) } } val letArr = arrayOf("Hola", "como", "estás", "bien", "Lelith")
var isAnimating = remember { mutableStateOf(false) } val animateee = { if (!isAnimating.value){ // No hacer nada si ya está animando isAnimating.value = true coroutineScope.launch { for (i in grupArr.indices) { launch { // Calcular nueva opacidad según los valores alternantes val newOpacity = when (arrOpac[i].value) { 0f -> 0.01f 0.3f -> 0f 1f -> 0.3f 0.35f -> 1f else -> 0.35f } arrOpac[i].animateTo( targetValue = newOpacity, animationSpec = tween(durationMillis = 300, easing = LinearEasing) ) if (i == 4){ //Log.i("isanimating", "siiii") isAnimating.value = false // Resetea el estado al finalizar la animación } } launch { // Cambiar la posición animada de los elementos val newPosition = if (grupArr[i].value != 100f) grupArr[i].value - 100f else 500f grupArr[i].animateTo( targetValue = newPosition, animationSpec = if (newPosition == 500f) tween(0) else tween(durationMillis = 300, easing = LinearEasing) ) } } } } } Column( Modifier.fillMaxSize(), horizontalAlignment = Alignment.CenterHorizontally ) { Box( modifier = Modifier .size(360.dp, 100.dp) .clip(RoundedCornerShape(20.dp)) .background(color = Color.Black) ) // El botón no debe depender del estado interno de las animaciones. Mybutton ( animatt = {animateee()} ) Box( modifier = Modifier .size(360.dp, 400.dp) .clip(RoundedCornerShape(30.dp)) .background(color = Color.DarkGray) ) { // Textos animados for (i in 0 until grupArr.size) { AnimatedText( text = letArr[i], yPosition = {grupArr[i].value}, alpha = {arrOpac[i].value} ) } } } - Assistant
- User
por que me sale error al usar "by": Property delegate must have a 'getValue(Nothing?, KMutableProperty0<>)' method. None of the following functions is applicable: fun Lazy.getValue(thisRef: Any?, property: KProperty<>): T fun KProperty0.getValue(thisRef: Any?, property: KProperty<>): V fun <T, V> KProperty1<T, V>.getValue(thisRef: T, property: KProperty<>): V fun <V, V1 : V> Map<in String, V>.getValue(thisRef: Any?, property: KProperty<>): V1 fun <V, V1 : V> MutableMap<in String, out V>.getValue(thisRef: Any?, property: KProperty<>): V1 fun <K, V> Map<K, V>.getValue(key: K): V Type 'androidx. compose. runtime. MutableState<kotlin. Boolean>' has no method 'setValue(Nothing?, KMutableProperty0<*>, ERROR CLASS: Ambiguity: getValue, [kotlin/ getValue, kotlin/ getValue, kotlin/ getValue, kotlin/ collections/ getValue, kotlin/ collections/ getValue, kotlin/ collections/ getValue])', so it cannot serve as a delegate for var (read-write property).
- Assistant
El error que estás viendo se debe a que el operador
byno se puede usar directamente con un objeto de tipoMutableState, que es lo que estás intentando hacer. En su lugar, debes acceder al valor deMutableStatedirectamente utilizando la propiedadvalue.Aquí te muestro cómo puedes corregirlo:
@Composable fun AnimationExample() { val coroutineScope = rememberCoroutineScope() val grupArr = remember { List(5) { Animatable(100f + it * 100f) } } val initialOpacities = listOf(0f, 0.3f, 1f, 0.35f, 0.01f) val arrOpac = remember { List(5) { Animatable(initialOpacities[it]) } } val letArr = remember { arrayOf("Hola", "como", "estás", "bien", "Lelith") } // Cambiar 'isAnimating' para no usar 'by' val isAnimating = remember { mutableStateOf(false) } val animateee = { if (!isAnimating.value) { isAnimating.value = true coroutineScope.launch { for (i in grupArr.indices) { launch { animateOpacity(i, arrOpac) } launch { animatePosition(i, grupArr) } } isAnimating.value = false } } } // ... resto de tu código } - User
es normal que en el inspector layout de android studio muestre que mis animaciones se recompongan 8 veces cada que hago click en el buton
- Assistant
- User
los textos de mi app cambia su opacidad cuando se desplazan eso se ejecuta al presionar el boton, es normal esas recomposiciones por el cambio de opacidad
- Assistant