此篇文章是以官方文档结合本人经验 编写出来的 ,欢迎大家纠错。 附上官方文档路径 docs/guide/topics/resources/drawable-resource.html
Shape Drawable
在XML中定义的形状
文件位于:
res/drawable/
filename
.xml
这个文件可以作用于资源ID
使用对象方式
对象为:android.graphics.drawable.GradientDrawable
引用方式:
In Java:
R.drawable.
filename
In XML:
@[
package
:]drawable/
filename
语法:
<?xml version="1.0" encoding="utf-8"?>
<shape
xmlns:android="http://schemas.android.com/apk/res/android"
android:shape=["rectangle" | "oval" | "line" | "ring"] >
<corners
android:radius="integer"
android:topLeftRadius="integer"
android:topRightRadius="integer"
android:bottomLeftRadius="integer"
android:bottomRightRadius="integer" />
<gradient
android:angle="integer"
android:centerX="integer"
android:centerY="integer"
android:centerColor="integer"
android:endColor="color"
android:gradientRadius="integer"
android:startColor="color"
android:type=["linear" | "radial" | "sweep"]
android:useLevel=["true" | "false"] />
<padding
android:left="integer"
android:top="integer"
android:right="integer"
android:bottom="integer" />
<size
android:width="integer"
android:height="integer" />
<solid
android:color="color" />
<stroke
android:width="integer"
android:color="color"
android:dashWidth="integer"
android:dashGap="integer" />
</shape>
参数说明:
<shape>
根元素
属性:
xmlns:android
这个是安卓的命名空间,必须是
"http://schemas.android.com/apk/res/android"
.
android:shape
关键的: 定义形状类型,下表为类型简介
值说明“rectangle”默认就是矩形,填充包裹视图"oval"一个适合的椭圆形包含视图的尺寸。"line"一条水平线横跨包含视图的宽度。这个形状要求< stroke>元素定义线的宽度"ring"一枚戒指的形状。(环)
只有使用这个属性
android:shape="ring"
:
android:innerRadius
尺寸。半径的内环(以中心),作为一个尺寸
android:innerRadiusRatio
表示一个内环的宽度比例例如:如果
android:innerRadiusRatio="5"
那么内半径等于环的宽度除以5
这个值会覆盖
android:innerRadius 默认值是
9.
android:thickness
环的厚度
android:thicknessRatio
表示一个环的厚度比例 ,比如
.
android:thicknessRatio="2"
, 那么
厚度等于环的宽度除以2
. 这个值会覆盖
android:innerRadius
.默认为 3.
android:useLevel
Boolean
. "true" if this is used as a
android.graphics.drawable.LevelListDrawable
这通常应该是“false”或你的形状可能不会出现。
<corners>
创建一个圆角
属性为:
android:radius
各个角的半径
android:topLeftRadius
左上角的半径
android:topRightRadius
右上角的半径
android:bottomLeftRadius
左下角的半径
android:bottomRightRadius
右下角的半径
注意:当值为0dp的时候 角是直角,否则将会以边到里面距离为中心点设置圆角,比如一个正方形,他将以边往里 (设置的距离)作为 中心点 原点 设置的半径为半径 画圆角。并裁剪多余的角
<gradient>
指定一个渐变颜色的形状。
属性:
android:angle
度数值 0 从左往右,90从下往上,他必须是45的倍数默认0
android:startColor
开始渐变的颜色
android:centerColor
中间开始渐变的颜色
android:centerX
中心点位置 相对X方向哪个位置开始渐变
(0 - 1.0). 当
android:type="radial"
.时这个指定的是中心点位置,并从这个位置开始渐变。
android:centerY
中心点位置 相对Y方向哪个位置开始渐变
(0 - 1.0).当
android:type="radial"
.时这个指定的是中心点位置,并从这个位置开始渐变。
android:endColor
结束渐变的颜色
android:gradientRadius
半径值
只适用于
android:type="radial"
.
android:type
梯度类型
值 说明 "linear"一个线性渐变。这是默认的。 "radial"一个圆形渐变。 "sweep"扫描渐变
android:useLevel
Boolean
. "true" if this is used as a
LevelListDrawable
<padding>
填充视图(填充视图内容).
属性:
android:left
填充左边
android:top
填充上边
android:right
填充右边
android:bottom
填充底部
<size>
形状大小.
属性:
android:height
形状的高度
android:width
形状的宽度
Note:
shape尺寸过大,比如使用ImageView 设置控件大小比shape控件大小小 你可以设置
android:scaleType
to
"center"
. 这样就会截取中间部分
<solid>
填充实体,当设置渐变后这个标签会失效
属性:
android:color
Color
. 设置颜色
<stroke>
设置边框
属性:
android:width
边框线的宽度
android:color
边框线的颜色
android:dashWidth
每个虚线的大小
android:dashGap
边框线段的距离 设置
android:dashWidth使用
效果图
例子:
XML 文件位于
res/drawable/gradient_box.xml
:
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="rectangle">
<gradient
android:startColor="#FFFF0000"
android:endColor="#80FF00FF"
android:angle="45"/>
<padding android:left="7dp"
android:top="7dp"
android:right="7dp"
android:bottom="7dp" />
<corners android:radius="8dp" />
</shape>
应用到控件
<TextView
android:background="@drawable/gradient_box"
android:layout_height="wrap_content"
android:layout_width="wrap_content" />
代码使用:
Resources res = getResources();
Drawable shape = res. getDrawable(R.drawable.gradient_box);
TextView tv = (TextView)findViewByID(R.id.textview);
tv.setBackground(shape);
更多请看:
android.graphics.drawable.ShapeDrawable
转载请注明原文地址: https://ju.6miu.com/read-668658.html