Android

关注公众号 jb51net

关闭
首页 > 软件编程 > Android > Android gradient 使用

Android gradient 使用小结

作者:stevenzqzq

在Android中使用gradient(渐变)通常是通过drawable文件来设置背景,下面是可以直接用的几种用法汇总,包括线性渐变、径向渐变、扫描渐变(sweep)等,感兴趣的朋友一起看看吧

在 Android 中使用 gradient(渐变) 通常是通过 drawable 文件来设置背景。下面是可以直接用的几种用法汇总,包括线性渐变、径向渐变、扫描渐变(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" />

🔹 angle 取值范围: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" />

🔹 centerX/Y: 百分比(0.5 表示中心)
🔹 gradientRadius: 渐变半径,单位为 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 的 gradient 中使用 android:angle 属性时,它控制渐变的方向。它的单位是角度,**以“从左到右顺时针旋转”**为标准。

android:angle 方向图解(基于 type="linear"

angle渐变方向说明(startColorendColor
0从左 ➜ 右左边是 startColor,右边是 endColor
45从左下 ➜ 右上斜向上渐变
90从下 ➜ 上下边是 startColor,上边是 endColor
135从右下 ➜ 左上斜向上渐变
180从右 ➜ 左右边是 startColor,左边是 endColor
225从右上 ➜ 左下斜向下渐变
270从上 ➜ 下上边是 startColor,下边是 endColor
315从左上 ➜ 右下斜向下渐变

✅ 举例说明:

<gradient
    android:startColor="#FF0000"
    android:endColor="#0000FF"
    android:angle="90"/>

上面是 从下往上 渐变(即底部是红色,顶部是蓝色),不是从左到右!这也是 Android 和 CSS 的一个差异点,容易混淆。

❗注意:

0:从左到右
45:从左下到右上
90:从下到上
135:从右下到左上
180:从右到左
225:从右上到左下
270:从上到下
315:从左上到右下

✅图示参考:

          ↑
    270°  ↑  90°
← 180° ←     → 0° →
         ↓

🌟 注意:这个角度是 Android 中 定义渐变方向用的逻辑值不是数学角度的坐标方向

✅ 示例一:从左到右渐变

<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"/>

🔸 颜色从上(绿) → 下(黑) 渐变。

⚠️ 注意事项:

radialsweep的区别

🔵 radial(放射状渐变)

✅ 特点:

✅ 用法示例:

<gradient
    android:type="radial"
    android:startColor="#FF0000"
    android:endColor="#0000FF"
    android:centerX="0.5"
    android:centerY="0.5"
    android:gradientRadius="200"/>

✅ 参数说明:

✅ 视觉示意:

  渐变像个圆圈扩散出去:
     R G B
     ↓↓↓
   ●●●●●●●●
   ●●◎◎◎●●
   ●●◎◎◎●●
   ●●◎◎◎●●
   ●●●●●●●●

🟣 sweep(扫描/扫描状渐变)

✅ 特点:

✅ 用法示例:

<gradient
    android:type="sweep"
    android:startColor="#FF0000"
    android:endColor="#0000FF"
    android:centerX="0.5"
    android:centerY="0.5"/>

✅ 参数说明:

✅ 视觉示意:

   色彩从中间绕一圈:
      红 → 橙 → 黄
      ↑       ↓
     紫 ← 蓝 ← 绿

🔄 总结对比表:

类型视觉效果可设置角度中心点常用场景
linear直线方向渐变✅ 支持按钮、背景、边框线
radial中心向外扩散❌ 不支持光晕、聚光灯、圆形按钮
sweep中心旋转渐变❌ 不支持表盘、雷达、加载动画

到此这篇关于Android gradient 使用的文章就介绍到这了,更多相关Android gradient 使用内容请搜索脚本之家以前的文章或继续浏览下面的相关文章希望大家以后多多支持脚本之家!

您可能感兴趣的文章:
阅读全文