Android - Resource 之 Drawable小结
本篇直接选择性地翻译官方开发指南
=============================
Drawable有十种类型,如下
(1) - Bitmap file:这个简单,也可以用xml来更详细的定义,先按下不写。。。。
(2) - Nine-Patch File: 这个是一个自定义绽放区域的Bitmap,一般以.9.png为后缀,可能用 SDK/tools/draw9patch.bat来生成,实际上这个工具在图片外边缘添加一个一像素的边框用来标记“缩放区域”,如下

调整一下后,会得到这样的效果:

(3) - Layer List:一个多层级的Drawable,按顺序绘制到屏幕:
<?xml version="1.0" encoding="utf-8"?>
<layer-list xmlns:android="http://schemas.android.com/apk/res/android">
<item>
<bitmap android:src="@drawable/android_red"
android:gravity="center" />
</item>
<item android:top="10dp" android:left="10dp">
<bitmap android:src="@drawable/android_green"
android:gravity="center" />
</item>
<item android:top="20dp" android:left="20dp">
<bitmap android:src="@drawable/android_blue"
android:gravity="center" />
</item>
</layer-list>
效果如下:

(4) - State List: 就是根据该Drawable的状态来决定用哪个Drawable来显示,例如是一个Button有(pressed、focused、niether)各种状态。
例子如下:
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_pressed="true"
android:drawable="@drawable/button_pressed" /> <!-- pressed -->
<item android:state_focused="true"
android:drawable="@drawable/button_focused" /> <!-- focused -->
<item android:state_hovered="true"
android:drawable="@drawable/button_focused" /> <!-- hovered -->
<item android:drawable="@drawable/button_normal" /> <!-- default -->
</selector>
(5) - Level List: 在一个View中根据其level显示不同的drawable,创建一个LevelListDrawable。例子:
<level-list xmlns:android="http://schemas.android.com/apk/res/android">
<item android:maxLevel="4" android:drawable="@drawable/stat_sys_battery_0" />
<item android:maxLevel="14" android:drawable="@drawable/stat_sys_battery_10" />
<item android:maxLevel="29" android:drawable="@drawable/stat_sys_battery_20" />
<item android:maxLevel="49" android:drawable="@drawable/stat_sys_battery_40" />
<item android:maxLevel="69" android:drawable="@drawable/stat_sys_battery_60" />
<item android:maxLevel="89" android:drawable="@drawable/stat_sys_battery_80" />
<item android:maxLevel="100" android:drawable="@drawable/stat_sys_battery_100" />
</level-list>
然后在View中,把ImageView的src设置为该xml,调用:
imageView.getDrawable().setLevel(50);
android会自动选择对应的图片,显示剩余电量就是这样实现的。选择的规则是选maxLevel大于或等于setLevel传入参数的item.
(6) - Transition Drawable: 用于定义一个能够淡入淡出的效果,创建一个TransitionDrawable。例子:/drawable/transition.xml
<?xml version="1.0" encoding="utf-8"?>
<transition xmlns:android="http://schemas.android.com/apk/res/android" >
<item android:drawable="@drawable/ic_m_48"
android:top="20dp"
android:left="20dp"/>
<item android:drawable="@drawable/ic_2_h"/>
</transition>
在Layout中引用如下:
<ImageButton android:id="@+id/btn_3"
android:contentDescription="@string/content_description"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="@drawable/transition"
android:onClick="TransitionDrawableTest"/>
在回调函数中进行如下调用:
public void TransitionDrawableTest(View view){
ImageButton button = (ImageButton) findViewById(R.id.btn_3);
TransitionDrawable drawable = (TransitionDrawable) button.getDrawable();
drawable.startTransition(500);
}
(7)- Inset Drawable:嵌入某个Drawable,可以保留一定的边缘。当一个View的背景比View本身大小要小的时候,这个很好用。创建一个InsetDrawable。
例子很简单:
<?xml version="1.0" encoding="utf-8"?>
<inset xmlns:android="http://schemas.android.com/apk/res/android"
android:drawable="@drawable/background"
android:insetTop="10dp"
android:insetLeft="10dp" />
(8) - Clip Drawable:裁剪的Drawable,进度条时十分好用。
clip.xml如下:
<?xml version="1.0" encoding="utf-8"?>
<clip xmlns:android="http://schemas.android.com/apk/res/android"
android:drawable="@drawable/ic_2_h"
android:clipOrientation="horizontal"
android:gravity="left" />
activity里的ImageView与seelbar:
<SeekBar android:id="@+id/seelbar"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:max="10000"
android:progress="5000"
android:thumb="@drawable/ic_launcher"
/>
<ImageView android:id="@+id/image_clip"
android:background="@drawable/clip"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:contentDescription="@string/content_description"
/>
在activity的onCreate函数里为seekbar添加一个Listener:
SeekBar seekbar = (SeekBar)findViewById(R.id.seelbar);
seekbar.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener() {
@Override
public void onStopTrackingTouch(SeekBar seekBar) {
}
@Override
public void onStartTrackingTouch(SeekBar seekBar) {
}
@Override
public void onProgressChanged(SeekBar seekBar, int progress,
boolean fromUser) {
// TODO Auto-generated method stub
ClipDrawable drawable = (ClipDrawable) imageview.getBackground();
drawable.setLevel(progress);
}
});
效果:

(9)Scale Drawable:用于改变其他drawable大小的一个drawable(太拗口),指向一个ScaleDrawable。
<?xml version="1.0" encoding="utf-8"?>
<scale
xmlns:android="http://schemas.android.com/apk/res/android"
android:drawable="@drawable/drawable_resource"
android:scaleGravity=["top" | "bottom" | "left" | "right" | "center_vertical" |
"fill_vertical" | "center_horizontal" | "fill_horizontal" |
"center" | "fill" | "clip_vertical" | "clip_horizontal"]
android:scaleHeight="percentage"
android:scaleWidth="percentage"/>
例子:
<?xml version="1.0" encoding="utf-8"?>
<scale xmlns:android="http://schemas.android.com/apk/res/android"
android:drawable="@drawable/logo"
android:scaleGravity="center_vertical|center_horizontal"
android:scaleHeight="80%"
android:scaleWidth="80%" />
(10) - Shape 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="integer"
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>
Android - Resource 之 Drawable小结的更多相关文章
- Android - Resource 之 Menu 小结
定义一个application的菜单,由MenuInflater召唤. 位置: res/menu/filename.xml 类型:指向Menu resource 文法: <?xml versio ...
- Android - Resource 之 Layout 小结
Layout定义了一个Activity的UI框架,或者是一个UI的组件. 文法如下: ?xml version="1.0" encoding="utf-8"?& ...
- Android - Resource 之 String 小结
简单的string: <?xml version="1.0" encoding="utf-8"?> <resources> <st ...
- 十、Android学习第九天——小结(转)
(转自:http://wenku.baidu.com/view/af39b3164431b90d6c85c72f.html) 十.Android学习第九天——小结 通过这段时间的学习,今晚上来做个小小 ...
- Android中各种Drawable总结
在Android中,Drawable使用广泛,但是种类也多,基于<Android开发艺术探索>中对Drawable的讲解,总结了如下表格.
- Android横竖屏切换小结
Android横竖屏切换小结 (老样子,图片啥的详细文档,可以下载后观看 http://files.cnblogs.com/franksunny/635350788930000000.pdf) And ...
- Android中Bitmap, Drawable, Byte,ID之间的转化
Android中Bitmap, Drawable, Byte,ID之间的转化 1. Bitmap 转化为 byte ByteArrayOutputStream out = new ByteArray ...
- Android中的Drawable和动画
Android中Drawable是一种可以在Canvas上进行绘制抽象的概念,种类很多,常见的颜色和图片都可以是一个Drawable.Drawable有很多种,它们表示一种图像的概念,但是它们又不全是 ...
- Android resource compilation failed
报错:Android resource compilation failed D:\android\EasySports\app\build\intermediates\incremental\mer ...
随机推荐
- webpack打包报错
Invalid configuration object. Webpack has been initialised using a configuration object that does no ...
- itextsharp display:none无效的bug
在使用itextsharp实现 html 2 pdf时,发现display:none无效.如 <div style="display: none">应该隐藏</d ...
- 运行vs时打开一个浏览器窗口,而不是在原有窗口上打开一个标签
1.运行vs时打开一个浏览器窗口,而不是在原有窗口上打开一个标签,结束调试时窗口又关闭了,特别麻烦. 在用swagger调试接口时,好不容易输入了测试数据,然而窗口关闭了,再次调试又得重新输入. 解决 ...
- Day11 多进程与多线程编程
一.进程与线程 1.什么是进程(process)? An executing instance of a program is called a process. Each process provi ...
- CSS文本(Text)属性-----letter-spacing和text-align
letter-spacing letter-spacing:normal | <length> 指定字符之间的额外间隙 normal:默认间隔.计算值为0 <length&g ...
- centos中yum命令删除还原的补救方法介绍
前言 yum,是Yellow dog Updater Modified的简称,起初是由yellow dog这一发行版的开发者Terra Soft研发,用python写成,那时还叫做yup(yellow ...
- MySql 应用语句
[1]MySQL基础语句 -- 查询mysql版本号 SELECT VERSION(); -- 创建数据库 DROP DATABASE IF EXISTS study; -- 如果存在先删除 CREA ...
- python pandas 基础理解
其实每一篇博客我都要用很多琐碎的时间片段来学完写完,每次一点点,用到了就学一点,学一点就记录一点,要用上好几天甚至一两个礼拜才感觉某一小类的知识结构学的差不多了. Pandas 是基于 NumPy 的 ...
- SQL Server 配置管理器
- FTP搭建 共享上网 穿透内网外网
1.ftp原理介绍 FTP只通过TCP连接,没有用于FTP的UDP组件.FTP不同于其他服务的是它使用了两个端口, 一个数据端口和一个命令端口(或称为控制端口).通常21端口是命令端口,20端口是数据 ...