1+ package progressindicator
2+
3+ import androidx.compose.animation.core.AnimationSpec
4+ import androidx.compose.animation.core.animateFloatAsState
5+ import androidx.compose.foundation.Canvas
6+ import androidx.compose.foundation.progressSemantics
7+ import androidx.compose.runtime.Composable
8+ import androidx.compose.runtime.getValue
9+ import androidx.compose.ui.Modifier
10+ import androidx.compose.ui.geometry.Offset
11+ import androidx.compose.ui.graphics.Color
12+ import androidx.compose.ui.unit.Dp
13+ import androidx.compose.ui.unit.dp
14+
15+ /* *
16+ * Created By Kevin Zou On 2023/9/13
17+ */
18+
19+ /* *
20+ * Determinate <a href="https://material.io/components/progress-indicators#linear-progress-indicators" class="external" target="_blank">Material Design linear progress indicator</a>.
21+ *
22+ * Progress indicators express an unspecified wait time or display the length of a process.
23+ *
24+ * @param progress The progress of this progress indicator, where 0.0 represents no progress and 1.0
25+ * represents full progress. Values outside of this range are coerced into the range.
26+ * @param progressBarColor The color of the progress indicator.
27+ * @param cornerRadius The corner radius of the progress indicator.
28+ * @param trackColor The color of the background behind the indicator, visible when the
29+ * progress has not reached that area of the overall indicator yet.
30+ * @param thumbRadius The radius of the thumb of the progress indicator.
31+ * @param thumbColor The color of the thumb of the progress indicator.
32+ * @param thumbOffset The offset of the thumb of the progress indicator. It determines the center of the
33+ * thumb. If the offset is zero, the center of the thumb will be at the end of the progress. By default, it is
34+ * set to [thumbRadius] so that it will coerce into the progressbar.
35+ * @param animationSpec The animation specifics for progress change.
36+ */
37+ @Composable
38+ fun SimpleProgressIndicatorWithAnim (
39+ modifier : Modifier = Modifier ,
40+ progress : Float = 0.7f,
41+ progressBarColor : Color = Color .Red ,
42+ cornerRadius : Dp = 0.dp,
43+ trackColor : Color = Color (0XFFFBE8E8),
44+ thumbRadius : Dp = 0.dp,
45+ thumbColor : Color = Color .White ,
46+ thumbOffset : Dp = thumbRadius,
47+ animationSpec : AnimationSpec <Float > = SimpleProgressIndicatorDefaults .SimpleProgressAnimationSpec ,
48+ ) {
49+ val mProgress: Float by animateFloatAsState(
50+ targetValue = progress,
51+ animationSpec = animationSpec
52+ )
53+ SimpleProgressIndicator (
54+ modifier,
55+ mProgress,
56+ progressBarColor,
57+ cornerRadius,
58+ trackColor,
59+ thumbRadius,
60+ thumbColor,
61+ thumbOffset
62+ )
63+ }
64+
65+
66+ /* *
67+ * Determinate <a href="https://material.io/components/progress-indicators#linear-progress-indicators" class="external" target="_blank">Material Design linear progress indicator</a>.
68+ *
69+ * Progress indicators express an unspecified wait time or display the length of a process.
70+ *
71+ * By default there is no animation between [progress] values. You can use
72+ * [SimpleProgressIndicatorWithAnim] to animate progress.
73+ *
74+ * @param progress The progress of this progress indicator, where 0.0 represents no progress and 1.0
75+ * represents full progress. Values outside of this range are coerced into the range.
76+ * @param progressBarColor The color of the progress indicator.
77+ * @param cornerRadius The corner radius of the progress indicator.
78+ * @param trackColor The color of the background behind the indicator, visible when the
79+ * progress has not reached that area of the overall indicator yet.
80+ * @param thumbRadius The radius of the thumb of the progress indicator.
81+ * @param thumbColor The color of the thumb of the progress indicator.
82+ * @param thumbOffset The offset of the thumb of the progress indicator. It determines the center of the
83+ * thumb. If the offset is zero, the center of the thumb will be at the end of the progress. By default, it is
84+ * set to [thumbRadius] so that it will coerce into the progressbar.
85+ */
86+ @Composable
87+ fun SimpleProgressIndicator (
88+ modifier : Modifier = Modifier ,
89+ progress : Float = 0.7f,
90+ progressBarColor : Color = Color .Red ,
91+ cornerRadius : Dp = 0.dp,
92+ trackColor : Color = Color (0XFFFBE8E8),
93+ thumbRadius : Dp = 0.dp,
94+ thumbColor : Color = Color .White ,
95+ thumbOffset : Dp = thumbRadius
96+ ) {
97+ Canvas (modifier.progressSemantics(progress)) {
98+ val progressWidth = size.width * progress
99+ drawLinearIndicatorBackground(trackColor, cornerRadius)
100+ drawLinearIndicator(progress, progressBarColor, cornerRadius)
101+ val thumbCenter = progressWidth - thumbOffset.toPx()
102+ if (thumbCenter > 0 ) {
103+ drawThumb(
104+ thumbRadius,
105+ thumbColor,
106+ Offset (progressWidth - thumbOffset.toPx(), size.height / 2f )
107+ )
108+ }
109+
110+ }
111+ }
0 commit comments