高级UI<第三十二篇>:Android中Shape的属性详解
2020-01-19 本文已影响0人
NoBugException
所谓Shape就是使用xml绘制简单形状的方法,常用于控件的背景。
了解shape的使用是学好高级UI的必要组成部分,下面贴出简单写法,如下:
<?xml version="1.0" encoding="utf-8"?>
<!--形状-->
<shape
xmlns:android="http://schemas.android.com/apk/res/android">
<!--圆角 -->
<corners />
<!-- 渐变 -->
<gradient />
<!-- 内边距 -->
<padding />
<!-- 大小 -->
<size />
<!-- 填充 -->
<solid />
<!-- 描边 -->
<stroke />
</shape>
这个xml是特别简单的,那么,下面整理几张表格,让shape、corners 、gradient 、padding 、size 、solid 、stroke 的属性变得一步了然吧。
(1)shape属性
shape属性 | 具体属性 | 描述 |
---|---|---|
dither | android:dither="false或true" | 将在位图的像素配置与屏幕不同时(例如:ARGB 8888 位图和 RGB 565 屏幕)启用位图的抖动;值为“false”时则停用抖动。默认值为 true。 |
shape | android:shape="rectangle或line或oval或ring" | 分别为矩形、线、椭圆、环。默认为矩形rectangle |
innerRadius | android:innerRadius="integer" | shape为ring时可用,内环半径 |
innerRadiusRatio | android:innerRadiusRatio="float" | shape为ring时可用,内环的厚度比,即环的宽度比表示内环半径,默认为3,可被innerRadius值覆盖 |
thickness | android:thickness="integer" | shape为ring时可用,环的厚度 |
thicknessRatio | android:thicknessRatio="float" | shape为ring时可用,环的厚度比,即环的宽度比表示环的厚度,默认为9,可被thickness值覆盖 |
tint | android:tint="color" | 给shape着色 |
tintMode | android:tintMode="src_in或src_atop或src_over或add或multiply或screen" | 着色类型 |
useLevel | android:useLevel="false或true" | 较少用,一般设为false,否则图形不显示。为true时可在LevelListDrawable使用 |
visible | android:visible="false或true" |
(2)corners属性
corners(圆角)属性 | 具体属性 | 描述 |
---|---|---|
radius | android:radius="integer" | 圆角半径,该值设置时下面四个属性失效 |
bottomLeftRadius | android:bottomLeftRadius="integer" | 左下角圆角半径 |
bottomRightRadius | android:bottomRightRadius="integer" | 右下角圆角半径 |
topLeftRadius | android:topLeftRadius | 左上角圆角半径 |
topRightRadius | android:topRightRadius="integer" | 右上角圆角半径 |
(3)gradient属性
gradient(渐变)属性 | 具体属性 | 描述 |
---|---|---|
useLevel | android:useLevel="false或true" | 与上面shape中该属性的一致 |
type | android:type="linear或radial或sweep" | 渐变类型,分别为线性、放射性、扫描性渐变,默认为线性渐变linear |
angle | android:angle="integer" | 渐变角度,当上面type为线性渐变linear时有效。角度为45的倍数,0度时从左往右渐变,角度方向逆时针 |
centerColor | android:centerColor="color" | 渐变中间位置颜色 |
startColor | android:startColor="color" | 渐变开始位置颜色 |
endColor | android:endColor="color" | 渐变结束位置颜色 |
centerX | android:centerX="float" | type为放射性渐变radial时有效,设置渐变中心的X坐标,取值区间[0,1],默认为0.5,即中心位置 |
centerY | android:centerY="float" | type为放射性渐变radial时有效,设置渐变中心的Y坐标,取值区间[0,1],默认为0.5,即中心位置 |
gradientRadius | android:gradientRadius="integer" | type为放射性渐变radial时有效,渐变的半径 |
(4)padding属性
padding(内边距 )属性 | 具体属性 | 描述 |
---|---|---|
bottom | ndroid:bottom="integer" | 设置底部边距 |
left | android:left="integer" | 左边边距 |
right | android:right="integer" | 右边 |
top | android:top="integer" | 顶部 |
(5)size属性
size(大小)属性 | 具体属性 | 描述 |
---|---|---|
height | android:height="integer" | 宽度 |
width | android:width="integer" | 高度 |
(6)solid属性
solid(填充)属性 | 具体属性 | 描述 |
---|---|---|
color | android:color="color" | shape的填充色 |
(7)stroke属性
stroke(描边)属性 | 具体属性 | 描述 |
---|---|---|
color | android:color="color" | 描边的颜色 |
width | android:width="integer" | 描边的宽度 |
dashGap | android:dashGap="integer" | 虚线间隔 |
dashWidth | android:dashWidth="integer" | 虚线宽度 |
以上属性就是全部了,有兴趣的猿可以试试。
[本章完...]