1+ package com.smarttoolfactory.composeimage.demo
2+
3+ import androidx.compose.foundation.Canvas
4+ import androidx.compose.foundation.Image
5+ import androidx.compose.foundation.background
6+ import androidx.compose.foundation.layout.*
7+ import androidx.compose.foundation.shape.CircleShape
8+ import androidx.compose.foundation.shape.RoundedCornerShape
9+ import androidx.compose.material3.Text
10+ import androidx.compose.runtime.Composable
11+ import androidx.compose.ui.Modifier
12+ import androidx.compose.ui.draw.clip
13+ import androidx.compose.ui.draw.shadow
14+ import androidx.compose.ui.geometry.Offset
15+ import androidx.compose.ui.graphics.Color
16+ import androidx.compose.ui.graphics.ImageBitmap
17+ import androidx.compose.ui.graphics.painter.Painter
18+ import androidx.compose.ui.layout.ContentScale
19+ import androidx.compose.ui.platform.LocalContext
20+ import androidx.compose.ui.platform.LocalDensity
21+ import androidx.compose.ui.res.imageResource
22+ import androidx.compose.ui.res.painterResource
23+ import androidx.compose.ui.unit.IntOffset
24+ import androidx.compose.ui.unit.dp
25+ import androidx.compose.ui.unit.sp
26+ import com.smarttoolfactory.composeimage.R
27+ import com.smarttoolfactory.image.beforeafter.BeforeAfterImage
28+ import com.smarttoolfactory.image.beforeafter.BeforeAfterLayout
29+
30+ @Composable
31+ fun BeforeAfterImageDemo () {
32+ Column (
33+ modifier = Modifier
34+ .fillMaxSize()
35+ .padding(10 .dp)
36+ ) {
37+ val imageBefore = ImageBitmap .imageResource(
38+ LocalContext .current.resources, R .drawable.landscape5
39+ )
40+
41+ val imageAfter = ImageBitmap .imageResource(
42+ LocalContext .current.resources, R .drawable.landscape5_after
43+ )
44+
45+ val painter: Painter = painterResource(id = R .drawable.baseline_swap_horiz_24)
46+
47+ val density = LocalDensity .current
48+
49+ BeforeAfterImage (
50+ modifier = Modifier
51+ .clip(RoundedCornerShape (10 .dp))
52+ // .background(Color.LightGray, RoundedCornerShape(10.dp))
53+ .fillMaxWidth()
54+ .aspectRatio(4 / 3f ),
55+ beforeImage = imageBefore,
56+ afterImage = imageAfter,
57+ contentScale = ContentScale .FillBounds
58+ ) {
59+
60+ val handlePosition = touchPosition.x
61+ val posY: Int
62+
63+ val realPos = handlePosition - with (density) {
64+ posY = (imageHeight / 3 ).roundToPx()
65+ imageWidth.toPx() / 2
66+ }
67+
68+
69+ Canvas (modifier = Modifier .size(imageWidth, imageHeight)) {
70+
71+ val canvasWidth = size.width
72+
73+ val imagePosition = handlePosition.coerceIn(0f , canvasWidth)
74+
75+ drawLine(
76+ Color .White ,
77+ strokeWidth = 1.5 .dp.toPx(),
78+ start = Offset (imagePosition, 0f ),
79+ end = Offset (imagePosition, size.height)
80+ )
81+
82+ }
83+
84+ Image (
85+ painter = painter,
86+ contentDescription = null ,
87+ modifier = Modifier
88+ .offset {
89+ IntOffset (realPos.toInt(), posY)
90+ }
91+ .shadow(2 .dp, CircleShape )
92+ .background(Color .White )
93+ .padding(2 .dp)
94+ )
95+ }
96+
97+ Spacer (modifier = Modifier .height(20 .dp))
98+
99+ BeforeAfterLayout (modifier = Modifier
100+ .fillMaxWidth()
101+ .aspectRatio(4 / 3f ),
102+ before = {
103+ Image (
104+ painter = painterResource(id = R .drawable.landscape5),
105+ contentDescription = " " ,
106+ contentScale = ContentScale .FillBounds
107+ )
108+
109+
110+ },
111+ after = {
112+ Image (
113+ painter = painterResource(id = R .drawable.landscape5_after),
114+ contentDescription = " " ,
115+ contentScale = ContentScale .FillBounds
116+ )
117+ }
118+ )
119+
120+
121+ Spacer (modifier = Modifier .height(20 .dp))
122+
123+ BeforeAfterLayout (modifier = Modifier ,
124+ before = {
125+ Text (text = " Hello World" , fontSize = 60 .sp, color = Color .Red )
126+
127+ },
128+ after = {
129+ Text (text = " Hello World" , fontSize = 60 .sp, color = Color .Green )
130+ }
131+ )
132+ }
133+ }
0 commit comments