@@ -29,7 +29,9 @@ import androidx.ui.geometry.Offset
29
29
import androidx.ui.graphics.Color
30
30
import androidx.ui.graphics.Paint
31
31
import androidx.ui.graphics.Shadow
32
- import androidx.ui.graphics.withSave
32
+ import androidx.ui.graphics.painter.Stroke
33
+ import androidx.ui.graphics.painter.drawCanvas
34
+ import androidx.ui.graphics.painter.rotate
33
35
import androidx.ui.graphics.withSaveLayer
34
36
import androidx.ui.layout.fillMaxSize
35
37
import androidx.ui.text.TextStyle
@@ -49,17 +51,19 @@ import com.squareup.workflow.ui.compose.bindCompose
49
51
internal fun placeholderViewFactory (modifier : Modifier ): ViewFactory <Any > =
50
52
bindCompose { rendering, _ ->
51
53
Text (
52
- modifier = modifier/* .fillMaxSize() */
54
+ modifier = modifier
53
55
.clipToBounds()
54
56
.drawBehind {
55
- withSaveLayer(size.toRect(), Paint ().apply { alpha = .2f }) {
56
- drawRect(size.toRect(), Paint ().apply { color = Color .Gray })
57
- drawCrossHatch(
58
- color = Color .Red ,
59
- strokeWidth = 2 .dp,
60
- spaceWidth = 5 .dp,
61
- angle = 45f
62
- )
57
+ drawCanvas { canvas, size ->
58
+ canvas.withSaveLayer(size.toRect(), Paint ().apply { alpha = .2f }) {
59
+ canvas.drawRect(size.toRect(), Paint ().apply { color = Color .Gray })
60
+ drawCrossHatch(
61
+ color = Color .Red ,
62
+ strokeWidth = 2 .dp,
63
+ spaceWidth = 5 .dp,
64
+ angle = 45f
65
+ )
66
+ }
63
67
}
64
68
},
65
69
text = rendering.toString(),
@@ -74,25 +78,25 @@ internal fun placeholderViewFactory(modifier: Modifier): ViewFactory<Any> =
74
78
@Preview(widthDp = 200 , heightDp = 200 )
75
79
@Composable private fun PreviewStubViewBindingOnWhite () {
76
80
Box (backgroundColor = Color .White ) {
77
- placeholderViewFactory(Modifier ).preview(
78
- rendering = " preview" ,
79
- modifier = Modifier .fillMaxSize()
80
- .drawBorder(size = 1 .dp, color = Color .Red )
81
- )
81
+ PreviewStubBindingPreviewTemplate ()
82
82
}
83
83
}
84
84
85
85
@Preview(widthDp = 200 , heightDp = 200 )
86
86
@Composable private fun PreviewStubViewBindingOnBlack () {
87
87
Box (backgroundColor = Color .Black ) {
88
- placeholderViewFactory(Modifier ).preview(
89
- rendering = " preview" ,
90
- modifier = Modifier .fillMaxSize()
91
- .drawBorder(size = 1 .dp, color = Color .Red )
92
- )
88
+ PreviewStubBindingPreviewTemplate ()
93
89
}
94
90
}
95
91
92
+ @Composable private fun PreviewStubBindingPreviewTemplate () {
93
+ placeholderViewFactory(Modifier ).preview(
94
+ rendering = " preview" ,
95
+ placeholderModifier = Modifier .fillMaxSize()
96
+ .drawBorder(size = 1 .dp, color = Color .Red )
97
+ )
98
+ }
99
+
96
100
private fun DrawScope.drawCrossHatch (
97
101
color : Color ,
98
102
strokeWidth : Dp ,
@@ -109,34 +113,27 @@ private fun DrawScope.drawHatch(
109
113
spaceWidth : Dp ,
110
114
angle : Float
111
115
) {
112
- val strokeWidthPx = strokeWidth.toPx()
113
- .value
114
- val paint = Paint ().also {
115
- it.color = color.scaleColors(.5f )
116
- it.strokeWidth = strokeWidthPx
117
- }
118
-
119
- withSave {
120
- val halfWidth = size.width.value / 2
121
- val halfHeight = size.height.value / 2
122
- translate(halfWidth, halfHeight)
123
- rotate(angle)
124
- translate(- halfWidth, - halfHeight)
116
+ val strokeWidthPx = strokeWidth.toPx().value
117
+ val spaceWidthPx = spaceWidth.toPx().value
118
+ val strokeColor = color.scaleColors(.5f )
119
+ val stroke = Stroke (width = strokeWidthPx)
125
120
121
+ rotate(angle) {
126
122
// Draw outside our bounds to fill the space even when rotated.
127
- val left = - size.width.value
128
- val right = size.width.value * 2
129
- val top = - size.height.value
130
- val bottom = size.height.value * 2
123
+ val left = - size.width
124
+ val right = size.width * 2
125
+ val top = - size.height
126
+ val bottom = size.height * 2
131
127
132
128
var y = top + strokeWidthPx * 2f
133
129
while (y < bottom) {
134
130
drawLine(
131
+ strokeColor,
135
132
Offset (left, y),
136
133
Offset (right, y),
137
- paint
134
+ stroke = stroke
138
135
)
139
- y + = spaceWidth.toPx().value * 2
136
+ y + = spaceWidthPx * 2
140
137
}
141
138
}
142
139
}
0 commit comments