6.2 Drawable的分类(一)

1. BitmapDrawable

<bitmap xmlns:android="http://schemas.android.com/apk/res/android"
    android:alpha="10"
    android:antialias="true"
    android:autoMirrored="true"
    android:dither="true"
    android:filter="false"
    android:gravity="center"
    android:mipMap="true"
    android:src="@drawable/logo"
    android:tileMode="repeat"
    android:tileModeX="repeat"
    android:tileModeY="mirror"
    android:tint="@color/colorAccent"
    android:tintMode="screen">

</bitmap>
  • src:图片资源id
  • alpha:设置图片的透明度,取值范围为0.0~1.0之间,0.0为全透明,1.0为全不透明,API Level最低要求是11,即Android 3.0
  • antialias:抗锯齿
  • autoMirrored:设置图片是否需要镜像反转,当布局方向是RTL,即从右到左布局时才有用,API Level 19(Android 4.4)才添加的属性
  • dither:防抖动
  • filter:设置是否允许对图片进行滤波,对图片进行收缩或者延展使用滤波可以获得平滑的外观效果
  • gravity:对齐方式
  • mipMap:纹理映射
  • =============================
  • tileMode:平铺方式
    平铺(repeat),镜像(mirror),扩展像素(clamp)
  • tileModeX:x方向平铺方式
  • tileModeY:y方向平铺方式
  • ==============================
  • tint:着色
  • tintMode:着色方式,5.0以后才有

2. NinePatchDrawable

<nine-patch xmlns:android="http://schemas.android.com/apk/res/android"
    android:src="@drawable/logo">

</nine-patch>
  • 这里的src必须是.9的图,否则报错

3. ShapeDrawable

<shape xmlns:android="http://schemas.android.com/apk/res/android"
    android:dither="true"
    android:innerRadius="50dp"
    android:innerRadiusRatio="9"
    android:shape="ring"
    android:thickness="10dp"
    android:thicknessRatio="3"
    android:tint="@color/colorAccent"
    android:tintMode="add"
    android:useLevel="false"
    android:visible="true">
    <stroke
        android:width="5dp"
        android:color="@color/colorPrimaryDark" />
</shape>
  • shape:图形形状 rectangle(矩形)、oval(椭圆)、line(横线)、ring(环)
  • userLeavel:一般为false,否则没有效果
  • visible:是否显示
  • dither:抖动
  • ================
  • tint|tintMode 着色
  • ================
  • innerRadius:内环大小
  • thickness:环的厚度(内环+厚度=环的直径)
  • innerRadiusRatio:内半径占整个Drawable宽度的比例,默认为9
  • thicknessRatio:厚度占整个Drawable宽度的比例
  • =============================
  • shape必须通过stroke(边框)或者solid(实心)或者gradient(渐变)指定颜色
<shape xmlns:android="http://schemas.android.com/apk/res/android"
    android:dither="true"
    android:shape="line"
    android:useLevel="false">
    <stroke
        android:width="2dp"
        android:color="@color/colorAccent" />
</shape>
  • line必须由stroke指定颜色和宽度
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
    android:shape="rectangle">
    <solid android:color="@color/colorAccent"></solid>
    <stroke
        android:width="5dp"
        android:color="@color/colorPrimaryDark"
        android:dashGap="3dp"
        android:dashWidth="3dp"></stroke>
    <corners
        android:bottomLeftRadius="5dp"
        android:bottomRightRadius="5dp"
        android:radius="10dp"
        android:topLeftRadius="5dp"
        android:topRightRadius="5dp" />
    <gradient
        android:angle="180"
        android:centerColor="@color/colorPrimary"
        android:centerX="0.5"
        android:centerY="0.5"
        android:endColor="@color/colorPrimaryDark"
        android:gradientRadius="10dp"
        android:startColor="@color/colorAccent"
        android:type="linear"
        android:useLevel="true" />
    <padding
        android:bottom="5dp"
        android:left="5dp"
        android:right="5dp"
        android:top="5dp" />
    <size
        android:width="100dp"
        android:height="100dp" />
</shape>
  • solid: 实心颜色
  • stroke:边框
    • width:边框宽度
    • color:边框颜色
    • dashGap:虚线间隔
    • dashWidth:虚线宽度
  • corners:边角
    • radius:圆角半径
    • 其他四个角的半径
  • gradient:渐变。渐变和solid是互斥的,solid是纯色,gradient是渐变。
    • angle:渐变的角度,默认为0,其值必须是45的倍数,0从左到右,90表示从下到上,180表示从右到左,顺时针顺序。
    • centerX,centerY:渐变的中心点
    • startColor,centerColor,endColor
    • type:渐变的类别,line(线性)/radial(径向)/sweep(扫描)
    • gradientRadius:渐变半径,仅当type为径向时有效
  • padding:不是shape的padding,而是包含它的View的padding。
  • size:这个表示shape的固有大小,但不是shape最终显示大小。shape没有宽高概念,作为View的背景会自适应View的宽高,对于图片Drawable,它的固有宽高就是图片尺寸,对于shapeDrawable,他没有固定宽高的概念,这个时候getIntrinsicWidht和getIntrinsicHeight会返回-1,但如果通过size标签来指定宽高信息,那么这个shapeDrawable就有固定宽高了,但作为View的背景,还会被拉伸或者缩小到Veiw的大小。
<shape xmlns:android="http://schemas.android.com/apk/res/android"
    android:shape="oval">
    <solid android:color="@color/colorAccent" />
    <size
        android:width="100dp"
        android:height="100dp" />
</shape>
  • oval是椭圆,正圆就是宽高一致即可。

4. LayerDrawable

<layer-list xmlns:android="http://schemas.android.com/apk/res/android">
    <item>
        <shape android:shape="rectangle">
            <solid android:color="#456789" />
        </shape>
    </item>
    <item
        android:bottom="3dp"
        android:left="3dp"
        android:right="3dp">
        <shape>
            <solid android:color="#ffffff" />
        </shape>
    </item>
    <item android:bottom="16dp">
        <shape>
            <solid android:color="#ffffff" />
        </shape>
    </item>
</layer-list>
  • layer-list可以包含item,item可以包含shape
  • bottom,left,top,right等可以理解为padding属性
  • layer-list可以理解为frameLayout
  • item可以有自己的drawable属性

5. StateListDrawable

<selector xmlns:android="http://schemas.android.com/apk/res/android">
    <item android:drawable="@drawable/shape_oval" 
        android:state_checked="true" 
        android:state_enabled="true" 
        android:state_first="true" 
        android:state_last="true" 
        android:state_middle="true" 
        android:state_pressed="true" 
        android:state_selected="true" />
    <item android:drawable="@drawable/shape_ring" />
</selector>
  • state_checked:view是否checked
  • state_enabled: View是否可以
  • state_first: View是否处于开始状态
  • state_last|state_middle: View是否处于中间状态|是否处于结束状态
  • state_pressed: View是否被按下
  • state_selected: View是否被选中

6. LevelListDrawable

<level-list xmlns:android="http://schemas.android.com/apk/res/android">
    <item
        android:drawable="@drawable/shape_oval"
        android:maxLevel="2"
        android:minLevel="2" />
    <item
        android:drawable="@drawable/shape_ring"
        android:maxLevel="1"
        android:minLevel="1" />
    <item android:drawable="@drawable/shape" />
</level-list>
LevelListDrawable drawable = (LevelListDrawable) findViewById(R.id.level).getBackground();
drawable.setLevel(1);
  • level-list表示一个Drawable集合,集合中每个Drawable都有一个等级的概念,等级不同,显示不同的Drawable。
  • 等级在minLevel和maxLevel之间的Drawable会被显示。
  • 如果level为2,而item1的maxLevel为2,而item2的minLevel为2,则maxLevel优先,显示item1.
  • drawable的等级是有范围的0-10000是,0是默认值。

7. TransitionDrawable

<transition xmlns:android="http://schemas.android.com/apk/res/android">
    <item android:drawable="@drawable/shape_oval" />
    <item android:drawable="@drawable/shape" />
</transition>
TransitionDrawable drawable = (TransitionDrawable) findViewById(R.id.transition).getBackground();
drawable.startTransition(5000);
  • TransitionDrawable是LayerDrawable的子类,本身就支持多item。
  • 它实现两个Drawable切换时的淡入淡出。
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
  • 序言:七十年代末,一起剥皮案震惊了整个滨河市,随后出现的几起案子,更是在滨河造成了极大的恐慌,老刑警刘岩,带你破解...
    沈念sama阅读 158,425评论 4 361
  • 序言:滨河连续发生了三起死亡事件,死亡现场离奇诡异,居然都是意外死亡,警方通过查阅死者的电脑和手机,发现死者居然都...
    沈念sama阅读 67,058评论 1 291
  • 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
    开封第一讲书人阅读 108,186评论 0 243
  • 文/不坏的土叔 我叫张陵,是天一观的道长。 经常有香客问我,道长,这世上最难降的妖魔是什么? 我笑而不...
    开封第一讲书人阅读 43,848评论 0 204
  • 正文 为了忘掉前任,我火速办了婚礼,结果婚礼上,老公的妹妹穿的比我还像新娘。我一直安慰自己,他们只是感情好,可当我...
    茶点故事阅读 52,249评论 3 286
  • 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
    开封第一讲书人阅读 40,554评论 1 216
  • 那天,我揣着相机与录音,去河边找鬼。 笑死,一个胖子当着我的面吹牛,可吹牛的内容都是我干的。 我是一名探鬼主播,决...
    沈念sama阅读 31,830评论 2 312
  • 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
    开封第一讲书人阅读 30,536评论 0 197
  • 序言:老挝万荣一对情侣失踪,失踪者是张志新(化名)和其女友刘颖,没想到半个月后,有当地人在树林里发现了一具尸体,经...
    沈念sama阅读 34,239评论 1 241
  • 正文 独居荒郊野岭守林人离奇死亡,尸身上长有42处带血的脓包…… 初始之章·张勋 以下内容为张勋视角 年9月15日...
    茶点故事阅读 30,505评论 2 244
  • 正文 我和宋清朗相恋三年,在试婚纱的时候发现自己被绿了。 大学时的朋友给我发了我未婚夫和他白月光在一起吃饭的照片。...
    茶点故事阅读 32,004评论 1 258
  • 序言:一个原本活蹦乱跳的男人离奇死亡,死状恐怖,灵堂内的尸体忽然破棺而出,到底是诈尸还是另有隐情,我是刑警宁泽,带...
    沈念sama阅读 28,346评论 2 253
  • 正文 年R本政府宣布,位于F岛的核电站,受9级特大地震影响,放射性物质发生泄漏。R本人自食恶果不足惜,却给世界环境...
    茶点故事阅读 32,999评论 3 235
  • 文/蒙蒙 一、第九天 我趴在偏房一处隐蔽的房顶上张望。 院中可真热闹,春花似锦、人声如沸。这庄子的主人今日做“春日...
    开封第一讲书人阅读 26,060评论 0 8
  • 文/苍兰香墨 我抬头看了看天上的太阳。三九已至,却和暖如春,着一层夹袄步出监牢的瞬间,已是汗流浃背。 一阵脚步声响...
    开封第一讲书人阅读 26,821评论 0 194
  • 我被黑心中介骗来泰国打工, 没想到刚下飞机就差点儿被人妖公主榨干…… 1. 我叫王不留,地道东北人。 一个月前我还...
    沈念sama阅读 35,574评论 2 271
  • 正文 我出身青楼,却偏偏与公主长得像,于是被迫代替她去往敌国和亲。 传闻我的和亲对象是个残疾皇子,可洞房花烛夜当晚...
    茶点故事阅读 35,480评论 2 267

推荐阅读更多精彩内容