Open yschimke opened 1 month ago
import androidx.compose.foundation.background import androidx.compose.foundation.layout.Arrangement import androidx.compose.foundation.layout.Column import androidx.compose.foundation.layout.fillMaxSize import androidx.compose.foundation.layout.fillMaxWidth import androidx.compose.foundation.layout.height import androidx.compose.runtime.Composable import androidx.compose.ui.Modifier import androidx.compose.ui.draw.drawWithCache import androidx.compose.ui.geometry.Offset import androidx.compose.ui.graphics.BlendMode import androidx.compose.ui.graphics.Brush import androidx.compose.ui.graphics.Canvas import androidx.compose.ui.graphics.Color import androidx.compose.ui.graphics.CompositingStrategy import androidx.compose.ui.graphics.compositeOver import androidx.compose.ui.graphics.graphicsLayer import androidx.compose.ui.text.TextStyle import androidx.compose.ui.text.drawText import androidx.compose.ui.text.rememberTextMeasurer import androidx.compose.ui.text.style.TextAlign import androidx.compose.ui.unit.dp import androidx.compose.ui.unit.sp import androidx.wear.compose.material3.MaterialTheme import androidx.wear.compose.material3.Text import androidx.wear.compose.ui.tooling.preview.WearPreviewLargeRound @WearPreviewLargeRound @Composable fun GradientProbs() { Column( modifier = Modifier.fillMaxSize().background(Brush.radialGradient(radialColors())), verticalArrangement = Arrangement.Center, ) { SomeBigText() SomeBigText( modifier = Modifier.graphicsLayer { this.compositingStrategy = CompositingStrategy.Offscreen clip = true } ) SomeBigText2() } } @Composable private fun SomeBigText(modifier: Modifier = Modifier) { Text( text = "SOME BIG TEXT", modifier = modifier.fillMaxWidth().drawWithCache { onDrawWithContent { drawContent() drawRect( brush = Brush.horizontalGradient( listOf( Color.Transparent, Color.Black, Color.Black, Color.Transparent, ) ), blendMode = BlendMode.DstIn, ) } }, style = TextStyle.Default.copy(fontSize = 30.sp), textAlign = TextAlign.Center, ) } @Composable private fun SomeBigText2(modifier: Modifier = Modifier) { val textMeasurer = rememberTextMeasurer() androidx.compose.foundation.Canvas(modifier = modifier.fillMaxWidth().height(50.dp)) { val textLayout = textMeasurer.measure("SOME BIG TEXT", style = TextStyle.Default.copy(fontSize = 30.sp)) drawText( textLayout, brush = Brush.horizontalGradient( listOf(Color.Transparent, Color.White, Color.White, Color.Transparent) ), topLeft = Offset((size.width - textLayout.size.width) / 2f, 0f), ) } } @Composable fun radialColors() = listOf( (Color.Red).copy(alpha = 0.5f).compositeOver(MaterialTheme.colorScheme.background), (Color.Blue).copy(alpha = 0.1f).compositeOver(MaterialTheme.colorScheme.background), )