Android Drawable - gradient

Drawable 概述

  1. Drawable 可以通俗理解为可绘制的图形对象

  2. Drawable 是一个可被画在屏幕上的通用东西,可以是颜色、图片等

  3. 在 Android 中,Drawable 是一个抽象类 android.graphics.drawable.Drawable

  4. Drawable 的核心优势是轻量和灵活,能省 APK 体积


gradient

1、基本介绍
  1. android:type:指定渐变类型
说明
linear(线性渐变) 颜色沿着一条直线方向(由 angle 控制)均匀变化 默认类型,是最常见的渐变,适用于背景、按钮等
radial(径向渐变) 颜色从中心点向外呈圆形扩散 必须同时设置 gradientRadius 来指定扩散的半径
sweep(扫描渐变) 又称扫描式渐变,颜色围绕一个中心点旋转扫过 类似钟表盘或彩虹的扇形效果
  1. android:startColor / android:centerColor / android:endColor:定义颜色节点

    startColor 和 endColor 是必需的,分别定义渐变起点和终点的颜色。

    centerColor 是可选的,它可以在起点和终点颜色之间增加一个中间色,让过渡更丰富

  2. android:centerX / android:centerY:调整径向渐变 / 扫描渐变的原点的位置

    值范围是 0.0 到 1.0,默认是 0.5

  3. android:angle:控制线性渐变方向

    它定义渐变绘制的角度,必须是 45 的整数倍

    它的值是基于 0 度(从左到右)逆时针旋转的

说明
0 从左到右
90 从下到上
180 从右到左
270 从上到下
2、基本使用
  1. 线性渐变
xml 复制代码
<shape xmlns:android="http://schemas.android.com/apk/res/android"
    android:shape="rectangle">
    <gradient
        android:angle="270"
        android:endColor="#001081d2"
        android:startColor="#ff1081d2" />
</shape>
  1. 径向渐变
xml 复制代码
<shape xmlns:android="http://schemas.android.com/apk/res/android"
    android:shape="rectangle">
    <gradient
        android:endColor="#001081d2"
        android:gradientRadius="100dp"
        android:startColor="#ff1081d2"
        android:type="radial" />
</shape>
xml 复制代码
<shape xmlns:android="http://schemas.android.com/apk/res/android"
    android:shape="rectangle">
    <gradient
        android:centerX="0.1"
        android:centerY="0.1"
        android:endColor="#001081d2"
        android:gradientRadius="100dp"
        android:startColor="#ff1081d2"
        android:type="radial" />
</shape>
xml 复制代码
<!-- 椭圆形 -->

<shape xmlns:android="http://schemas.android.com/apk/res/android"
    android:shape="oval">
    <gradient
        android:endColor="#0000FF"
        android:gradientRadius="100dp"
        android:startColor="#FFFFFF"
        android:type="radial" />
</shape>
  1. 扫描渐变
xml 复制代码
<shape xmlns:android="http://schemas.android.com/apk/res/android"
    android:shape="rectangle">
    <gradient
        android:endColor="#001081d2"
        android:startColor="#ff1081d2"
        android:type="sweep" />
</shape>
xml 复制代码
<shape xmlns:android="http://schemas.android.com/apk/res/android"
    android:shape="rectangle">
    <gradient
        android:centerX="0.1"
        android:centerY="0.1"
        android:endColor="#001081d2"
        android:startColor="#ff1081d2"
        android:type="sweep" />
</shape>
xml 复制代码
<!-- 环形 -->

<shape xmlns:android="http://schemas.android.com/apk/res/android"
    android:shape="ring"
    android:useLevel="false">
    <gradient
        android:endColor="#0000FF"
        android:startColor="#FF0000"
        android:type="sweep" />
</shape>
相关推荐
恋猫de小郭3 小时前
Flutter 的另外一种形态?社区 DartNative 要来了。
android·前端·flutter
Kapaseker3 小时前
AI 时代,读源码还有用吗?从 Kotlin 集合排序说起
android·kotlin
淡海水4 小时前
15-07-YooAsset面试篇-Unity性能优化与内存管理
java·unity·面试·性能优化·c#·游戏引擎
snow@li4 小时前
Java:跨平台原理与JDK、JRE、JVM全景深度解析
java·开发语言·jvm
覆东流4 小时前
2.Java程序基础
java·开发语言·后端
snow@li4 小时前
SpringBoot:全套生命周期全景详解/应用级+Bean级
java·spring boot·rpc
love_muming4 小时前
二叉树操作全解析:从递归到层序遍历
java·数据结构·算法·二叉树
小的~~4 小时前
ThreadLocal 、InheritableThreadLocal 与 TransmittableThreadLocal 的进阶指南
java·开发语言
北极糊的狐4 小时前
钉钉小程序报错data.formatTime is not a function是因为 axml 模板中不能直接调用 Page 内自定义方法!
java·小程序·钉钉