美文网首页
【总结】Drawable的用法,shape

【总结】Drawable的用法,shape

作者: 械勒的时间 | 来源:发表于2017-06-19 17:55 被阅读0次

shape是android开发中常用的一种drawable,它可以生成简单的图片或者背景色,相比于图片体积非常小,并且是android开发人员自己通过代码来实现,所以深受UI同学的爱戴。┑( ̄Д  ̄)┍

因为shape文件非常简单,就是一些规则的条条框框,所以分享一个shape文件,能用到的属性都在里头了

我自己一般就删吧删吧就拿来用了,非常好用
为了方便大家观看,我添加了详细的注释

删吧删吧拿来用的文件

 
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
    android:innerRadius="100dp"
    android:innerRadiusRatio="1"
    android:shape="ring"
    android:thickness="100dp"
    android:thicknessRatio="1"
    android:useLevel="false">
    <!--
    android:shape="rectangle"   矩形
    android:shape="oval"        椭圆
    android:shape="line"        横线
    android:shape="ring"        圆环
    line 和 ring 必须通过 <stroke> 标签来指定线宽和颜色信息,否则无法达到预期
    我们最常用的是 rectangle 矩形

    其中,若 shape="ring" 时候,有五个特殊的属性
    innerRadius 和 innerRadiusRatio ,用来控制圆环的内半径
    innerRadius 使用数字表示,当两个同时存在时,以它为准
    innerRadiusRatio 表示内半径占整个shape的比例,默认为9,优先级低

    thickness 和 thicknessRatio,用来控制圆环的厚度
    thickness 使用数字表示,当两个同时存在时,以它为准
    thicknessRatio 表示厚度占整个shape的比例,默认为3,优先级低

    useLevel 一般为false,否则达不到显示效果除非被当作LevelListDrawable使用时为true
    -->

    <corners
        android:bottomLeftRadius="10dp"
        android:bottomRightRadius="10dp"
        android:radius="10dp"
        android:topLeftRadius="10dp"
        android:topRightRadius="10dp" />
    <!--corners 表示角度,其中 radius 优先级最低,会被其他四个属性覆盖-->

    <solid android:color="#000000" />
    <!--solid 表示纯色填充,通过唯一的属性指定填充色-->

    <gradient
        android:angle="45"
        android:centerColor="#00FF00"
        android:centerX="0.6"
        android:centerY="0.6"
        android:endColor="#0000FF"
        android:gradientRadius="50dp"
        android:startColor="#FF0000"
        android:type="radial"
        android:useLevel="false" />
    <!--
    gradient 表示渐变色填充,他的几个属性的含义分别是
    angle 渐变角度,默认为0,器数值必须为45的倍数,这个属性描述起来很不直观,建议写出来观察
    centerX 渐变中心点的横坐标,范围0-1
    centerY 渐变中心点的纵坐标,范围0-1
    startColor 渐变起始颜色
    centerColor 渐变的中间颜色
    endColor 渐变的结束色
    gradientRadius 渐变的半径,当 type="radial" 的时候生效,可以通过改大这个属性来产生中心渐变的效果
    useLevel 一般为false,当Drawable作为StateListDrawable使用时为true
    -->
    <!--solid,gradient 这两个互斥,后写的生效-->

    <padding
        android:bottom="5dp"
        android:left="5dp"
        android:right="5dp"
        android:top="5dp" />
    <!--
    padding 代表边距,但它不是表示shape的边距,而是使用这个View的控件的内边距
    例如TextView使用了这个shape文件,就相当于是这个TextView的四个padding属性的默认值为5dp
    但是此处属性的优先级低于在TextView中定义的padding属性的优先级
    -->

    <size
        android:width="1000dp"
        android:height="1000dp" />
    <!--
    size 表示shape的故有大小,跟图片类似
    shape跟图片都有自身的固有大小,在相应情况下,比如做ImageView的src时,会用到固有大小来渲染控件
    但是用在做各种控件的背景时,shape与图片都会产生相应的尺寸改变
    -->

    <stroke
        android:width="10dp"
        android:color="#000000"
        android:dashGap="10dp"
        android:dashWidth="10dp" />
    <!--
    stroke 表示描边
    width 表示描边的宽度,越大的话shape边缘线会越粗
    color 描边的颜色
    dashWidth 组成虚线的线段的长度
    dashGap 组成虚线之间空白的长度,越大的话空隙会越大
    -->

</shape>

这个文件大概是这个样子


上文的shape

其中,shape的渐变有三种,名称分别为linear(线性渐变),radial(径向渐变)与sweep(扫描线渐变),上图中右下角那个渐变,实际上是一个红绿蓝的三色径向渐变。

从左到右分别为 linear radial sweep.png

这里渐变看起来很丑,因为我用了颜色分明的纯色来展示,这样比较清晰,工作中还是挺实用的一个属性。
简单的图形,基本上这就能搞定了,比如一些按钮的图片背景,毕竟代码实现复杂度有限,太难得还是老老实实找UI同学作图吧。

ps:可以给radius属性设置一个非常大的值,这样做背景的时候,可以保证图形是一个腰圆矩形(图左),防止图形在失配的时候,变形成圆角矩形纺锤形,或者用.9图时候,起毛边

圆角矩形

shape 的其他玩法

layer-list

layer-list 可以多个shape组合,shape 需要用<item></item>包裹


<?xml version="1.0" encoding="utf-8"?>
<layer-list xmlns:android="http://schemas.android.com/apk/res/android">
    <item>
        <shape>
            <!-- 圆角 -->
            <corners android:radius="100dp" />
            <!-- 大小 -->
            <size
                android:width="50dp"
                android:height="25dp" /><!-- 宽度和高度 -->

            <!-- 填充 -->
            <solid android:color="#A77D61" /><!-- 填充的颜色 -->
        </shape>
    </item>

    <item
        android:bottom="1dp"
        android:left="26dp"
        android:right="1dp"
        android:top="1dp">
        <shape>
            <!-- 圆角 -->
            <corners android:radius="100dp" />

            <!-- 大小 -->
            <size
                android:width="24dp"
                android:height="24dp" /><!-- 宽度和高度 -->

            <!-- 填充 -->
            <solid android:color="#FFFFFF" /><!-- 填充的颜色 -->

        </shape>
    </item>
</layer-list>

开关开启.png

这个在某些情况下,还是很实用的小技巧,如图就是我们项目里用作togglebutton,开启时的底图。


个人理解,难免有错误纰漏,欢迎指正。转载请注明出处。

相关文章

网友评论

      本文标题:【总结】Drawable的用法,shape

      本文链接:https://www.haomeiwen.com/subject/kmxnqxtx.html