Jorkoh
03/22/2021, 3:45 PMsindrenm
03/22/2021, 3:46 PMJorkoh
03/22/2021, 3:48 PMsindrenm
03/22/2021, 3:53 PMChachako
03/22/2021, 4:03 PMJorkoh
03/22/2021, 4:13 PMChachako
03/22/2021, 4:26 PMJorkoh
03/22/2021, 5:36 PM@Composable
fun Digit(digit: Int) {
val textDelegate = TextDelegate(
text = AnnotatedString(digit.toString()),
style = MaterialTheme.typography.h1.copy(
color = MaterialTheme.colors.onBackground,
textAlign = TextAlign.Center
),
density = LocalDensity.current,
resourceLoader = LocalFontLoader.current
).layout(Constraints(), LayoutDirection.Ltr)
Canvas(
modifier = Modifier
.fillMaxSize()
.alpha(0.99f)
) {
TextDelegate.paint(drawContext.canvas, textDelegate)
drawRect(
brush = Brush.verticalGradient(listOf(Color.Transparent, Color.White)),
blendMode = BlendMode.SrcIn
)
}
}
Sean McQuillan [G]
03/22/2021, 7:27 PMParagraph
to do the text layout.
@Composable
fun ParaFadingText(text: String, modifier: Modifier = Modifier) {
val density = LocalDensity.current
val style = MaterialTheme.typography.h2
val resourceLoader = LocalFontLoader.current
BoxWithConstraints(modifier) {
val paragraph = remember(text, density, style, resourceLoader) {
Paragraph(
text = text,
style = style,
density = density,
resourceLoader = resourceLoader,
width = constraints.maxWidth.toFloat()
)
}
with(density) {
Canvas(modifier.size(paragraph.width.toDp(), paragraph.height.toDp())) {
drawIntoCanvas { canvas ->
paragraph.paint(canvas)
drawRect( // or some other drawing commands here
Brush.verticalGradient(listOf(Color.Transparent, Color.White)),
blendMode = BlendMode.SrcAtop
)
}
}
}
}
}
Sean McQuillan [G]
03/22/2021, 7:28 PMSean McQuillan [G]
03/22/2021, 7:29 PMremember
your Paragraph
objects and TextLayoutResult
objects! They're quite expensive (they do full text layout, which involves quite a bit of work)Nader Jawad
03/22/2021, 7:39 PMJorkoh
03/22/2021, 8:03 PMTextDelegate
for now since this is just for the challenge and I'm a bit short on time but I have remembered the TextLayoutResult
for performance and I'll use Paragraph next time. And yes, being able to pass a Brush to the Text composable would have been ideal for this case.Archie
05/10/2021, 4:59 PMTextDelegate
to get the exact size that the canvas needs to be:
@InternalFoundationTextApi
@Composable
fun GradientText(
text: String,
brush: Brush,
modifier: Modifier = Modifier,
maxLines: Int = Int.MAX_VALUE,
softWrap: Boolean = true,
overflow: TextOverflow = TextOverflow.Clip,
style: TextStyle = LocalTextStyle.current,
) {
val density = LocalDensity.current
val resourceLoader = LocalFontLoader.current
BoxWithConstraints(
modifier = modifier,
) {
val textDelegate =
remember(text, style, maxLines, softWrap, overflow, density, resourceLoader) {
TextDelegate(
text = AnnotatedString(text),
style = style,
maxLines = maxLines,
softWrap = softWrap,
overflow = overflow,
density = density,
resourceLoader = resourceLoader,
).layout(constraints, LayoutDirection.Ltr)
}
with(density) {
Canvas(
modifier = Modifier.size(
width = textDelegate.size.width.toDp(),
height = textDelegate.size.height.toDp()
)
) {
TextDelegate.paint(drawContext.canvas, textDelegate)
drawRect(
brush = brush,
blendMode = BlendMode.SrcIn
)
}
}
}
}
Works great if the text isn’t drawn on top of a surface.
@InternalFoundationTextApi
@Preview
@Composable
fun GradientTextPreview() {
DfTheme {
// Surface {
GradientText(
text = "Streaming",
brush = horizontalGradient(
colors = listOf(
MaterialTheme.colors.secondaryVariant,
MaterialTheme.colors.secondary,
)
),
modifier = Modifier,
style = MaterialTheme.typography.h6,
)
// }
}
}
but once rendered on a surface, the brush just blends with the surface as well:
@InternalFoundationTextApi
@Preview
@Composable
fun GradientTextPreview() {
DfTheme {
Surface {
GradientText(
text = "Streaming",
brush = horizontalGradient(
colors = listOf(
MaterialTheme.colors.secondaryVariant,
MaterialTheme.colors.secondary,
)
),
modifier = Modifier,
style = MaterialTheme.typography.h6,
)
}
}
}
Do you have any advise to work around this? Thanks in advance.Sean McQuillan [G]
05/10/2021, 5:21 PMArchie
05/10/2021, 5:35 PMSean McQuillan [G]
05/10/2021, 6:01 PMArchie
05/11/2021, 3:43 PMArchie
05/11/2021, 3:44 PM