Android Drawable的那些事儿


Drawable和View区别   ε-(´∀`; )

Drawable是一个抽象类,是对可绘制物件的抽象。
与View不同,Drawable没有时间和交互的方法,而View直接面向用户,可以为其添加事件。
Drawable有很多子类操作类型资源,如下

  • BitmapDrawable
  • LayerDrawable
  • StateListDrawable
  • ClipDrawable
  • LevelListDrawable
  • TransitionDrawable
  • InsertDrawable
  • ClipDrawable
  • ShapeDrawable
  • ……

╭( ′• o •′ )╭☞ Drawable原理

draw(Canvas canvas)在其中通过画布进行绘制;
setBounds()指定绘制的边界,通常传入一个矩形的绘制对象对其进行绘制。


1.BitmapDrawable

BitmapDrawable是对bitmap的一种包装,可以设置它所包装的bitmap在BitmapDrawable区域内的绘制方式
包括:平铺、拉伸或者保持原始大小等等

  • android:tileMode="mirror" 模式
    mirror:镜像
    repeat:重复
    disable:平铺
    clamp:图片四周的像素扩散到周围区域

  • android:antialias="true" 抗锯齿

  • android:dither="true" 抖动

  • android:filter="true" 过滤(图片拉伸或压缩建议开启过滤)

<?xml version="1.0" encoding="utf-8"?>
<bitmap xmlns:android="http://schemas.android.com/apk/res/android"
    android:src="@drawable/ic_avatar"
    android:tileMode="mirror"
    android:antialias="true"
    android:dither="true"
    android:filter="true">

</bitmap>

2.LayerDrawable

LayerDrawable管理一组drawable对象在LayerDrawable的绘制顺序,列表最后一个drawable在最上层


image.png
<?xml version="1.0" encoding="utf-8"?>
<layer-list xmlns:android="http://schemas.android.com/apk/res/android">
    <item
        android:left="20dp"
        android:top="20dp">
        <bitmap
            android:gravity="center"
            android:src="@drawable/ic_avatar1"
    </item>
    
    <item
        android:left="20dp"
        android:top="20dp">
        <bitmap
            android:gravity="center"
            android:src="@drawable/ic_avatar2"
    </item>
</layer-list>

3.StateListDrawable

根据不同、状态提供不同的背景。比如按钮获取失去焦点,点击等。

状态 含义
android:state_pressed 按下的状态,(按下但是还没松开)
android:state_focused 当前View获取了焦点
android:state_selected 用户选择了当前View
android:state_checked 用户选中了View,一般用于CheckBox这种非黑即白的选项
android:state_enabled 当前View处于可用的状态
android:state_hovered 光标是否悬停,通常与focused state相同,它是4.0的新特性
android:state_checkable 组件是否能被check。如:RadioButton是可以被check的。
android:state_activated 是否被激活
android:state_window_focused 应用程序是否在前台,当有通知栏被拉下来或者一个对话框弹出的时候应用程序就不在前台了
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
   <item android:state_focused="true" android:drawable="@drawable/ic_down"></item>
   <item android:state_pressed="true" android:drawable="@drawable/ic_select"></item>
   <item android:state_selected="true" android:drawable="@drawable/ic_select"></item>
   <item android:drawable="@drawable/ic"></item>
</selector>

( ´◔‸◔`)    为毛一定要在最后加一个默认的状态?
每个item对应一个具体drawable,系统从上到下进行查找,直到匹配;所以,如果没找到就选择默认的,啊哈哈,留一手。

EditText下边框

<?xml version="1.0" encoding="utf-8"?>
<selector
  xmlns:android="http://schemas.android.com/apk/res/android">
    <item android:state_pressed="true" >
        <shape android:shape="rectangle">
            <corners android:radius="5dp" />
            <solid android:color="@color/deeppink" />
         </shape>
    </item>
    <item >
        <shape android:shape="rectangle">
            <corners android:radius="5dp" />
            <solid android:color="@color/pink" />
         </shape>
    </item>
</selector>

4.LevelListDrawable

管理一组drawable资源,通过调用setLevel()方法加载listvel-list或代码中定义的某个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/ic_on"
        android:maxLevel="10"
        android:minLevel="6">
    </item>
    <item
        android:drawable="@drawable/ic_off"
        android:maxLevel="20"
        android:minLevel="12">
    </item>
</level-list>

在代码中设置imageView.setImageLevel(8);

5.TransitionDrawable

为LayerDrawable子类,只管理两层,提供透明度变化,可以控制从一层过度到另一层的动画效果

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

在代码中


TransitionDrawable drawable = (TransitionDrawable)v.getDrawable();
drawable.startTransition(3000);

// ↓ 变身

TransitionDrawable drawable = (TransitionDrawable)v.getDrawable();
drawable.reverseTransition(3000); // /渐变时间3s

6.InsertDrawable

嵌入到另一个drawable,可以设置inset内边距

<?xml version="1.0" encoding="utf-8"?>
<inset xmlns:android="http://schemas.android.com/apk/res/android"
       android:drawable="@drawable/ic_avatar"
       android:inset="50dp">
</inset>

7.ClipDrawable

对drawable进行剪切,通过setLevel进行剪切,level从0-10000

裁剪方式android:clipOrientation="horizontal"

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

横向显示图片的一半(5000)

ClipDrawable drawable = (ClipDrawable)v.getDrawable();
drawable.setLevel(5000);

8.ShapeDrawable

android:shape="oval" 图片形状 rectangle(默认)、oval(椭圆)、line、ring
android:angle 渐变的角度 默认为0,值为45的倍数,0表示从左往右,90表示从下到上
corners 圆角角度 适用于矩形
solid 纯色填充
gradient 渐变效果 与solid相排斥
stroke 描边 android:width 宽度、android:color 颜色 、android:dashWidth 虚线线段宽度、android:dashGap 注册虚线之间的间距
size shape大小 size指定宽高后,ShapeDrawable就有固定宽/高
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
       android:shape="oval">
    <corners
        android:bottomLeftRadius="10dp"
        android:bottomRightRadius="10dp"
        android:radius="5dp"
        android:topLeftRadius="10dp"
        android:topRightRadius="10dp"/>
    <gradient
        android:angle="0"
        android:centerColor="#cccccc"
        android:centerX="100"
        android:centerY="20"
        android:endColor="#abcdef"
        android:gradientRadius="100dp"
        android:startColor="#000000"
        android:type="linear"
        android:useLevel="false"/>
    <solid android:color="#cccccc"/>
    <stroke
        android:width="1dp"
        android:color="#cccccc"
        android:dashGap="2dp"
        android:dashWidth="50dp"/>
</shape>

eg:文字加入边框

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

推荐阅读更多精彩内容