drawable中xml的shape标签
介绍
1概述
android中的shape属于drawable中的xml绘制的标签,主要用于绘制简单的图形
2作用
- 占内存小
- 加载快
- 不会出现失真情况
3属性
3.1demo
新建drawable下shape_demo.xml
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:dither="false"
android:innerRadius="10dp"
android:innerRadiusRatio="10"
android:shape="ring"
android:thickness="10dp"
android:thicknessRatio="10"
android:tint="#999999"
android:tintMode="multiply"
android:useLevel="false"
android:visible="true">
<corners
android:bottomLeftRadius="10dp"
android:bottomRightRadius="10dp"
android:radius="10dp"
android:topLeftRadius="10dp"
android:topRightRadius="10dp" />
<size
android:width="100dp"
android:height="10dp" />
<padding
android:bottom="5dp"
android:left="5dp"
android:right="5dp"
android:top="5dp" />
<solid android:color="#CA3434" />
<stroke
android:width="1dp"
android:color="#FFBA0D"
android:dashWidth="0dp"
android:dashGap="10dp" />
<gradient
android:angle="100"
android:centerColor="#FF3030"
android:centerX="0"
android:centerY="0"
android:endColor="#0048FF"
android:gradientRadius="100"
android:startColor="#41814A"
android:type="linear"
android:useLevel="true" />
</shape>
3.2详解
3.2.1shape(形状)
- dither:在位图的像素配置与屏幕不同时(例如:ARGB 8888 位图和 RGB 565 屏幕)启用位图的抖动;值为“false”时则停用抖动。默认值为 true
- innerRadius:shape为ring时可用,内环半径
- innerRadiusRatio:shape为ring时可用,内环的厚度比,即环的宽度比表示内环半径,默认为3,可被innerRadius值覆盖
- shape:分别为rectangle(矩形)、line(线)、oval(椭圆)、ring(环)。默认为rectangle(矩形)
- thickness:shape为ring时可用,环的厚度
- thicknessRatio:shape为ring时可用,环的厚度比,即环的宽度比表示环的厚度,默认为9,可被thickness值覆盖
- tint:给shape着色
- tintMode:shape着色类型:src_in、src_atop、src_over、add、multiply、screen
- useLevel:较少用,一般设为false,否则图形不显示。为true时可在LevelListDrawable使用
- ** visible**:true、false
3.2.2corners(圆角)
- bottomLeftRadius:左下角圆角半径
- bottomRightRadius:右下角圆角半径
- radius:圆角半径,该值设置时其他四个属性失效
- topLeftRadius:左上角圆角半径
- topRightRadius:右上角圆角半径
3.2.3size(大小)
- width:宽度
- height:高度
3.2.4padding(内边距)
- bottom:底部边距
- left:左边边距
- right:右边边距
- top:顶部边距
3.2.5solid(填充)
- color:shape的填充色
3.2.6stroke(描边)
- width:描边的宽度
- color:描边的颜色
- dashWidth:虚线宽度
- dashGap:虚线间隔
3.2.7gradient(渐变)
- angle:渐变角度,当type为linear(线性)时有效。角度为45的倍数,0度时从左往右渐变,角度方向逆时针
- centerColor:渐变中间位置颜色
- centerX:type为radial(放射性)时有效,设置渐变中心的X坐标,取值区间[0,1],默认为0.5,即中心位置
- centerY:type为radial(放射性)时有效,设置渐变中心的Y坐标,取值区间[0,1],默认为0.5,即中心位置
- endColor:渐变结束位置颜色
- gradientRadius:type为radial(放射性)时有效,渐变的半径
- startColor:渐变开始位置颜色
- type:渐变类型,分别为linear(线性)、radial(放射性)、sweep(扫描性)渐变,默认为linear(线性)
- useLevel:与shape中该属性的一致
使用
1layout中的xml布局
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<TextView
android:id="@+id/tv_shape"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="@drawable/shape_demo"
/>
</LinearLayout>
2代码activity中使用
Resources res = getResources();
Drawable shape = res.getDrawable(R.drawable.shape_demo);
TextView tv = findViewById(R.id.tv_shape);
tv.setBackgroundDrawable(shape);