Skip to content

Commit d0d40b1

Browse files
committed
add dividerBrush to allow setting gradiant color
1 parent 0156756 commit d0d40b1

File tree

2 files changed

+25
-4
lines changed

2 files changed

+25
-4
lines changed

app/src/main/java/com/smarttoolfactory/composebeforeafter/demo/BeforeAfterImageDemo.kt

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@ import androidx.compose.runtime.remember
2727
import androidx.compose.runtime.setValue
2828
import androidx.compose.ui.Modifier
2929
import androidx.compose.ui.draw.clip
30+
import androidx.compose.ui.graphics.Brush
3031
import androidx.compose.ui.graphics.Color
3132
import androidx.compose.ui.graphics.ImageBitmap
3233
import androidx.compose.ui.layout.ContentScale
@@ -103,7 +104,16 @@ fun BeforeAfterImageDemo() {
103104
.aspectRatio(4 / 3f),
104105
beforeImage = imageBefore,
105106
afterImage = imageAfter,
106-
contentScale = contentScale
107+
contentScale = contentScale,
108+
overlayStyle = OverlayStyle(
109+
dividerBrush = Brush.verticalGradient(
110+
listOf(
111+
Color.Red,
112+
Color.Blue,
113+
),
114+
),
115+
dividerWidth = 4.dp,
116+
)
107117
)
108118

109119
Spacer(modifier = Modifier.height(50.dp))

beforeafter/src/main/java/com/smarttoolfactory/beforeafter/DefaultOverlay.kt

Lines changed: 14 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ import androidx.compose.ui.Alignment
1717
import androidx.compose.ui.Modifier
1818
import androidx.compose.ui.draw.shadow
1919
import androidx.compose.ui.geometry.Offset
20+
import androidx.compose.ui.graphics.Brush
2021
import androidx.compose.ui.graphics.Color
2122
import androidx.compose.ui.graphics.Shape
2223
import androidx.compose.ui.platform.LocalDensity
@@ -45,6 +46,7 @@ internal fun DefaultOverlay(
4546

4647
val verticalThumbMove = overlayStyle.verticalThumbMove
4748
val dividerColor = overlayStyle.dividerColor
49+
val dividerBrush = overlayStyle.dividerBrush
4850
val dividerWidth = overlayStyle.dividerWidth
4951
val thumbBackgroundColor = overlayStyle.thumbBackgroundColor
5052
val thumbTintColor = overlayStyle.thumbTintColor
@@ -90,7 +92,14 @@ internal fun DefaultOverlay(
9092
) {
9193
Canvas(modifier = Modifier.fillMaxSize()) {
9294

93-
drawLine(
95+
dividerBrush?.let {
96+
drawLine(
97+
dividerBrush,
98+
strokeWidth = dividerWidth.toPx(),
99+
start = Offset(linePosition, 0f),
100+
end = Offset(linePosition, size.height)
101+
)
102+
} ?: drawLine(
94103
dividerColor,
95104
strokeWidth = dividerWidth.toPx(),
96105
start = Offset(linePosition, 0f),
@@ -117,8 +126,9 @@ internal fun DefaultOverlay(
117126
/**
118127
* Values for styling [DefaultOverlay]
119128
* @param verticalThumbMove when true thumb can move vertically based on user touch
120-
* @param dividerColor color if divider line
121-
* @param dividerWidth width if divider line
129+
* @param dividerColor color of divider line
130+
* @param dividerBrush brush to set color gradiant in divider line
131+
* @param dividerWidth width of divider line
122132
* @param thumbBackgroundColor background color of thumb [Icon]
123133
* @param thumbTintColor tint color of thumb [Icon]
124134
* @param thumbShape shape of thumb [Icon]
@@ -131,6 +141,7 @@ internal fun DefaultOverlay(
131141
@Immutable
132142
class OverlayStyle(
133143
val dividerColor: Color = Color.White,
144+
val dividerBrush: Brush? = null,
134145
val dividerWidth: Dp = 1.5.dp,
135146
val verticalThumbMove: Boolean = false,
136147
val thumbBackgroundColor: Color = Color.White,

0 commit comments

Comments
 (0)