I don’ undestand why alpha doesn’t change. Ok i cr...
# compose
a
I don’ undestand why alpha doesn’t change. Ok i created 3 image and i/d like change alpha moving a bar/percentage. // data
Copy code
data class ImageTransitionData(
    var imageList: List<Image> = emptyList()
)

data class Image(var src: Bitmap?, var alpha: Float = 0f)
// screen
Copy code
var pageState = viewModel.uiState.collectAsState().value
pageState.imageList.forEach { image ->
    Box(
        modifier = Modifier.fillMaxSize(),
        contentAlignment = Alignment.Center
    ) {
        BitmapImage2(
            image.src, image.alpha
        )
    }
}

@Composable
fun BitmapImage2(image: Bitmap?, alpha:Float) {


    val imageBitmap = remember {
        mutableStateOf<ImageBitmap?>(null)
    }

    image?.apply {
        imageBitmap.value = this.asImageBitmap()

    }
    imageBitmap.value?.let {
        Image(
            modifier = Modifier.fillMaxSize(),
            bitmap = it,
            contentDescription = "",
            contentScale = ContentScale.Fit,
            alpha = alpha
        )
    }

    Log.d("touch", "TOUCH ${ alpha}")
}
Ok this works.. screen shows first image in alpha 1f i want change first image alpha… so…
Copy code
private fun setPercentage(percentage: Float) {
    if(percentage >=0){

       var image = uiState.value.imageList.toMutableList()
        image.get(0).alpha = percentage /100

        uiState.value = uiState.value.copy(
            imageList= image.toList()
        )
        Log.d("touch", "TOUCH ${image.map { "Alpha:${it.alpha} - " }}")
    }

}
it’s strange , i changed array but i dont’see recomposition
🧵 6
🧵 4
Ok now it works.. but i have opposite effect… BitmapImage2 print continuer log | Log.d(“touch”, “TOUCH ${ alpha}“) and it doesn/t stop
z
I’m seeing a few things: 1. Your Image class has mutable properties that are not backed by snapshot state (eg mutableStateOf), so Compose has no way to know when their value changes. Make those properties snapshot state then you don’t need to copy the list at all in setPercentage, you can just update the image properties directly. Alternatively, make them immutable so when you copy the list you’re also making new copies of the changed Image objects. The principles in this post apply here. 2. The imageBitmap state in Bitmap Image2 is redundant - you’re setting it on every composition and then immediately reading its value, you don’t need the intermediate variable to be a state, or even be mutable, or even have a name:
Copy code
image?.asImageBitmap()?.let { Image(…) }