Drawable 概述
-
Drawable 可以通俗理解为可绘制的图形对象
-
Drawable 是一个可被画在屏幕上的通用东西,可以是颜色、图片等
-
在 Android 中,Drawable 是一个抽象类
android.graphics.drawable.Drawable -
Drawable 的核心优势是轻量和灵活,能省 APK 体积
gradient
1、基本介绍
android:type:指定渐变类型
| 值 | 说明 |
|---|---|
| linear(线性渐变) | 颜色沿着一条直线方向(由 angle 控制)均匀变化 默认类型,是最常见的渐变,适用于背景、按钮等 |
| radial(径向渐变) | 颜色从中心点向外呈圆形扩散 必须同时设置 gradientRadius 来指定扩散的半径 |
| sweep(扫描渐变) | 又称扫描式渐变,颜色围绕一个中心点旋转扫过 类似钟表盘或彩虹的扇形效果 |
-
android:startColor/android:centerColor/android:endColor:定义颜色节点startColor 和 endColor 是必需的,分别定义渐变起点和终点的颜色。
centerColor 是可选的,它可以在起点和终点颜色之间增加一个中间色,让过渡更丰富
-
android:centerX/android:centerY:调整径向渐变 / 扫描渐变的原点的位置值范围是 0.0 到 1.0,默认是 0.5
-
android:angle:控制线性渐变方向它定义渐变绘制的角度,必须是 45 的整数倍
它的值是基于 0 度(从左到右)逆时针旋转的
| 值 | 说明 |
|---|---|
| 0 | 从左到右 |
| 90 | 从下到上 |
| 180 | 从右到左 |
| 270 | 从上到下 |
2、基本使用
- 线性渐变
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>
- 径向渐变
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>
- 扫描渐变
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>