@@ -27,6 +27,7 @@ import androidx.compose.runtime.remember
2727import androidx.compose.runtime.setValue
2828import androidx.compose.runtime.snapshots.SnapshotStateList
2929import androidx.compose.ui.Alignment
30+ import androidx.compose.ui.BiasAlignment
3031import androidx.compose.ui.Modifier
3132import androidx.compose.ui.draw.drawWithContent
3233import androidx.compose.ui.geometry.Offset
@@ -49,15 +50,41 @@ import com.smarttoolfactory.tutorial1_1basics.R
4950import java.util.UUID
5051
5152
52- private fun calculateRect (srcSize : Size , dstSize : Size , contentScale : ContentScale ): Rect {
53+ private fun calculateRect (
54+ srcSize : Size ,
55+ dstSize : Size ,
56+ contentScale : ContentScale ,
57+ alignment : Alignment
58+ ): Rect {
5359 val scaleFactor = contentScale.computeScaleFactor(srcSize, dstSize)
5460
5561 val scaledSrcSize = Size (
5662 srcSize.width * scaleFactor.scaleX,
5763 srcSize.height * scaleFactor.scaleY
5864 )
59- val left = ((dstSize.width - scaledSrcSize.width) / 2 ).coerceAtLeast(0f )
60- val top = ((dstSize.height - scaledSrcSize.height) / 2 ).coerceAtLeast(0f )
65+
66+ val biasAlignment: BiasAlignment = alignment as BiasAlignment
67+
68+ // - Left, 0 Center, 1 Right
69+ val horizontalBias: Float = biasAlignment.horizontalBias
70+ // -1 Top, 0 Center, 1 Bottom
71+ val verticalBias: Float = biasAlignment.verticalBias
72+
73+
74+ val horizontalGap = ((dstSize.width - scaledSrcSize.width) / 2 ).coerceAtLeast(0f )
75+ val verticalGap = ((dstSize.height - scaledSrcSize.height) / 2 ).coerceAtLeast(0f )
76+
77+ val left = when (horizontalBias) {
78+ - 1f -> 0f
79+ 0f -> horizontalGap
80+ else -> horizontalGap * 2
81+ }
82+
83+ val top = when (verticalBias) {
84+ - 1f -> 0f
85+ 0f -> verticalGap
86+ else -> verticalGap * 2
87+ }
6188
6289 val right = (left + scaledSrcSize.width).coerceAtMost(dstSize.width)
6390 val bottom = (top + scaledSrcSize.height).coerceAtMost(dstSize.height)
@@ -77,7 +104,7 @@ data class ImgAnnotation(
77104@Preview
78105@Composable
79106fun ImageWithMarkersSample () {
80- val imageBitmap: ImageBitmap = ImageBitmap .imageResource(R .drawable.landscape3 )
107+ val imageBitmap: ImageBitmap = ImageBitmap .imageResource(R .drawable.landscape11 )
81108
82109 val imgAnnotationList = remember {
83110 mutableStateListOf<ImgAnnotation >()
@@ -168,6 +195,7 @@ fun ImageWithMarkersSample() {
168195 .fillMaxWidth()
169196 .aspectRatio(5 / 3f ),
170197 contentScale = ContentScale .Fit ,
198+ alignment = Alignment .BottomEnd ,
171199 imgAnnotationList = imgAnnotationList,
172200 imageBitmap = imageBitmap
173201 ) {
@@ -185,6 +213,7 @@ fun ImageWithMarkersSample() {
185213private fun ImageWithMarkers (
186214 modifier : Modifier = Modifier ,
187215 contentScale : ContentScale ,
216+ alignment : Alignment = Alignment .Center ,
188217 imgAnnotationList : SnapshotStateList <ImgAnnotation >,
189218 imageBitmap : ImageBitmap ,
190219 onClick : (ImgAnnotation ) -> Unit
@@ -236,7 +265,7 @@ private fun ImageWithMarkers(
236265 val srcSize =
237266 Size (imageBitmap.width.toFloat(), imageBitmap.height.toFloat())
238267
239- rect = calculateRect(srcSize, dstSize, contentScale)
268+ rect = calculateRect(srcSize, dstSize, contentScale, alignment )
240269
241270 val rectWidth = rect.width
242271 val rectHeight = rect.height
@@ -246,6 +275,7 @@ private fun ImageWithMarkers(
246275 },
247276 bitmap = imageBitmap,
248277 contentScale = contentScale,
278+ alignment = alignment,
249279 contentDescription = null
250280 )
251281
0 commit comments