Skip to content

Commit c55334c

Browse files
add Offset to BeforeAfterImage
1 parent 1f01e48 commit c55334c

1 file changed

Lines changed: 18 additions & 44 deletions

File tree

image/src/main/java/com/smarttoolfactory/image/beforeafter/BeforeAfterImage.kt

Lines changed: 18 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,6 @@ import androidx.compose.ui.unit.*
2424
import com.smarttoolfactory.gesture.detectTransformGestures
2525
import com.smarttoolfactory.gesture.pointerMotionEvents
2626
import com.smarttoolfactory.image.ImageScope
27-
import com.smarttoolfactory.image.ImageScopeImpl
2827
import com.smarttoolfactory.image.getParentSize
2928
import com.smarttoolfactory.image.getScaledBitmapRect
3029

@@ -73,7 +72,7 @@ fun BeforeAfterImage(
7372
alpha: Float = DefaultAlpha,
7473
colorFilter: ColorFilter? = null,
7574
filterQuality: FilterQuality = DrawScope.DefaultFilterQuality,
76-
content: @Composable ImageScope.() -> Unit = {}
75+
content: @Composable BeforeAfterImageScope.() -> Unit = {}
7776
) {
7877

7978
val semantics = if (contentDescription != null) {
@@ -94,7 +93,6 @@ fun BeforeAfterImage(
9493
val bitmapWidth = beforeImage.width
9594
val bitmapHeight = beforeImage.height
9695

97-
9896
val (boxWidth: Int, boxHeight: Int) = getParentSize(bitmapWidth, bitmapHeight)
9997

10098
// Src is Bitmap, Dst is the container(Image) that Bitmap will be displayed
@@ -108,14 +106,20 @@ fun BeforeAfterImage(
108106
val imageWidth = bitmapWidth * scaleFactor.scaleX
109107
val imageHeight = bitmapHeight * scaleFactor.scaleY
110108

111-
var handlePosition by remember { mutableStateOf(imageWidth.coerceAtMost(boxWidth.toFloat()) / 2f) }
109+
var handlePosition by remember {
110+
mutableStateOf(
111+
Offset(
112+
x = imageWidth.coerceAtMost(boxWidth.toFloat()) / 2f,
113+
y = imageHeight.coerceAtMost(boxHeight.toFloat()) / 2f,
114+
)
115+
)
116+
}
112117

113118
var isHandleTouched by remember { mutableStateOf(false) }
114119

115120
var zoom by remember { mutableStateOf(1f) }
116121
var pan by remember { mutableStateOf(Offset.Zero) }
117122

118-
119123
val imageModifier = Modifier
120124
.clipToBounds()
121125
.pointerInput(Unit) {
@@ -140,11 +144,11 @@ fun BeforeAfterImage(
140144
val position = it.position
141145
val xPos = position.x
142146

143-
isHandleTouched = ((handlePosition - xPos) * (handlePosition - xPos) < 10000)
147+
isHandleTouched = ((handlePosition.x - xPos) * (handlePosition.x - xPos) < 10000)
144148
},
145149
onMove = {
146150
if (isHandleTouched) {
147-
handlePosition = it.position.x
151+
handlePosition = handlePosition.copy(x = it.position.x)
148152
it.consume()
149153
}
150154
},
@@ -206,7 +210,7 @@ private fun ImageLayout(
206210
constraints: Constraints,
207211
beforeImage: ImageBitmap,
208212
afterImage: ImageBitmap,
209-
handlePosition: Float,
213+
handlePosition: Offset,
210214
translateX: Float,
211215
zoom: Float,
212216
bitmapRect: IntRect,
@@ -217,7 +221,7 @@ private fun ImageLayout(
217221
alpha: Float = DefaultAlpha,
218222
colorFilter: ColorFilter? = null,
219223
filterQuality: FilterQuality = DrawScope.DefaultFilterQuality,
220-
content: @Composable ImageScope.() -> Unit
224+
content: @Composable BeforeAfterImageScope.() -> Unit
221225
) {
222226

223227
val density = LocalDensity.current
@@ -227,12 +231,13 @@ private fun ImageLayout(
227231

228232
// canvasWidthInDp, and canvasHeightInDp are Canvas dimensions coerced to Box size
229233
// that covers Canvas
230-
val imageScopeImpl = ImageScopeImpl(
234+
val imageScopeImpl = BeforeAfterImageScopeImpl(
231235
density = density,
232236
constraints = constraints,
233237
imageWidth = canvasWidthInDp,
234238
imageHeight = canvasHeightInDp,
235-
rect = bitmapRect
239+
rect = bitmapRect,
240+
touchPosition = handlePosition
236241
)
237242

238243
// width and height params for translating draw position if scaled Image dimensions are
@@ -247,8 +252,6 @@ private fun ImageLayout(
247252
alpha = alpha,
248253
width = imageWidth.toInt(),
249254
height = imageHeight.toInt(),
250-
canvasWidthInDp = canvasWidthInDp,
251-
canvasHeightInDp = canvasHeightInDp,
252255
colorFilter = colorFilter,
253256
filterQuality = filterQuality
254257
)
@@ -261,13 +264,11 @@ private fun ImageImpl(
261264
modifier: Modifier,
262265
beforeImage: ImageBitmap,
263266
afterImage: ImageBitmap,
264-
handlePosition: Float,
267+
handlePosition: Offset,
265268
translateX: Float,
266269
zoom: Float,
267270
width: Int,
268271
height: Int,
269-
canvasWidthInDp: Dp,
270-
canvasHeightInDp: Dp,
271272
alpha: Float = DefaultAlpha,
272273
colorFilter: ColorFilter? = null,
273274
filterQuality: FilterQuality = DrawScope.DefaultFilterQuality,
@@ -282,7 +283,7 @@ private fun ImageImpl(
282283
val canvasHeight = size.height
283284

284285
val touchPosition =
285-
(+width - canvasWidth) / 2f + (handlePosition / zoom).coerceIn(0f, canvasWidth)
286+
(+width - canvasWidth) / 2f + (handlePosition.x / zoom).coerceIn(0f, canvasWidth)
286287
.toInt()
287288

288289
// Translate to left or down when Image size is bigger than this canvas.
@@ -297,17 +298,9 @@ private fun ImageImpl(
297298
val maxX = (size.width * (zoom - 1) / 2f)
298299
val pan = (maxX - translateX) / zoom
299300

300-
println(
301-
"🔥 canvasWidth: $canvasWidth, bitmapWidth: $bitmapWidth, maxX: $maxX\n" +
302-
"touchPosition: $touchPosition, translateX: $translateX, pan: $pan, zoom: $zoom"
303-
)
304-
305-
306301
val srcOffsetX = ((pan + touchPosition) * bitmapWidth / width).toInt()
307302
val dstOffsetX = (pan + touchPosition).toInt()
308303

309-
println("🍏 srcOffsetX: $srcOffsetX, dstOffsetX: $dstOffsetX")
310-
311304
drawImage(
312305
afterImage,
313306
srcSize = IntSize(bitmapWidth, bitmapHeight),
@@ -328,24 +321,5 @@ private fun ImageImpl(
328321
)
329322
}
330323
}
331-
332-
Canvas(modifier = Modifier.size(canvasWidthInDp, canvasHeightInDp)) {
333-
334-
val canvasWidth = size.width
335-
336-
val imagePosition = handlePosition.coerceIn(0f, canvasWidth)
337-
338-
drawLine(
339-
Color.White,
340-
strokeWidth = 2.dp.toPx(),
341-
start = Offset(imagePosition, 0f),
342-
end = Offset(imagePosition, size.height)
343-
)
344-
drawCircle(
345-
color = Color.Red,
346-
center = Offset(imagePosition, size.height / 2),
347-
radius = 30f
348-
)
349-
}
350324
}
351325
}

0 commit comments

Comments
 (0)