引言
Android中常常需要用到一些边框、背景之类的素材,而这些很多时候不是通过美工人员P图得到的,而是通过shape标签来实现的。今天就探究一下shape到底怎么用以及相关的一些特性。
shape的定义
本来想在网上找点简单易懂的中文解释,搜了半天没看到,于是乎就上Android官网上看了看,官网的解释是:
An XML file that defines a geometric shape, including colors and gradients.
大概意思就是:一种定义形状的xml文件,包括颜色、渐变。其实还包括圆角、内间距、边框等。总之是一种用于定义形状资源的文件。
shape使用
举一个小例子先看看
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="rectangle" >
<stroke
android:width="2dp"
android:color="#FFFFFF"
/>
<corners
android:radius="8dp"
/>
<solid
android:color="#00000000"
/>
</shape>
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
作为背景用于TextView
android:background="@drawable/recetegle_textview"
效果:
shape属性详解
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="rectangle" >
</shape>
这是shape文件的基本格式。
根标签是shape,其中属性:android:shape="……"
是必要属性,它规定shape的基本形状。
android:shape
它可选的值如下:
rectangle
oval
line
ring
android:shape=ring时的特殊属性
android:innerRadius="dimension" //内环的半径。
android:innerRadiusRatio="float" //这个值表示内部环的比例,例如,如果android:innerRadiusRatio = " 5 ",那么内部的半径等于环的宽度除以5。这个值会被android:innerRadius重写。 默认值是9。
android:thickness="dimension" //环的厚度.
android:thicknessRatio="float" //厚度的比例。例如,如果android:thicknessRatio= " 2 ",然后厚度等于环的宽度除以2。这个值是被android:innerRadius重写, 默认值是3。
android:useLevel="boolean" //如果用在 LevelListDrawable里,那么就是true。如果通常不出现则为false。
子属性
自属性是包含在shape内部的一些属性,主要包括:
corners
gradient
padding
size
solid
stroke
corners 圆角
<corners
android:radius="dimension"
android:topLeftRadius="dimension"
android:topRightRadius="dimension"
android:bottomLeftRadius="dimension"
android:bottomRightRadius="dimension" />
gradient渐变
<gradient
android:angle="integer"
android:centerX="float"
android:centerY="float"
android:centerColor="color"
android:endColor="color"
android:gradientRadius="integer"
android:startColor="color"
android:type="linear"
android:useLevel="boolean" />
padding 内边距
<padding
android:left="dimension"
android:top="dimension"
android:right="dimension"
android:bottom="dimension" />
size 大小
<size
android:width="dimension"
android:height="dimension" />
solid 填充
<solid
android:color="color" />
stroke 边框
<stroke
android:width="dimension"
android:color="color"
android:dashWidth="dimension"
android:dashGap="dimension" />
ShapeDrawable
ShapeDrawable是一个Drawable的子类用与绘制原始的shape。这里不再展开了。
需要详细了解的同学看这里
总结
shape是一个很好的绘制图片的工具,如果用好了,可以绘制出很棒的效果。
我画了个球,哈哈哈。
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="oval">
<gradient
android:startColor="#FFFFFF"
android:endColor="#000000"
android:centerX="0.6"
android:centerY="0.4"
android:type="radial"
android:gradientRadius="25dp"/>
<size
android:height="50dp"
android:width="50dp"/>
</shape>
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18