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

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

本文详细介绍了如何在 Jetpack Compose 中实现文本渐变效果的方法,利用 Brush API提供的更符合 Compose 习惯用法的新方法,在 TextStyleSpanStyle 中使用 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)
)
相关推荐
my_power52017 小时前
车载安卓面试题汇总
android
csj5017 小时前
安卓基础之《(15)—内容提供者(1)在应用之间共享数据》
android
yeziyfx18 小时前
kotlin中 ?:的用法
android·开发语言·kotlin
2501_9159184119 小时前
只有 Flutter IPA 文件,通过多工具组合完成有效混淆与保护
android·flutter·ios·小程序·uni-app·iphone·webview
robotx19 小时前
AOSP 设置-提示音和振动 添加一个带有开关(Switch)的设置项
android
青莲84319 小时前
RecyclerView 完全指南
android·前端·面试
青莲84319 小时前
Android WebView 混合开发完整指南
android·前端·面试
龙之叶20 小时前
【Android Monkey源码解析三】- 运行解析
android
KevinWang_21 小时前
Android 的 assets 资源和 raw 资源有什么区别?
android
码农幻想梦1 天前
2021Android从零入门到实战(慕课网官方账号)
android