Android中常常需要用到一些边框、背景之类的素材,而这些很多时候不是通过美工人员P图得到的,而是通过shape标签来实现的。今天就探究一下shape到底怎么用以及相关的一些特性。
本来想在网上找点简单易懂的中文解释,搜了半天没看到,于是乎就上Android官网上看了看,官网的解释是:
An xml file that defines a geometric shape, including colors and gradients.11大概意思就是:一种定义形状的xml文件,包括颜色、渐变。其实还包括圆角、内间距、边框等。总之是一种用于定义形状资源的文件。
举一个小例子先看看
<?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>12345678910111213141516171234567891011121314151617作为背景用于TextView
android:background="@drawable/recetegle_textview"11效果:
这是shape文件的基本格式。 根标签是shape,其中属性:android:shape="……"是必要属性,它规定shape的基本形状。
它可选的值如下:
rectangle //矩形oval //椭圆line //线ring //圆环12341234自属性是包含在shape内部的一些属性,主要包括:
corners //圆角gradient//渐变padding //内边距size //大小solid //填充stroke //边框12345671234567ShapeDrawable是一个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>123456789101112131415161718123456789101112131415161718
新闻热点
疑难解答