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>
相关推荐
文丑颜不良啊39 分钟前
LeetCode1461~1480题目解析
java·leetcode
2301_780789668 小时前
跨平台适配!SDK 游戏盾兼容全终端游戏场景
java·网络·ddos
学长毕设9 小时前
基于springboot的食品仓库管理系统
java·spring boot·后端
步行cgn10 小时前
FormHttpMessageConverter 详解:Spring MVC 中表单数据的读写转换器
java·spring·mvc
龙亘川10 小时前
构建城市公共安全数字底座:亘川智城智慧安防系统全维度解读
java·大数据·开发语言·人工智能·信息可视化
金銀銅鐵10 小时前
[Java] 用 CLI 展示 EnumMap 增删键值对时的内部变化
java·源码·ai编程
秋天的怀念_David10 小时前
第二十二夜 · 数字迷雾
java·程序人生·算法
Raas10010 小时前
AI网关部署方案?从部署到上线全流程
java·开发语言·人工智能·gateway·ai网关·企业级ai网关·企业级ai产品
niucloud-admin11 小时前
NIUCLOUD PHP SAAS上门家政使用文档:城市管理
java·前端·php
霸道流氓气质11 小时前
智能客服组件:分层意图识别、RAG Advisor 与人工转接状态机的 Vue3 + Spring AI 生产级实战
java·人工智能·spring