Skip to content

Commit e06aed8

Browse files
add Tutorial6_35MultiColoredLineChart
1 parent 87727cf commit e06aed8

File tree

2 files changed

+134
-2
lines changed

2 files changed

+134
-2
lines changed

Tutorial1-1Basics/src/main/java/com/smarttoolfactory/tutorial1_1basics/chapter6_graphics/Tutorial6_1_3CanvasPaths.kt

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -680,13 +680,13 @@ private fun DrawQuad() {
680680
) {
681681
path1.reset()
682682
path1.moveTo(x0, y0)
683-
path1.quadraticBezierTo(x1 = x1, y1 = y1, x2 = x2, y2 = y2)
683+
path1.quadraticTo(x1 = x1, y1 = y1, x2 = x2, y2 = y2)
684684

685685
// relativeQuadraticBezierTo draws quadraticBezierTo by adding offset
686686
// instead of setting absolute position
687687
path2.reset()
688688
path2.moveTo(x0, y0)
689-
path2.relativeQuadraticBezierTo(dx1 = x1 - x0, dy1 = y1 - y0, dx2 = x2 - x0, dy2 = y2 - y0)
689+
path2.relativeQuadraticTo(dx1 = x1 - x0, dy1 = y1 - y0, dx2 = x2 - x0, dy2 = y2 - y0)
690690

691691

692692
drawPath(
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,132 @@
1+
package com.smarttoolfactory.tutorial1_1basics.chapter6_graphics
2+
3+
import androidx.compose.foundation.Canvas
4+
import androidx.compose.foundation.layout.Column
5+
import androidx.compose.foundation.layout.fillMaxSize
6+
import androidx.compose.foundation.layout.fillMaxWidth
7+
import androidx.compose.foundation.layout.height
8+
import androidx.compose.foundation.layout.padding
9+
import androidx.compose.runtime.Composable
10+
import androidx.compose.runtime.remember
11+
import androidx.compose.ui.Modifier
12+
import androidx.compose.ui.geometry.Offset
13+
import androidx.compose.ui.geometry.Size
14+
import androidx.compose.ui.graphics.BlendMode
15+
import androidx.compose.ui.graphics.Color
16+
import androidx.compose.ui.graphics.Path
17+
import androidx.compose.ui.graphics.PathEffect
18+
import androidx.compose.ui.graphics.StrokeCap
19+
import androidx.compose.ui.graphics.drawscope.DrawScope
20+
import androidx.compose.ui.graphics.drawscope.Stroke
21+
import androidx.compose.ui.graphics.nativeCanvas
22+
import androidx.compose.ui.tooling.preview.Preview
23+
import androidx.compose.ui.unit.dp
24+
import com.smarttoolfactory.tutorial1_1basics.ui.Green400
25+
import com.smarttoolfactory.tutorial1_1basics.ui.Orange400
26+
import com.smarttoolfactory.tutorial1_1basics.ui.Purple400
27+
28+
@Preview
29+
@Composable
30+
fun Tutorial6_35Screen() {
31+
TutorialContent()
32+
}
33+
34+
@Composable
35+
private fun TutorialContent() {
36+
MultiColoredLineChart()
37+
}
38+
39+
@Preview
40+
@Composable
41+
private fun MultiColoredLineChart() {
42+
43+
Column(
44+
modifier = Modifier
45+
.fillMaxSize()
46+
.padding(vertical = 8.dp)
47+
) {
48+
val path = remember {
49+
Path()
50+
}
51+
Canvas(
52+
modifier = Modifier
53+
.fillMaxWidth()
54+
.height(200.dp)
55+
// .graphicsLayer {
56+
// compositingStrategy = CompositingStrategy.Offscreen
57+
// }
58+
) {
59+
60+
val canvasHeight = size.height
61+
62+
if (path.isEmpty) {
63+
val points = getSinusoidalPoints(size, 30f)
64+
path.apply {
65+
moveTo(30f, center.y)
66+
points.forEach { offset: Offset ->
67+
lineTo(offset.x, offset.y)
68+
}
69+
}
70+
}
71+
72+
drawWithLayer {
73+
74+
// Destination
75+
drawPath(
76+
color = Green400,
77+
path = path,
78+
style = Stroke(
79+
width = 6.dp.toPx(),
80+
cap = StrokeCap.Round
81+
)
82+
)
83+
84+
// Source
85+
drawRect(
86+
color = Orange400,
87+
topLeft = Offset(0f, -10f),
88+
size = Size(size.width, size.height / 3f + 10f),
89+
blendMode = BlendMode.SrcIn
90+
)
91+
92+
// Source
93+
drawRect(
94+
color = Purple400,
95+
topLeft = Offset(0f, size.height / 3f),
96+
size = Size(size.width, size.height / 3f),
97+
blendMode = BlendMode.SrcIn
98+
)
99+
}
100+
101+
drawRect(
102+
color = Purple400.copy(alpha = .2f),
103+
topLeft = Offset(0f, size.height / 3f),
104+
size = Size(size.width, size.height / 3f),
105+
)
106+
107+
drawLine(
108+
strokeWidth = 2.dp.toPx(),
109+
start = Offset(0f, size.height / 3f),
110+
end = Offset(size.width, size.height / 3f),
111+
color = Color.Gray,
112+
pathEffect = PathEffect.dashPathEffect(floatArrayOf(10f, 10f))
113+
)
114+
115+
drawLine(
116+
strokeWidth = 2.dp.toPx(),
117+
start = Offset(0f, 2 * size.height / 3f),
118+
end = Offset(size.width, 2 * size.height / 3f),
119+
color = Color.Gray,
120+
pathEffect = PathEffect.dashPathEffect(floatArrayOf(10f, 10f))
121+
)
122+
}
123+
}
124+
}
125+
126+
private fun DrawScope.drawWithLayer(block: DrawScope.() -> Unit) {
127+
with(drawContext.canvas.nativeCanvas) {
128+
val checkPoint = saveLayer(null, null)
129+
block()
130+
restoreToCount(checkPoint)
131+
}
132+
}

0 commit comments

Comments
 (0)