Drawable及其子类(BitmapDrawable、ShapeDrawable等)

一、BitmapDrawable:(.9格式的图片NinePatchDrawable类似)表示一张图片,在开发中我们可以使用原始的图片或者通过xml的方式来描述。xml描述如下:

<?xml version="1.0" encoding="utf-8"?>
<bitmap 
    android:src(图片资源id)
    android:antialias(是否开启抗锯齿功能)
    android:dither(是否开启抖动效果)
    android:filter(是否开启过滤)
    android:gravity
    android:mipMap
    android:tileMode
    >
</bitmap>

二.ShapeDrawable:通过颜色来构造图片。xml描述如下:

<?xml version="1.0" encoding="utf-8"?>
<shape
    android:shape="rectangle | oval | line | ring"
    >
    <corners>
        ....适用于shape,表示四个角的角度;
    </corners>

    <gradient>
        ....和<solid>相互排斥,表示渐变的效果,而solid表示纯色填充;
    </gradient>

    <solid>
        ....表示纯色填充,用android:color可指定该颜色;
    </solid>

    <stroke>
        ....shape的描边
    </stroke>

    <padding>
        ....表示空白
    </padding>

    <size>
        ....可以表示shape的宽高
    </size>

</shape>

三 LayerDrawable:表示一种层次化的drawable集合。

<?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>

五. StateListDrawable:表示drawable集合,主要用于设置可单击的view的背景。

<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android"
    android:constantSize="false"
    .
    .
    .
     >
    <item 
        android:state_focused=""
        .
        .
        .
        ></item>
</selector>

六.LevelListDrawable:集合中的每个Drawable都有一个等级的概念,根据不同的等级,LevelListDrawable会切换到对应的drawable。

七.TransitionDrawable:对应于 实现2个drawable之间的淡入淡出

<?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>

八.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>

九. ScaleDrawable:对应于,可以根据之间的等级将指定的drawable缩放到一定的比例;

<?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" />

十.ClipDrawable:对应于,可以根据自己当前的等级(level)来裁剪另一个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" />
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值