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)
)
相关推荐
A0微声z1 天前
Kotlin Multiplatform (KMP) 中使用 Protobuf
kotlin
雮尘1 天前
手把手带你玩转Android gRPC:一篇搞定原理、配置与客户端开发
android·前端·grpc
ktl1 天前
Android 编译加速/优化 80%:一个文件搞定,零侵入零配置
android
alexhilton2 天前
使用FunctionGemma进行设备端函数调用
android·kotlin·android jetpack
冬奇Lab2 天前
InputManagerService:输入事件分发与ANR机制
android·源码阅读
张小潇2 天前
AOSP15 Input专题InputManager源码分析
android·操作系统
lhDream2 天前
Kotlin 开发者必看!JetBrains 开源 LLM 框架 Koog 快速上手指南(含示例)
kotlin
RdoZam2 天前
Android-封装基类Activity\Fragment,从0到1记录
android·kotlin
奥陌陌2 天前
android 打印函数调用堆栈
android
用户985120035832 天前
Compose Navigation 3 深度解析(二):基础用法
android·android jetpack