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>
相关推荐
泡海椒1 小时前
PDF 表格样式优化:jquick-pdf 边框、圆角、背景色
java·开发语言·pdf
景熙55232 小时前
15.Java 8 Stream 流入门到实战
java·开发语言·数据结构
wno7043 小时前
Spring Security权限控制
java·python·spring
杨运交3 小时前
[071][验证码模块]基于Spring拦截器的验证码认证设计思想
java·后端·spring
Geek-Chow4 小时前
CountDownLatch in Java
java
SL_staff4 小时前
从RBAC到场景化授权:《无忧·企业文档》三级权限模型的技术实践解析
java·开源·产品
传奇开心果编程5 小时前
【springboot基础语法学与练】第 1 课:从零开始
java·spring boot·后端·学习
SL_staff5 小时前
财务系统慎用低代码?从数据模型闭环看合规落地的技术实践
java·低代码·全栈
滕州市燕猫虎计算机科技工作室个体工商户5 小时前
IDEA:Command line is too long
java·ide·intellij-idea