[Android]Shape Drawable

导读:本篇文章讲解 [Android]Shape Drawable,希望对大家有帮助,欢迎收藏,转发!站点地址:www.bmabk.com

ShapeDrawable可以理解为通过颜色来构造的图形

[Android]Shape Drawable

 

 <android.widget.Button
       android:id="@+id/button1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Button"
        android:background="@drawable/shape"/>
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android">
    <solid android:color="@color/black"></solid>
    <corners android:radius="45dp"></corners>
    <gradient
        android:angle="90"
        android:startColor="@color/white"
        android:centerColor="@color/purple_200"
        android:endColor="@color/teal_200"
        ></gradient>
   <stroke
       android:width="2dp"
       android:color="@color/purple_700"
       ></stroke>
</shape>

各个属性的含义

android:shape

表示图片的形状,有四个选项:rectangle(矩形)、oval(椭圆)、line(横线)和ring(圆环),它的默认值是矩形。另外line和ring这两个选项必须要通过<stroke>标签来指定线的宽度和颜色等信息。

针对ring这个形状,有5个特殊的属性:android:innerRadius、android:thickness、android:immerRadiusRatio、android:thicknessRatio和android:useLevel,它们的含义如表所示。

Value Desciption
android:innerRadius 圆环的内半径,和android:innerRadiusRatio同时存在时,以android:innerRadius为准
android:thickness 圆环的厚度,即外半径减去内半径的大小,和android thicknessRatio同时存在时,以 android:thickness为准
android:innerRadiusRatio 内半径占整个Drawable宽度的比例,默认值为9。如果为n,那么内半径-宽度/n
 
android:thicknessRatio 厚度占整个Drawable宽度的比例,默认值为3.如果为n,那么厚度-宽度/n
android:useLevel 一般都应该使用false,否则有可能无法到达预期的显示效果,除非它被当作LevelListDrawable 来使用

<corners>

表示shape的四个角的角度。它只适用于矩形shape,这里的角度是指圆角的程度,用ps来表示,它有如下5个属性:
android:radius——为四个角同时设定相同的角度,优先级较低,会被其他四个属性覆盖:
android:topLeftRadius——设定最上角的角度;
android:topRightRadius——设定右上角的角度;
android:bottomLeftRadius——设定最下角的角度;
android:bottomRightRadius——设定右下角的角度。

<gradient>

它与<solid>标签是互相排斥的,其中solid表示纯色填充,而gradient则表示渐变效果,gradient有如下几个属性:
android:angle——渐变的角度,默认为0,其值必须为45的倍数,0表示从左到右, 90表示从下到上,具体的效果需要自行体验,总之角度会影响渐变的方向:

android:centerX——渐变的中心点的横坐标

android:centerY——渐变的中心点的纵坐标,渐变的中心点会影响渐变的具体效果:
android:startColor——渐变的起始色;
android:centerColor——渐变的中间色;
android:endColor——渐变的结束色;
android:gradientRadius——渐变半径,仅当android:type=”radial”时有效:
android:useLevel——一般为false,当Drawable作为StateListDrawable使用时为true:
android:type——渐变的类别,有linear(线性渐变)、radial(径向渐变)、sweep(扫描线渐变)三种,其中默认值为线性渐变。

<solid>
这个标签表示纯色填充,通过android:color即可指定shape中填充的颜色。
<stroke>
Shape的描边,有如下几个属性:
android:width——描边的宽度,越大则shape的边缘线就会看起来越粗;
android:color—描边的颜色:
android:dashWidth——组成虚线的线段的宽度:
android:dashGap——组成虚线的线段之间的间隔,间隔越大则虚线看起来空隙就越大

<padding>

表示包含它的View的空白,有四个属性:android:left、android:top、android:right和android:bottom。

<size>

shape的大小,有两个属性:android:width和android:height。<size>标签设置的宽/高就是ShapeDrawable的固有宽/高,但是作为View的背景时,shape还会被拉伸或者缩小为View的大小。

版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 举报,一经查实,本站将立刻删除。

文章由极客之音整理,本文链接:https://www.bmabk.com/index.php/post/95952.html

(0)
小半的头像小半

相关推荐

极客之音——专业性很强的中文编程技术网站,欢迎收藏到浏览器,订阅我们!