Skip to content

Commit 85676db

Browse files
add gradient rotation demo
1 parent ab9d452 commit 85676db

File tree

2 files changed

+141
-1
lines changed

2 files changed

+141
-1
lines changed

app/src/main/java/com/smarttoolfactory/composecolorsextended/MainActivity.kt

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ import com.google.accompanist.pager.HorizontalPager
1717
import com.google.accompanist.pager.PagerState
1818
import com.google.accompanist.pager.rememberPagerState
1919
import com.google.accompanist.systemuicontroller.rememberSystemUiController
20+
import com.smarttoolfactory.composecolorsextended.demo.GradientAngleDemo
2021
import com.smarttoolfactory.composecolorsextended.demo.MD2ColorSelectionDemo
2122
import com.smarttoolfactory.composecolorsextended.demo.MD3ColorShadeSelectionDemo
2223
import com.smarttoolfactory.composecolorsextended.ui.theme.ComposeColorsExtendedTheme
@@ -94,9 +95,10 @@ private fun HomeContent() {
9495
0 -> MD2ColorSelectionDemo {
9596
backgroundColor = it
9697
}
97-
else -> MD3ColorShadeSelectionDemo {
98+
1 -> MD3ColorShadeSelectionDemo {
9899
backgroundColor = it
99100
}
101+
else -> GradientAngleDemo()
100102
}
101103
}
102104
}
Lines changed: 138 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,138 @@
1+
package com.smarttoolfactory.composecolorsextended.demo
2+
3+
import androidx.compose.foundation.Canvas
4+
import androidx.compose.foundation.layout.*
5+
import androidx.compose.material.Slider
6+
import androidx.compose.material.Text
7+
import androidx.compose.runtime.*
8+
import androidx.compose.ui.Alignment
9+
import androidx.compose.ui.Modifier
10+
import androidx.compose.ui.graphics.Brush
11+
import androidx.compose.ui.graphics.Color
12+
import androidx.compose.ui.graphics.drawscope.DrawScope
13+
import androidx.compose.ui.text.font.FontWeight
14+
import androidx.compose.ui.unit.dp
15+
import androidx.compose.ui.unit.sp
16+
import com.smarttoolfactory.extendedcolors.GradientAngle
17+
import com.smarttoolfactory.extendedcolors.GradientOffset
18+
import com.smarttoolfactory.extendedcolors.MaterialColor.Blue400
19+
import kotlin.math.roundToInt
20+
21+
/**
22+
* Demo for creating gradients for different type of pickers or sliders
23+
*/
24+
@Composable
25+
fun GradientAngleDemo() {
26+
27+
val canvasModifier = Modifier.size(300.dp)
28+
29+
// Offsets for gradients based on selected angle
30+
var gradientOffset by remember {
31+
mutableStateOf(GradientOffset(GradientAngle.CW0))
32+
}
33+
34+
var angleSelection by remember { mutableStateOf(0f) }
35+
var angleText by remember { mutableStateOf("0 Degrees") }
36+
37+
38+
39+
Column(
40+
modifier = Modifier
41+
.fillMaxSize()
42+
.padding(8.dp),
43+
horizontalAlignment = Alignment.CenterHorizontally
44+
) {
45+
46+
Text(
47+
text = angleText,
48+
color = Blue400,
49+
modifier = Modifier
50+
.padding(8.dp),
51+
fontSize = 18.sp,
52+
fontWeight = FontWeight.Bold
53+
)
54+
55+
Slider(
56+
modifier = Modifier.height(50.dp),
57+
value = angleSelection,
58+
onValueChange = {
59+
angleSelection = it
60+
61+
gradientOffset = when (angleSelection.roundToInt()) {
62+
0 -> {
63+
angleText = "0 Degrees"
64+
GradientOffset(GradientAngle.CW0)
65+
}
66+
1 -> {
67+
angleText = "45 Degrees"
68+
GradientOffset(GradientAngle.CW45)
69+
}
70+
2 -> {
71+
angleText = "90 Degrees"
72+
GradientOffset(GradientAngle.CW90)
73+
}
74+
3 -> {
75+
angleText = "135 Degrees"
76+
GradientOffset(GradientAngle.CW135)
77+
}
78+
4 -> {
79+
angleText = "180 Degrees"
80+
GradientOffset(GradientAngle.CW180)
81+
}
82+
83+
5 -> {
84+
angleText = "225 Degrees"
85+
GradientOffset(GradientAngle.CW225)
86+
}
87+
6 -> {
88+
angleText = "270 Degrees"
89+
GradientOffset(GradientAngle.CW270)
90+
}
91+
else -> {
92+
angleText = "315 Degrees"
93+
GradientOffset(GradientAngle.CW315)
94+
}
95+
}
96+
},
97+
steps = 6,
98+
valueRange = 0f..7f
99+
)
100+
101+
CanvasWithTitle(
102+
modifier = canvasModifier,
103+
text = "Gradient Angle"
104+
) {
105+
val redGreenGradient = Brush.linearGradient(
106+
colors = listOf(Color.Red, Color.Green, Color.Blue),
107+
start = gradientOffset.start,
108+
end = gradientOffset.end
109+
)
110+
drawRect(redGreenGradient)
111+
}
112+
113+
}
114+
}
115+
116+
@Composable
117+
private fun CanvasWithTitle(
118+
modifier: Modifier = Modifier,
119+
text: String,
120+
onDraw: DrawScope.() -> Unit
121+
) {
122+
Column(
123+
modifier = Modifier
124+
.wrapContentWidth()
125+
) {
126+
127+
Text(
128+
text = text,
129+
color = Blue400,
130+
modifier = Modifier
131+
.padding(8.dp),
132+
fontSize = 18.sp,
133+
fontWeight = FontWeight.Bold
134+
)
135+
136+
Canvas(modifier = modifier, onDraw = onDraw)
137+
}
138+
}

0 commit comments

Comments
 (0)