Android Drawable 分类


Drawable 是一种可以在Canvas上进行绘制的一种抽象资源。可以表达颜色、图片。通常都是通过xml来定义,当然也可以通过代码来定义。drawable内部的宽高元素比较重要。getIntrinsicWidth getIntrinsicHeight .
优点:使用简单。比自定义view成本更低;其次,非图片类型的drawable占用内容空间小。

1.BitmapDrawable

<?xml version-"1.0" encoding-"utf-8"?>
<bitmap
xmlns:android="http://schemas. android. com/apk/res/android"
android:src="@ (package: ]drawable/drawable resource"
android:antialias= ["true"I "false"]
android:dither= ["true"I "false"]
android:filter=("true" 1 "false"]
android:gravity=("top" I "bottom" I"left" "right" 1 "center vertical" 1
"fill_ vertical" I"center horizontal" 1 "fill hori zontal"
"center" 1 "fill" I "clip vertical" I "clip horizontal"]
android:mipMap= ["true"I "false"]
android:tileMode= ["disabled" 1 "clamp" 1 "repeat" 1 "mirror"] />
  • src
  • antialias 抗锯齿
  • dither 开启抖动效果
  • filter 开启过滤效果
  • gravity 布局内容摆放位置
  • mipmap 纹理映射
  • tileMode 平铺模式

2.shapeDrawable

<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
    android:shape="rectangle" >

    <solid android:color="#ff0000" />

    <corners
        android:bottomLeftRadius="0dp"
        android:bottomRightRadius="15dp"
        android:topLeftRadius="10dp"
        android:topRightRadius="15dp" />

</shape>
  • shape 四种形状 rectangle 矩形 oval 椭圆 line 线 ring 圆环

  • corners android:radius 为四个角 设置相同的角度

  • gradient 渐变效果

    • angle渐变的角度
    • centerX centerY 渐变中心坐标
    • startColor centerColor endColor
    • gradientRadius 渐变半径
    • type 渐变类型 线性line 径向渐变(radial) sweep(扫描线渐变)
  • solid 纯色填充

  • stoke 描边

    • width
    • color
    • dashWidth 虚线 线段的宽度
    • dishGap 虚线 线段 之间的间隔
  • size shape的宽高大小
    width height 形状的大小

3.LayerDrawable

<?xml version="1.0" encoding="utf-8"?>
<layer-list xmlns:android="http://schemas.android.com/apk/res/android">

    <item>
        <shape android:shape="rectangle" >
            <solid android:color="#0ac39e" />
        </shape>
    </item>

    <item android:bottom="6dp">
        <shape android:shape="rectangle" >
            <solid android:color="#ffffff" />
        </shape>
    </item>

    <item
        android:bottom="1dp"
        android:left="1dp"
        android:right="1dp">
        <shape android:shape="rectangle" >
            <solid android:color="#ffffff" />
        </shape>
    </item>

</layer-list>

4.StateListDrawable

对应于标签
android:constantSize 固定大小 ,状态的改变是否会引用drawable的大小。

android:dither 是否开启抖动
android:variablePadding

<?xml version="1.0" encoding="utf-8"?>
<layer-list xmlns:android="http://schemas.android.com/apk/res/android" >

    <item>
        <shape android:shape="rectangle" >
            <solid android:color="#0ac39e" />
        </shape>
    </item>

    <item android:bottom="6dp">
        <shape android:shape="rectangle" >
            <solid android:color="#ffffff" />
        </shape>
    </item>

    <item
        android:bottom="1dp"
        android:left="1dp"
        android:right="1dp">
        <shape android:shape="rectangle" >
            <solid android:color="#ffffff" />
        </shape>
    </item>

</layer-list>

5.LevelListDrawable

表示是一个drawable集合对象。每个drawable 有个level等级概念。通过设置level来展示不同drawable
在这里插入图片描述

6.TransitionDrawable

transition 来实现drable之间淡出淡入的效果。

<?xml version="1.0" encoding="utf-8"?>
<transition xmlns:android="http://schemas.android.com/apk/res/android" >

    <item android:drawable="@drawable/shape_drawable_gradient_linear"/>
    <item android:drawable="@drawable/shape_drawable_gradient_radius"/>

</transition>

7.InsetDrawable

对象标签 他可以把drawable内嵌到自身当中,同时四周留有一定的间距。

<?xml version="1.0" encoding="utf-8"?>
<inset xmlns:android="http://schemas.android.com/apk/res/android"
    android:insetBottom="15dp"
    android:insetLeft="15dp"
    android:insetRight="15dp"
    android:insetTop="15dp" >

    <shape android:shape="rectangle" >
        <solid android:color="#ff0000" />
    </shape>
</inset>

8.ScaleDrawable

scale 缩放,可以根据level等级缩放到一定比例.

<?xml version="1.0" encoding="utf-8"?>
<scale xmlns:android="http://schemas.android.com/apk/res/android"
    android:drawable="@drawable/image1"
    android:scaleHeight="70%"
    android:scaleWidth="70%"
    android:scaleGravity="center" />

9.ClipDrawable

裁剪drawable

<?xml version="1.0" encoding="utf-8"?>
<clip xmlns:android="http://schemas.android.com/apk/res/android"
    android:clipOrientation="vertical"
    android:drawable="@drawable/image1"
    android:gravity="bottom" />

代码参考:https://github.com/chentaishan/android-art-res/blob/master/Chapter_6/res/drawable/scale_drawable.xml

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值