ShapeDrawable资源的制作与使用

Shape是xml绘图不可或缺的一部分,本文将通过一组实例来展示它的使用方法。

(1)保存与引用

保存位置:
res/drawable/filename.xml,文件名用作资源ID。
引用:
java文件中使用R.drawable.filename
xml文件中使用@[package:]drawable/filename
下面用一个实例来作演示。首先在res/drawable文件夹下创建一个xml文件,取名为shape_round_rect,内容如下:

<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
    android:shape="rectangle">
    <corners android:radius="40dip"/>
    <solid android:color="#8800ffff"/>

</shape>

这样就创建了一个圆角矩形资源。使用它有两种方法:
(1)xml中引用资源

android:background="@drawable/shape_round_rect"

简单地将其设置为某个view的background即可。
(2)代码中引用资源

TextView textView = (TextView)findViewById(R.id.text_view);
Drawable drawable = getResources().getDrawableForDensity(R.drawable.shape_round_rect,(int)getResources().getDisplayMetrics().density,null);
textView.setBackground(drawable);

注:低版本下直接用getDrawable()代替getDrawableForDensity()。
效果如下:
圆角矩形

(2)创建shape资源

首先,将xml的根标签设置为shape,并定义XML命名空间,如下:

<shape 
xmlns:android="http://schemas.android.com/apk/res/android">
</shape>

接下来看看shape标签下的属性:

android:shape=["rectangle" | "oval" | "line" | "ring"]

前三个分别为矩形、椭圆与直线,就不过多解释了,重点看看ring(环形)。下面几个属性只对ring有效:

android:innerRadius="integer"

内环半径,用尺寸值表示。

android:innerRadiusRatio="integer"

环宽度与内环半径的比值,默认为9,即内环半径等于环宽度除以9。该属性会被innerRadius覆盖。

android:thickness="integer"

环厚度,用尺寸值表示。

android:thicknessRatio="integer"

环宽度与环厚度的比值,默认为3,即环厚度等于环宽度除以3。该属性会被thickness覆盖。

android:useLevel=["true" | "false"]

这通常应为“false”,否则形状不会显示。
下面看一个示例:

<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
    android:shape="ring"
    android:useLevel="false"
    android:innerRadiusRatio="4"
    android:thicknessRatio="4">
    <solid android:color="#00ffff"/>
</shape>

环
shape标签下就这么多属性了,接下来看看shape的几个子标签。

<solid
    android:color="color" />

用于填充颜色,属性也只有一个color,输入#AARRGGBB值或是#RRGGBB值均可。

<corners
    android:radius="integer"
    android:topLeftRadius="integer"
    android:topRightRadius="integer"
    android:bottomLeftRadius="integer"
    android:bottomRightRadius="integer" />

用于为形状产生圆角,只有在形状是矩形的时候才有效。radius属性定义所有角的半径,另外四个分别对应矩形的四个角的半径。注意radius会被后四个覆盖。开头的圆角矩形就是用这个子标签实现的。

<gradient
    android:type=["linear" | "radial" | "sweep"]
    android:angle="integer"
    android:centerX="float"
    android:centerY="float"
    android:centerColor="integer"
    android:endColor="color"
    android:gradientRadius="integer"
    android:startColor="color"
    android:useLevel=["true" | "false"] />

这个属性稍微有点多,但其中有些只在特定的type中有效。首先看type:

android:type=["linear" | "radial" | "sweep"]

有线性、辐射、扫描三种。

android:startColor="color"
android:centerColor="integer"
android:endColor="color"

定义开始、中间、结束位置的颜色。这个类似动画中的关键帧,颜色会根据这个顺序逐渐变化。

android:centerX="float"
android:centerY="float"

定义渐变中心的位置,取值在0~1之间。

android:angle="integer"

定义渐变的角度,必须是 45 的倍数。0 为从左到右,90 为从下到上。默认值为 0。

android:gradientRadius="integer"

辐射半径,只在type为radial时有效。
接下来看几个实例(只贴出一种情况的代码):
1、线性渐变,角度0——45——90:

<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
    android:shape="rectangle">
    <corners android:radius="40dip"/>
    <gradient
        android:type="linear"
        android:angle="0"
        android:startColor="#ff0000"
        android:centerColor="#00ff00"
        android:endColor="#0000ff"/>
</shape>

linear
2、辐射渐变,半径逐渐增大。

<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
    android:shape="rectangle">
    <corners android:radius="40dip"/>
    <gradient
        android:type="radial"
        android:gradientRadius="15dp"
        android:startColor="#ff0000"
        android:centerColor="#00ff00"
        android:endColor="#0000ff"/>
</shape>

radial
3、扫描渐变,中心不同:

<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
    android:shape="rectangle">
    <corners android:radius="40dip"/>
    <gradient
        android:type="sweep"
        android:centerX="0.3"
        android:centerY="0.7"
        android:startColor="#ff0000"
        android:centerColor="#00ff00"
        android:endColor="#0000ff"/>
</shape>

sweep
gradient标签就介绍到这里,下面看看padding与size属性:

<padding
    android:left="integer"
    android:top="integer"
    android:right="integer"
    android:bottom="integer" />
<size
    android:width="integer"
    android:height="integer" />

分别代表尺寸与边距。这两个属性,个人认为没必要在制作资源时设置,而且使用方法也和View中一致,就不介绍了。
最后是stroke:

<stroke
    android:width="integer"
    android:color="color"
    android:dashWidth="integer"
    android:dashGap="integer" />

用于制作虚线或实线边框。四个属性分别为线宽、颜色、每划的长度、两划之间的间隔。直接上实例:

<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android">
    <stroke
        android:width="10dp"
        android:color="#00ffff"
        android:dashGap="4dp"
        android:dashWidth="8dp"/>
</shape>

stroke
以上内容大体上就是Shape资源的全部内容了,掌握Shape资源的制作,对于实现更加美观的UI有很大帮助。

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值