Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Test optimisation of MarqueeText #2427

Open
yschimke opened this issue Oct 16, 2024 · 0 comments
Open

Test optimisation of MarqueeText #2427

yschimke opened this issue Oct 16, 2024 · 0 comments

Comments

@yschimke
Copy link
Collaborator

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),
    )

Image

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant