I have an offset value that is calculated or reset as
var offset by remember(key1 = contentScale, key2 = imageBitmap) {
mutableStateOf(Offset.Unspecified)
}
This is basically a color detector from an Image
but when ContentScale
of the custom Image
I wrote or ImageBitmap
changes I want to reset it to Offset.Unspecified
so it doesn't draw markers outside of area that is image being drawn, the grey area in gif.
This is simplified but reproducible code, I
use Jetpack Compose 1.2.0-beta01
@Composable
fun ImageColorDetector(
modifier: Modifier = Modifier,
imageBitmap: ImageBitmap,
contentScale: ContentScale = ContentScale.FillBounds,
onColorChange: (ColorData) -> Unit
) {
var offset by remember(key1 = contentScale, key2 = imageBitmap) {
mutableStateOf(Offset.Unspecified)
}
println(
"✊ImageColorDetector() imageBitmap: $imageBitmap\n" +
"bitmap: ${imageBitmap.asAndroidBitmap()}\n" +
"width: ${imageBitmap.width}, height: ${imageBitmap.height}\n" +
"offset: $offset\n" +
"contentScale: $contentScale\n\n"
)
Box(
modifier = modifier
.pointerInput(Unit) {
detectDragGestures { change, dragAmount ->
offset = change.position
println("onTouchEvent: offset: $offset")
}
}
) {
Image(
bitmap = imageBitmap,
contentScale = contentScale,
contentDescription = null
)
}
Text("Offset: $offset")
}
In detectDragGestures
I see that offset is updated on drag gesture and when ImageBitmap
or ContentScale
changes, I saw in parent and this composable that they are not the same instances, so remember
should be recalculate with new keys but it doesn't seem to work.
I/System.out: ⛺️ ImageColorDetectionDemo imageBitmap: androidx.compose.ui.graphics.AndroidImageBitmap@5552c68
I/System.out: bitmap: android.graphics.Bitmap@93bc881
I/System.out: width: 236, height: 394
I/System.out: contentScale: androidx.compose.ui.layout.ContentScale$Companion$Crop$1@3226649
I/System.out:
I/System.out: ✊ImageColorDetector() imageBitmap: androidx.compose.ui.graphics.AndroidImageBitmap@5552c68
I/System.out: bitmap: android.graphics.Bitmap@93bc881
I/System.out: width: 236, height: 394
I/System.out: offset: Offset(0.0, 0.0)
I/System.out: contentScale: androidx.compose.ui.layout.ContentScale$Companion$Crop$1@3226649
I/System.out:
I/System.out: ⛺️ ImageColorDetectionDemo imageBitmap: androidx.compose.ui.graphics.AndroidImageBitmap@7607f98
I/System.out: bitmap: android.graphics.Bitmap@286d3f1
I/System.out: width: 736, height: 920
I/System.out: contentScale: androidx.compose.ui.layout.ContentScale$Companion$Crop$1@3226649
I/System.out:
I/System.out: ✊ImageColorDetector() imageBitmap: androidx.compose.ui.graphics.AndroidImageBitmap@7607f98
I/System.out: bitmap: android.graphics.Bitmap@286d3f1
I/System.out: width: 736, height: 920
I/System.out: offset: Offset(0.0, 0.0)
I/System.out: contentScale: androidx.compose.ui.layout.ContentScale$Companion$Crop$1@3226649
ContentScale
is from default image code; each is an Object
, so changing from Crop
to Fit
means a key
is set.