在 Android 中使用 gradient(渐变) 通常是通过 文件来设置背景。下面是可以直接用的几种用法汇总,包括线性渐变、径向渐变、扫描渐变(sweep)等:
✅ 1. Linear Gradient(线性渐变)
- <!-- res/drawable/bg_gradient_linear.xml -->
- <gradient xmlns:android="http://schemas.android.com/apk/res/android"
- android:type="linear"
- android:startColor="#FF512F"
- android:endColor="#DD2476"
- android:angle="45" />
复制代码🔹 取值范围:0~360,表示渐变方向(0 为从上往下,90 为从左往右)。
✅ 2. Radial Gradient(径向渐变)
- <!-- res/drawable/bg_gradient_radial.xml -->
- <gradient xmlns:android="http://schemas.android.com/apk/res/android"
- android:type="radial"
- android:centerX="0.5"
- android:centerY="0.5"
- android:gradientRadius="200"
- android:startColor="#FF512F"
- android:endColor="#DD2476" />
复制代码🔹 : 百分比(0.5 表示中心)
🔹 : 渐变半径,单位为 px
✅ 3. Sweep Gradient(扫描渐变)
- <!-- res/drawable/bg_gradient_sweep.xml -->
- <gradient xmlns:android="http://schemas.android.com/apk/res/android"
- android:type="sweep"
- android:centerX="0.5"
- android:centerY="0.5"
- android:startColor="#FF512F"
- android:endColor="#DD2476" />
复制代码 ✅ 4. 多色渐变
- <gradient xmlns:android="http://schemas.android.com/apk/res/android"
- android:type="linear"
- android:startColor="#FF512F"
- android:centerColor="#F09819"
- android:endColor="#DD2476"
- android:angle="90" />
复制代码 ✅ 5. 设置背景到 View
- android:background="@drawable/bg_gradient_linear"
复制代码 ✅ 6. 代码中创建 GradientDrawable
- val gradient = GradientDrawable(
- GradientDrawable.Orientation.LEFT_RIGHT,
- intArrayOf(Color.RED, Color.BLUE)
- )
- gradient.cornerRadius = 20f
- yourView.background = gradient
复制代码 ✅ 7. 圆角 + 渐变(常用)
- <shape xmlns:android="http://schemas.android.com/apk/res/android"
- android:shape="rectangle">
- <corners android:radius="16dp"/>
- <gradient
- android:type="linear"
- android:startColor="#FF512F"
- android:endColor="#DD2476"
- android:angle="90"/>
- </shape>
复制代码 android:angle 方向图解
在 Android 的 中使用 属性时,它 控制渐变的方向。它的单位是角度,**以“从左到右顺时针旋转”**为标准。
✅ 方向图解(基于 )
angle渐变方向说明( ➜ ) 0从左 ➜ 右左边是 ,右边是 45从左下 ➜ 右上斜向上渐变 90从下 ➜ 上下边是 ,上边是 135从右下 ➜ 左上斜向上渐变 180从右 ➜ 左右边是 ,左边是 225从右上 ➜ 左下斜向下渐变 270从上 ➜ 下上边是 ,下边是 315从左上 ➜ 右下斜向下渐变
✅ 举例说明:
- <gradient
- android:startColor="#FF0000"
- android:endColor="#0000FF"
- android:angle="90"/>
复制代码上面是 从下往上 渐变(即底部是红色,顶部是蓝色),不是从左到右!这也是 Android 和 CSS 的一个差异点,容易混淆。
❗注意:
- 必须是 45 的整数倍,否则会被忽略或默认处理。
- 的值是 顺时针旋转角度,从 0 度(从左 ➜ 右)开始。
0:从左到右
45:从左下到右上
90:从下到上
135:从右下到左上
180:从右到左
225:从右上到左下
270:从上到下
315:从左上到右下
✅图示参考: - ↑
- 270° ↑ 90°
- ← 180° ← → 0° →
- ↓
复制代码- 🌟 注意:这个角度是 Android 中 <strong>定义渐变方向用的逻辑值</strong>,<strong>不是数学角度的坐标方向</strong>。
复制代码 ✅ 示例一:从左到右渐变
- <gradient
- android:type="linear"
- android:startColor="#FF0000"
- android:endColor="#0000FF"
- android:angle="90"/>
复制代码🔸 颜色从左(红) → 右(蓝) 渐变。
✅ 示例二:从上到下渐变
- <gradient
- android:type="linear"
- android:startColor="#00FF00"
- android:endColor="#000000"
- android:angle="0"/>
复制代码🔸 颜色从上(绿) → 下(黑) 渐变。
⚠️ 注意事项:
- 只能是 45 的倍数(如 0、45、90、135…),否则 Android 会忽略。
- 默认是,也就是 从上到下。
- 时,才生效。
- 对于和类型,不起作用。
三 和 的区别
🔵 (放射状渐变)
✅ 特点:
- 从中心向外发散。
- 渐变是圆形扩散的效果。
- 类似水波或聚光灯、光晕。
✅ 用法示例: - <gradient
- android:type="radial"
- android:startColor="#FF0000"
- android:endColor="#0000FF"
- android:centerX="0.5"
- android:centerY="0.5"
- android:gradientRadius="200"/>
复制代码✅ 参数说明:
- /:中心点位置(0~1,表示百分比)。
- :渐变的半径(必须设置)。
- 无效!
✅ 视觉示意: - 渐变像个圆圈扩散出去:
- R G B
- ↓↓↓
- ●●●●●●●●
- ●●◎◎◎●●
- ●●◎◎◎●●
- ●●◎◎◎●●
- ●●●●●●●●
复制代码🟣 (扫描/扫描状渐变)
✅ 特点:
- 从中心点绕一圈旋转(360°)改变颜色。
- 类似时钟的指针旋转、雷达扫描。
✅ 用法示例: - <gradient
- android:type="sweep"
- android:startColor="#FF0000"
- android:endColor="#0000FF"
- android:centerX="0.5"
- android:centerY="0.5"/>
复制代码✅ 参数说明:
- /:设置渐变中心。
- 不支持,方向是固定的:从 0° 顺时针到 360°。
✅ 视觉示意: - 色彩从中间绕一圈:
- 红 → 橙 → 黄
- ↑ ↓
- 紫 ← 蓝 ← 绿
复制代码🔄 总结对比表:
类型视觉效果可设置角度中心点常用场景 直线方向渐变✅ 支持❌按钮、背景、边框线 中心向外扩散❌ 不支持✅光晕、聚光灯、圆形按钮 中心旋转渐变❌ 不支持✅表盘、雷达、加载动画到此这篇关于Android gradient 使用的文章就介绍到这了,更多相关Android gradient 使用内容请搜索晓枫资讯以前的文章或继续浏览下面的相关文章希望大家以后多多支持晓枫资讯!
免责声明:如果侵犯了您的权益,请联系站长,我们会及时删除侵权内容,谢谢合作! |