android Drawable资源讲解


Bitmap

android支持3种格式的图片,png、jpg和gif。png为推荐格式。

XML Bitmap:

XML的bitmap也是一种资源文件。如:

<bitmap

android:antialias="" 平滑,是否要锯齿 true|false

android:dither="" 抖动 true|false

android:filter="" 过滤 true|false

android:gravity="" 这个不用说了

android:src="" 可是是一个drawable资源,如图片、颜色等

android:tileMode="" 平铺方式 disable[失效。默认选项]|clamp[固定]|repeat[平铺]|mirror[反射]

>

</bitmap>

Nine-Patch

9图。这个常用了,可以对视图进行拉伸。

XML Nine-Patch

这个比较奇怪,我一直用都是直接用图片的就9图。

Layer List

LayerDrawable是一个管理drawable数组的对象。每一个drawable对象都会又第一个画起,最后那个在画布的顶端。

<?xml version="1.0" encoding="utf-8"?>

<layer-list

xmlns:android="http://schemas.android.com/apk/res/android" >

<item

android:drawable="@[package:]drawable/drawable_resource"

android:id="@[+][package:]id/resource_name"

android:top="dimension" 就是向顶端平移多少

android:right="dimension"

android:bottom="dimension"

android:left="dimension" />

</layer-list>


State List

官方文档的解析:

A StateListDrawable is a drawable object defined in XML that uses a several different images to represent the same graphic, depending on the state of the object. For example, a Button widget can exist in one of several different states (pressed, focused, or niether) and, using a state list drawable, you can provide a different background image for each state.

其实就是selector.

Level List

andoird的Drawable有个方法是setLevel,可以通过这个级别来用LevelListDrawable来选择不同的Drawable.

<?xml version="1.0" encoding="utf-8"?>

<level-list

xmlns:android="http://schemas.android.com/apk/res/android" >

<item

android:drawable="@drawable/drawable_resource"

android:maxLevel="integer"

android:minLevel="integer" />

</level-list>


用法:

view.getBackground().setLevel(X);

或 ImageView.setImageLevel(X);

Transition Drawable

A TransitionDrawable is a drawable object that can cross-fade between the two drawable resources.

TransitionDrawable对象是一个可以使2个drawable对象淡入淡出的drawable对象。

<?xml version="1.0" encoding="utf-8"?>

<transition

xmlns:android="http://schemas.android.com/apk/res/android" >

<item

android:drawable="@[package:]drawable/drawable_resource"

android:id="@[+][package:]id/resource_name"

android:top="dimension"

android:right="dimension"

android:bottom="dimension"

android:left="dimension" />

</transition>


可以在transition嵌套2个drawable,调用startTransition()和reverseTransition()可以去激活。

Inset Drawable

A drawable defined in XML that insets another drawable by a specified distance. This is useful when a View needs a background that is smaller than the View's actual bounds.

这个适用于背景图片比边框要小的情况。

Clip Drawable

这个是用于截取drawable对象的。

<?xml version="1.0" encoding="utf-8"?>

<clip

xmlns:android="http://schemas.android.com/apk/res/android"

android:drawable="@drawable/drawable_resource"

android:clipOrientation=["horizontal" | "vertical"]

android:gravity=["top" | "bottom" | "left" | "right" | "center_vertical" |

"fill_vertical" | "center_horizontal" | "fill_horizontal" |

"center" | "fill" | "clip_vertical" | "clip_horizontal"] />

通过setLevel去调整截取的区域。

网上找到一个很好的用法:

http://www.ssc.stn.sh.cn/html/zjbk/2012-8/7459.html

也可以用于我们平时看到别人加载图片时那种效果。不停地变截取的部分就可以了。

Scale Drawable

A drawable defined in XML that changes the size of another drawable based on its current level.

一个可以基于现有的级别改变另一个drawable的XML Deawable对象...好唔顺....

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

Shape Drawable

这个比较强大,可以i很多形状,不用图片,省空间。

<?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" 指定4个角,其它为单独指定

android:topLeftRadius="integer"

android:topRightRadius="integer"

android:bottomLeftRadius="integer"

android:bottomRightRadius="integer" />

<gradient 设置颜色渐变,

android:angle="integer" 角度设置渐变的角度,数值必须为45的倍数,默认为0,即从左到右渐变。当值为90时,从下到上渐变,以此类推当值为180时 为270时从上向下渐变。

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>

posted @ 2013-03-17 14:56  shermannnn  阅读(1179)  评论(0编辑  收藏  举报