android进阶(六)-----Android Drawable

136 阅读3分钟

一、Drawable简介

Drawable一般通过XML来定义,Drawable是一个抽象类,是所有Drawable的基类。

二、Drawable分类

1、BitmapDrawable:表示一张图片,开发中,可以直接引用原始的图片,但是也可以通过XML来描述它。例如:

<?xml version="1.0" encoding="utf-8"> 
<bitmap xmlns:android="http://schemas.android.com/apk/res/android" 
    android:src="@[package:]drawavke/drawable_resource" 
    android:antialias=["true"|"false"] 
    android:dither=["true"|"false"] 
    android:filter=["true"|"false"] 
    android:gravity=["top"|"bottom"|"left"|"right"|"center_vertical"|"fill_vertical"|"venter_horizontal"|"fill_horizontal"|"center"|"fill"|"clip_vertical"|"clip_horizontal"] 
    android:mipMap=["true"|"false"] 
    android:tileMode=["disabled"|"clamp"|"repeat"|"mirror"]/>

src:图片的资源id

antialias:是否开启图片锯齿功能

dither:是否开启抖动效果

filter:是否开启过滤效果

gravity:对图片进行定位

mipMap:图像相关的处理技术,也叫纹理映射

tileMode:平铺模式。

2、ShapeDrawable

ShapeDrawable是一种常见的Drawable,可以理解为通过颜色来构造的图形,既可以是纯色的图形,也可以是渐变的图形。

<?xml version="1.0" encoding="utf-8"> 
<shape xmlns:android="http://schemas.android.com/apk/res/android" 
    android:shape=["rectangle"|"oval"|"line"|"ring"]> 
    <corners android:radius="integer" 
        android:topLeftRadius="integer" 
        android:topRightRadius="integer" 
        android:bottomLeftRadius="integer" 
        android:bottomRightRadius="integer"/> 
    <gradient android:angle="integer" 
        android:centerX="integer" 
        android:centerY="integer" 
        android:centerColor="interger" 
        android:endColor="color" 
        android:gradientRadius="integer" 
        android:startColor="color" 
        android:type=["linear"|"radial"|"sweep"] 
        android:useLevel=["true"|"false"]/> 
    <padding android:left="integer" 
        android:top="integer" 
        android:right="integer" 
        android:bottom="integer"/> 
    <size android:width="integer" 
        android:height="integer"/> 
    <solid android:color="color"/> 
    <stroke android:width="integer" 
        android:color="color" 
        android:dashWidth="integer" 
        android:dashGap="integer"/> 
    </shape>

(1)shape:表示图形的形状,rectangle(矩形)、oval(椭圆)、line(横线)、ring(圆环)

(2)corners:表示shape的角度,只适用于矩形shape

radius:为四个角同时设定相同的角度,优先级低,会被其他四个属性覆盖

toLeftRadius:左上角的角度

toRightRadius:右上角的角度

bottomLeftRadius:左下角的角度

bottomRightRadius:右下角的角度

(3)gradient:与solid标签互斥,solid纯色填充,而gradient渐变效果

angle:渐变的角度,值必须是45的倍数,0表示从左到右,90表示从下到上

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

centerY:渐变的中心点的纵坐标,渐变的中心点会影响渐变的具体效果

startColor:渐变的起始色

centerColor:渐变的中间色

endColor:渐变的结束色

gradientRadius:渐变的半径,当type=“radial”有效

useLevel:一般为false,当Drawable作为StateListDrawable使用时为true

type:渐变的类别,有linear(线性渐变)、radial(径向渐变)、sweep(扫描线渐变)

(4)solid:纯色填充,通过color即可指定shape填充的颜色

(5)stroke:Shape的描边

width:描边的宽度,越大则shape的边缘线就会越粗

color:描边的颜色

dashWidth:虚线的线段宽度

dashGap:虚线的线段间隔

(6)padding:表示空白,不是shape的空白,是包含的View的空白,有四个属性:left、top、right、bottom

(7)size:shape的大小:有两个属性width和height,表示shape的宽高

3、LayerDrawable

LayerDrawable对应的XML标签是,表示一种层次的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="#ccc"/> 
        </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

对应于标签,也是表示Drawable集合,每个Drawable对应View的一种状态。主要用于设置单击View的背景

<?xml version="1.0" encoding="utf-8"?> 
<selector xmlns:android="http://schemas.android.com/apk/res/android" 
    android:constantSize=["true"|"false"]
    android:dither=["true"|"fase"] 
    android:variablePadding=["true"|"false"]>
    <item android:drawable="@[package:]drawable/drawable_resource"
        android:state_pressed=["true"|"false"] 
        android:state_focused=["true"|"false"]
        android:state_hovered=["true"|"false"] 
        android:state_selected=["true"|"false"]
        android:state_checkable=["true"|"false"] 
        android:state_enabled=["true"|"false"]
        android:state_activated=["true"|"false"] 
        android:state_window_focused=["true"|"false"]/>
</selector>

constantSize:是否随着状态改变固有大小,默认是false(随状态改变而改变)

dither:是否开启抖动效果

variablePadding:是否随状态改变而改变,true改变,false表示StateListDrawable的paddiing是内部所有Drawable的padding的最大值。默认false,不建议开启

5、LevelListDrawable

对应于标签,表示一个Drawable集合,集合中每个Drawable都有一个等级概念,根据等级不同切换不同Drawable。等级范围0-10000

<?xml version="1.0" encoding="utf-8"?> 
<level-list xmlns:android="http://schemas.android.com/apk/res/android"> 
    <item android:drawable="@drawable/status_off" 
        android:maxLevel="0"/> 
        <item android:drawable="@drawable/status_on" 
            android:maxLevel="1"/> 
</selector>

6、TransitionDrawable

对应于标签,用于实现两个Drawable之间的淡入淡出

<?xml version="1.0" encoding="utf-8"?> 
<transition xmlns:android="http://schemas.android.com/apk/res/android"> 
    <item android:drawable="@[package:]@drawable/status_resource" 
        android:id="@[+][package:]id/resource_name" 
        android:top="dimension" 
        android:right="dimension" 
        android:bottom="dimension" 
        android:left="dimension"/> 
</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> 
</transition> 

8、ScaleDrawable

对应于标签,可以根据自己的等级将指定的Drawable缩放到一定比例。

<?xml version="1.0" encoding="utf-8"?> 
<scale xmlns:android="http://schemas.android.com/apk/res/android" 
    android:idrawable="drawable/drawable_resource" 
    android:scaleGravity=["top"|"bottom"|"left"|"right"|"center_vertical"|"fill_vertical"|"venter_horizontal"|"fill_horizontal"|"center"|"fill","clip_vertical","clip_horizontal"] 
    android:scaleHeight="percentage" 
    android:scaleWidth="percentage"> 
    <shape android:shape="rectangle"> 
        <solid android:color="#ff0000"> 
    </shape> 
</transition>

9、ClipDrawable

对应于标签,根据自己当前的等级来裁剪另一个Drawable,裁剪方向通过android:clipOrientation和android:gravity控制

<?xml version="1.0" encoding="utf-8"?> 
<clip xmlns:android="http://schemas.android.com/apk/res/android" 
    android:idrawable="drawable/drawable_resource" 
    android:clipOrientation=["horizontal"|"vertical"] 
    android:gravity=["top"|"bottom"|"left"|"right"|"center_vertical"|"fill_vertical"|"venter_horizontal"|"fill_horizontal"|"center"|"fill","clip_vertical","clip_horizontal"] 
    android:scaleHeight="percentage" 
    android:scaleWidth="percentage"> 
    <shape android:shape="rectangle"> 
        <solid android:color="#ff0000"> 
    </shape> 
</clip>