Jetpack Compose Brush API 简单使用实战 —— 新手指南

Jetpack Compose Brush API 详解 ------ 新手指南

本文详细介绍了如何在 Jetpack Compose 中实现文本渐变效果的方法,利用 Brush API提供的更符合 Compose 习惯用法的新方法,在 TextStyle 和 SpanStyle 中使用 Brush等,这使得为文本添加复杂的颜色变化变得前所未有的简单。

1. Brush 使用 drawWithCache 修饰符 "emoji" 表情也会被绘制

kotlin 复制代码
Text(
    text = "Jetpack Compose ❤",
    fontSize = 18.sp,
    modifier = Modifier.padding(start = 16.dp, top = 24.dp, end = 16.dp)
        .graphicsLayer(alpha = 0.99f).drawWithCache {
                val brush = Brush.horizontalGradient(rainbowColors)
                onDrawWithContent {
                    drawContent()
                    drawRect(brush, blendMode = BlendMode.SrcAtop)
            }
        }
)

2. Brush 简单使用

kotlin 复制代码
Text(
    modifier = Modifier.padding(start = 16.dp, top = 24.dp, end = 16.dp),
    text = "Jetpack Compose ❤",
    fontSize = 18.sp,
    style = TextStyle(
        brush = Brush.linearGradient(
            colors = rainbowColors
        )
    )
)

3. Brush 简单使用2(横向、竖向、径向渐变、扇形)

kotlin 复制代码
// 横向笔刷
Text(
    modifier = Modifier.padding(start = 16.dp, top = 24.dp, end = 16.dp),
    text = "Jetpack Compose" +
            "\nWhere Design Meets Dev in Real-Time" +
            "\n Smart IDE," +
            "\n bringing AI into" +
            "\n your workflow",
    fontSize = 18.sp,
    style = TextStyle(
        brush = Brush.horizontalGradient(
            colors = rainbowColors2
        )
    )
)

// 竖向笔刷
Text(
    modifier = Modifier.padding(start = 16.dp, top = 24.dp, end = 16.dp),
    text = "Jetpack Compose" +
            "\nWhere Design Meets Dev in Real-Time" +
            "\n Smart IDE," +
            "\n bringing AI into" +
            "\n your workflow",
    fontSize = 18.sp,
    style = TextStyle(
        brush = Brush.verticalGradient(
            colors = rainbowColors2
        )
    )
)

// 放射笔刷
Text(
    modifier = Modifier.padding(start = 16.dp, top = 24.dp, end = 16.dp),
    text = "Jetpack Compose" +
            "\nWhere Design Meets Dev in Real-Time" +
            "\n Smart IDE," +
            "\n bringing AI into" +
            "\n your workflow",
    fontSize = 18.sp,
    style = TextStyle(
        brush = Brush.radialGradient(
            colors = rainbowColors2
        )
    )
)

// 扫掠笔刷
Text(
    modifier = Modifier.padding(start = 16.dp, top = 24.dp, end = 16.dp),
    text = "Jetpack Compose" +
            "\nWhere Design Meets Dev in Real-Time" +
            "\n Smart IDE," +
            "\n bringing AI into" +
            "\n your workflow",
    fontSize = 18.sp,
    style = TextStyle(
        brush = Brush.sweepGradient(
            colors = rainbowColors2
        )
    )
)

4. Brush 单色

kotlin 复制代码
// 单色笔刷
Text(
    modifier = Modifier.padding(start = 16.dp, top = 24.dp, end = 16.dp),
    text = "Jetpack Compose" +
            "\nWhere Design Meets Dev in Real-Time" +
            "\n Smart IDE," +
            "\n bringing AI into" +
            "\n your workflow",
    fontSize = 18.sp,
    style = TextStyle(
        brush = SolidColor(Color.Green)
    )
)

5. Brush自定义

kotlin 复制代码
Text(
    modifier = Modifier.padding(start = 16.dp, top = 24.dp, end = 16.dp),
    text = "Jetpack Compose" +
            "\nWhere Design Meets Dev in Real-Time" +
            "\n Smart IDE," +
            "\n bringing AI into" +
            "\n your workflow",
    fontSize = 18.sp,
    style = TextStyle(
        brush = ScaledRepetitionBrush(Brush.linearGradient(
            colors = rainbowColors,
            tileMode = TileMode.Repeated
        ) as ShaderBrush)
    )
)


//自定义笔刷
class ScaledRepetitionBrush(val shaderBrush: ShaderBrush): ShaderBrush() {
    override fun createShader(size: Size): Shader {
        return shaderBrush.createShader(size / 4f)
    }
}

色值集

kotlin 复制代码
val rainbowColors = listOf(
    Color(0xFFFF0000),// Red
    Color(0xFFFF7F00),// Orange
    Color(0xFFFFFF00),// Yellow
    Color(0xFF00FF00),// Green
    Color(0xFF00FFFF),// Cyan
    Color(0xFF0000FF),// Blue
    Color(0xFF8B00FF)// Purple (or Violet)
)
相关推荐
千里马学框架5 天前
一起学 Android 14:ShellTransition 屏幕旋转过程深度剖析
android·智能手机·性能优化·framework·性能·屏幕旋转·rotation
美狐美颜SDK开放平台5 天前
开发直播APP时如何接入视频美颜SDK?开发流程与注意事项
android·人工智能·计算机视觉·音视频·直播美颜sdk
AFinalStone5 天前
Android7 SystemUI源码解析(七)Keyguard锁屏模块深度解析
android·systemui
致远ccc5 天前
Google Play 上架前如何测试 App?多国家 Android 环境测试
android·app测试·googleplay·多国家应用测试
ttyyttemo5 天前
Kotlin 协程中的 Job 结构化并发与取消
android
sun0077005 天前
tbox 4g/5g切换,导致wan ip 改变,导致车机旧网络不可用。需要重启车机才行
android
ai2work6 天前
ch23 综合复刻:从零做一个最小可用版本(capstone)
kotlin
其实防守也摸鱼6 天前
内网穿透与反向代理:原理、工具与实战指南
android·大数据·运维·安全·网络安全·自动化·渗透
ai2work6 天前
ch21 签名、校验与发版
kotlin
AFinalStone6 天前
Android7 SystemUI 源码解析(四)NavigationBar 导航栏与 SystemBars
android·systemui