forked from SimformSolutionsPvtLtd/SSComposeCookBook
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathBasicCanvasExampleActivity.kt
366 lines (351 loc) · 13.3 KB
/
BasicCanvasExampleActivity.kt
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
package com.jetpack.compose.learning.canvas
import android.graphics.Paint
import android.os.Bundle
import androidx.activity.ComponentActivity
import androidx.activity.compose.setContent
import androidx.compose.animation.core.Animatable
import androidx.compose.animation.core.FastOutSlowInEasing
import androidx.compose.animation.core.LinearEasing
import androidx.compose.animation.core.RepeatMode
import androidx.compose.animation.core.animateFloat
import androidx.compose.animation.core.infiniteRepeatable
import androidx.compose.animation.core.rememberInfiniteTransition
import androidx.compose.animation.core.tween
import androidx.compose.foundation.Canvas
import androidx.compose.foundation.layout.Column
import androidx.compose.foundation.layout.requiredHeight
import androidx.compose.foundation.layout.fillMaxWidth
import androidx.compose.foundation.layout.padding
import androidx.compose.foundation.layout.requiredSize
import androidx.compose.foundation.layout.Row
import androidx.compose.foundation.rememberScrollState
import androidx.compose.foundation.verticalScroll
import androidx.compose.material.Button
import androidx.compose.material.IconButton
import androidx.compose.material.Scaffold
import androidx.compose.material.Text
import androidx.compose.material.TopAppBar
import androidx.compose.material.Icon
import androidx.compose.material.icons.Icons
import androidx.compose.material.icons.filled.ArrowBack
import androidx.compose.runtime.Composable
import androidx.compose.runtime.LaunchedEffect
import androidx.compose.runtime.mutableStateOf
import androidx.compose.runtime.remember
import androidx.compose.runtime.getValue
import androidx.compose.runtime.setValue
import androidx.compose.ui.Alignment
import androidx.compose.ui.Modifier
import androidx.compose.ui.geometry.Offset
import androidx.compose.ui.geometry.Size
import androidx.compose.ui.graphics.Color
import androidx.compose.ui.graphics.graphicsLayer
import androidx.compose.ui.graphics.PathEffect
import androidx.compose.ui.graphics.StrokeCap
import androidx.compose.ui.graphics.drawscope.Stroke
import androidx.compose.ui.graphics.drawscope.drawIntoCanvas
import androidx.compose.ui.graphics.drawscope.inset
import androidx.compose.ui.graphics.drawscope.rotate
import androidx.compose.ui.graphics.toArgb
import androidx.compose.ui.graphics.nativeCanvas
import androidx.compose.ui.unit.dp
import com.jetpack.compose.learning.theme.AppThemeState
import com.jetpack.compose.learning.theme.BaseView
import com.jetpack.compose.learning.theme.SystemUiController
import kotlinx.coroutines.launch
private const val START_ANGLE = 135f
private const val SWEEP_ANGLE = 270f
private const val ARC_RADIUS = 140f
private const val STROKE_SIZE = 25f
/**
* This activity contains basic example related to canvas
* Examples included common shapes, progress bar and a gauge meter
*/
class BasicCanvasExampleActivity : ComponentActivity() {
private val paint = Paint().apply {
strokeCap = Paint.Cap.ROUND
strokeWidth = 5f
color = Color.Black.toArgb()
textSize = 40f
}
private val currentSpeedPaint = Paint().apply {
strokeCap = Paint.Cap.ROUND
strokeWidth = 5f
color = Color.Black.toArgb()
textSize = 120f
}
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContent {
val systemUiController = remember { SystemUiController(window) }
val appTheme = remember { mutableStateOf(AppThemeState()) }
BaseView(appTheme.value, systemUiController) {
Scaffold(topBar = {
TopAppBar(
title = { Text("Basic Example") },
navigationIcon = {
IconButton(onClick = { onBackPressed() }) {
Icon(Icons.Filled.ArrowBack, contentDescription = null)
}
}
)
}) {
val scrollState = rememberScrollState()
Column(
Modifier
.padding(
start = 8.dp,
end = 8.dp,
top = it.calculateTopPadding(),
bottom = it.calculateBottomPadding()
)
.verticalScroll(scrollState)
.fillMaxWidth(),
horizontalAlignment = Alignment.CenterHorizontally
) {
VerticalSpace()
Title("Basic Example:")
BasicCanvasExample()
VerticalSpace()
Title("Progress Indicator:")
LoadingCircle()
VerticalSpace()
Title("Gauge Meter:")
GaugeMeterExample()
}
}
}
}
}
/**
* Basic example containing the common shapes and their usage
*/
@Composable
fun BasicCanvasExample(modifier: Modifier = Modifier) {
val radiusAnimation = remember { Animatable(0f) }
LaunchedEffect(radiusAnimation) {
radiusAnimation.animateTo(
360f,
animationSpec = tween(2000, easing = LinearEasing)
)
}
val strokeWidth = 8f
val stroke = Stroke(width = strokeWidth)
Canvas(
modifier = modifier
.requiredSize(200.dp)
.padding(8.dp)
) {
val canvasWidth = size.width
val canvasHeight = size.height
drawRect(Color.Red, style = stroke, size = Size(canvasWidth, canvasHeight))
inset(100f + strokeWidth) {
val innerCanvasWidth = size.width
val innerCanvasHeight = size.height
drawLine(
color = Color.Green,
strokeWidth = strokeWidth,
cap = StrokeCap.Round,
start = Offset(0f, 0f),
end = Offset(innerCanvasWidth, innerCanvasHeight)
)
drawLine(
color = Color.Green,
strokeWidth = strokeWidth,
cap = StrokeCap.Round,
start = Offset(innerCanvasWidth, 0f),
end = Offset(0f, innerCanvasHeight)
)
}
inset(50f) {
drawArc(Color.Blue, 0f, radiusAnimation.value, false, style = stroke)
}
}
}
/**
* Progress indicator with infinite animation transition
*/
@Composable
fun LoadingCircle(modifier: Modifier = Modifier) {
val radius = 180f
val infiniteTransition = rememberInfiniteTransition()
val rotationAnim by infiniteTransition.animateFloat(
initialValue = 0F,
targetValue = 360F,
animationSpec = infiniteRepeatable(
animation = tween(durationMillis = 3000, easing = LinearEasing)
)
)
val radiusAnim by infiniteTransition.animateFloat(
initialValue = 0f,
targetValue = radius,
animationSpec = infiniteRepeatable(tween(2000), RepeatMode.Reverse)
)
val stroke = Stroke(
width = 8f,
pathEffect = PathEffect.dashPathEffect(floatArrayOf(10f, 10f))
)
Canvas(
modifier = modifier
.padding(8.dp)
.requiredHeight(radius.dp)
.graphicsLayer {
rotationZ = rotationAnim
}
) {
val canvasHalfWidth = size.width / 2
val canvasHalfHeight = size.height / 2
drawCircle(
Color.Red, radiusAnim,
Offset(canvasHalfWidth, canvasHalfHeight),
style = stroke,
)
drawCircle(
Color.Green, radiusAnim / 1.5f,
Offset(canvasHalfWidth, canvasHalfHeight),
style = stroke,
)
drawCircle(
Color.Blue, radiusAnim / 3,
Offset(canvasHalfWidth, canvasHalfHeight),
style = stroke,
)
}
}
/**
* Gauge meter created with canvas
* This example uses basic shapes + animation to achieve the gauge meter effects
*/
@Composable
fun GaugeMeterExample() {
val startSpeed = 0
val endSpeed = 100
var currentSpeed by remember { mutableStateOf(45) }
Column(horizontalAlignment = Alignment.CenterHorizontally) {
GaugeMeter(startSpeed, endSpeed, currentSpeed)
VerticalSpace()
Row {
Button(onClick = {
if (currentSpeed >= endSpeed) {
currentSpeed = endSpeed
return@Button
}
currentSpeed += 5
}) {
Text("Increase Speed")
}
HorizontalSpace()
Button(onClick = {
if (currentSpeed <= startSpeed) {
currentSpeed = startSpeed
return@Button
}
currentSpeed -= 5
}) {
Text("Decrease Speed")
}
}
VerticalSpace()
}
}
@Composable
fun GaugeMeter(
startSpeed: Int,
endSpeed: Int,
currentSpeed: Int,
numOfDivision: Int = 10,
) {
val numOfLines = numOfDivision + 1
val divisionValue = (endSpeed - startSpeed) / numOfDivision
val floatAnimValues = (0 until numOfLines).map { remember { Animatable(1F) } }
LaunchedEffect(floatAnimValues) {
(0 until numOfLines).map { index ->
launch {
floatAnimValues[index].animateTo(
targetValue = 0F,
animationSpec = tween(
durationMillis = 1000,
easing = FastOutSlowInEasing,
delayMillis = 350 * index
)
)
}
}
}
val animateFloat = remember { Animatable(0f) }
LaunchedEffect(animateFloat) {
animateFloat.animateTo(
targetValue = 1f,
animationSpec = tween(durationMillis = 3000, easing = LinearEasing)
)
}
val animateCurrentSpeedFloat = remember { Animatable(0f) }
LaunchedEffect(animateFloat) {
animateCurrentSpeedFloat.animateTo(
targetValue = 1f,
animationSpec = tween(4000)
)
}
val measureText = currentSpeedPaint.measureText(currentSpeed.toString())
Canvas(
modifier = Modifier
.requiredSize(300.dp)
.padding(8.dp)
) {
rotate(START_ANGLE + 90f) {
var startAngle = 0f
repeat(numOfLines) { index ->
rotate(degrees = startAngle) {
drawLine(
color = Color.Black,
alpha = if (floatAnimValues[index].value < 1f) 1f else 0f,
strokeWidth = 4F,
cap = StrokeCap.Round,
start = Offset(size.width / 2, ARC_RADIUS - STROKE_SIZE),
end = Offset(size.width / 2, STROKE_SIZE * 3)
)
drawIntoCanvas {
it.nativeCanvas.drawText(
"${startSpeed + (divisionValue * index)}",
(size.width / 2) - STROKE_SIZE, STROKE_SIZE * 2,
paint
)
}
}
startAngle += SWEEP_ANGLE / numOfDivision
}
}
drawIntoCanvas {
it.nativeCanvas.drawText(
"$currentSpeed",
(size.width / 2) - measureText / 2, size.height / 2,
currentSpeedPaint
)
}
inset(ARC_RADIUS) {
drawArc(
Color.Gray,
START_ANGLE,
SWEEP_ANGLE * animateFloat.value,
false,
style = Stroke(width = STROKE_SIZE, cap = StrokeCap.Round),
)
drawArc(
Color.Red,
START_ANGLE,
getCurrentSpeedAngle(
currentSpeed,
startSpeed,
endSpeed
) * animateCurrentSpeedFloat.value,
false,
style = Stroke(width = STROKE_SIZE, cap = StrokeCap.Round),
)
}
}
}
private fun getCurrentSpeedAngle(currentSpeed: Int, startSpeed: Int, endSpeed: Int): Float {
val newValue = (endSpeed.toFloat() - startSpeed.toFloat()) / currentSpeed.toFloat()
return SWEEP_ANGLE / newValue
}
}