Android 开发指南 翻译11 Application Resources -- Drawable Resources
来源:互联网 发布:类似clover的软件 编辑:程序博客网 时间:2024/04/29 21:50
Application Resources -- Drawable Resources
getDrawable(int)
or apply to another XML resource with attributes such as android:drawable
and android:icon
. There are several different types of drawables:- Bitmap File
- A bitmap graphic file (
.png
,.jpg
, or.gif
). Creates aBitmapDrawable
. - Nine-Patch File
- A PNG file with stretchable regions to allow image resizing based on content (
.9.png
). Creates aNinePatchDrawable
. - Layer List
- A Drawable that manages an array of other Drawables. These are drawn in array order, so the element with the largest index is be drawn on top. Creates a
LayerDrawable
. - State List
- An XML file that references different bitmap graphics for different states (for example, to use a different image when a button is pressed). Creates a
StateListDrawable
. - Level List
- An XML file that defines a drawable that manages a number of alternate Drawables, each assigned a maximum numerical value. Creates a
LevelListDrawable
. - Transition Drawable
- An XML file that defines a drawable that can cross-fade between two drawable resources. Creates a
TransitionDrawable
. - Inset Drawable
- An XML file that defines a drawable that insets another drawable by a specified distance. This is useful when a View needs a background drawble that is smaller than the View's actual bounds.
- Clip Drawable
- An XML file that defines a drawable that clips another Drawable based on this Drawable's current level value. Creates a
ClipDrawable
. - Scale Drawable
- An XML file that defines a drawable that changes the size of another Drawable based on its current level value. Creates a
ScaleDrawable
- Shape Drawable
- An XML file that defines a geometric shape, including colors and gradients. Creates a
ShapeDrawable
.
Also see the Animation Resource document for how to create an AnimationDrawable
.
Note: A color resource can also be used as a drawable in XML. For example, when creating a state list drawable, you can reference a color resource for the android:drawable
attribute (android:drawable="@color/green"
).
Bitmap
A bitmap image. Android supports bitmap files in a three formats: .png
(preferred), .jpg
(acceptable), .gif
(discouraged).
位图,Android支持三种格式的位图:.png(首选),.jpg(可接受),.gif(不鼓励).
You can reference a bitmap file directly, using the filename as the resource ID, or create an alias resource ID in XML.
Note: Bitmap files may be automatically optimized with lossless image compression by the aapt
tool during the build process. For example, a true-color PNG that does not require more than 256 colors may be converted to an 8-bit PNG with a color palette. This will result in an image of equal quality but which requires less memory. So be aware that the image binaries placed in this directory can change during the build. If you plan on reading an image as a bit stream in order to convert it to a bitmap, put your images in the res/raw/
folder instead, where they will not be optimized.
在编译时,Bitmap文件会被aapt工具无损压缩。
Bitmap File
A bitmap file is a .png
, .jpg
, or .gif
file. Android creates a Drawable
resource for any of these files when you save them in the res/drawable/
directory.
- file location:
res/drawable/filename.png
(.png
,.jpg
, or.gif
)
The filename is used as the resource ID.- compiled resource datatype:
- Resource pointer to a
BitmapDrawable
. - resource reference:
- In Java:
R.drawable.filename
In XML:@[package:]drawable/filename
- example:
- With an image saved at
res/drawable/myimage.png
, this layout XML applies the image to a View:<ImageView android:layout_height="wrap_content" android:layout_width="wrap_content" android:src="@drawable/myimage" />
The following application code retrieves the image as a
Drawable
:Resources res =
getResources()
;Drawable drawable = res.getDrawable
(R.drawable.myimage); - see also:
- 2D Graphics
BitmapDrawable
XML Bitmap
An XML bitmap is a resource defined in XML that points to a bitmap file. The effect is an alias for a raw bitmap file. The XML can specify additional properties for the bitmap such as dithering and tiling.
XML位图是定义在XML文件中指向位图文件的资源,是原位图的别名。XML可以指定附加属性如:抖动和马赛克。
Note: You can use a <bitmap>
element as a child of an <item>
element. For example, when creating a state list or layer list, you can exclude the android:drawable
attribute from an <item>
element and nest a <bitmap>
inside it that defines the drawable item.
- file location:
res/drawable/filename.xml
The filename is used as the resource ID.- compiled resource datatype:
- Resource pointer to a
BitmapDrawable
. - resource reference:
- In Java:
R.drawable.filename
In XML:@[package:]drawable/filename
- syntax:
<?xml version="1.0" encoding="utf-8"?><bitmap xmlns:android="http://schemas.android.com/apk/res/android" android:src="@[package:]drawable/drawable_resource" android:antialias=["true" | "false"] android:dither=["true" | "false"] android:filter=["true" | "false"] android:gravity=["top" | "bottom" | "left" | "right" | "center_vertical" | "fill_vertical" | "center_horizontal" | "fill_horizontal" | "center" | "fill" | "clip_vertical" | "clip_horizontal"] android:tileMode=["disabled" | "clamp" | "repeat" | "mirror"] />
- elements:
- example:
<?xml version="1.0" encoding="utf-8"?><bitmap xmlns:android="http://schemas.android.com/apk/res/android" android:src="@drawable/icon" android:tileMode="repeat" />
- see also:
BitmapDrawable
- Creating alias resources
Nine-Patch
A NinePatch
is a PNG image in which you can define stretchable regions that Android scales when content within the View exceeds the normal image bounds. You typically assign this type of image as the background of a View that has at least one dimension set to "wrap_content"
, and when the View grows to accomodate the content, the Nine-Patch image is also scaled to match the size of the View. An example use of a Nine-Patch image is the background used by Android's standard Button
widget, which must stretch to accommodate the text (or image) inside the button.
Same as with a normal bitmap, you can reference a Nine-Patch file directly or from a resource defined by XML.
For a complete discussion about how to create a Nine-Patch file with stretchable regions, see the 2D Graphics document.
Nine-Patch File
- file location:
res/drawable/filename.9.png
The filename is used as the resource ID.- compiled resource datatype:
- Resource pointer to a
NinePatchDrawable
. - resource reference:
- In Java:
R.drawable.filename
In XML:@[package:]drawable/filename
- example:
- With an image saved at
res/drawable/myninepatch.9.png
, this layout XML applies the Nine-Patch to a View:<Button android:layout_height="wrap_content" android:layout_width="wrap_content" android:background="@drawable/myninepatch" />
- see also:
- 2D Graphics
NinePatchDrawable
XML Nine-Patch
An XML Nine-Patch is a resource defined in XML that points to a Nine-Patch file. The XML can specify dithering for the image.
- file location:
res/drawable/filename.xml
The filename is used as the resource ID.- compiled resource datatype:
- Resource pointer to a
NinePatchDrawable
. - resource reference:
- In Java:
R.drawable.filename
In XML:@[package:]drawable/filename
- syntax:
<?xml version="1.0" encoding="utf-8"?><nine-patch xmlns:android="http://schemas.android.com/apk/res/android" android:src="@[package:]drawable/drawable_resource" android:dither=["true" | "false"] />
- elements:
- example:
<?xml version="1.0" encoding="utf-8"?><nine-patch xmlns:android="http://schemas.android.com/apk/res/android" android:src="@drawable/myninepatch" android:dither="false" />
Layer List 层列表
A LayerDrawable
is a drawable object that manages an array of other drawables. Each drawable in the list is drawn in the order of the list—the last drawable in the list is drawn on top.
一个绘图层是一组其他图像的管理对象,在列表中的每个图像按照在列表中的顺序绘制--最后的画在最上面。
Each drawable is represented by an <item>
element inside a single <layer-list>
element.
- file location:
res/drawable/filename.xml
The filename is used as the resource ID.- compiled resource datatype:
- Resource pointer to a
LayerDrawable
. - resource reference:
- In Java:
R.drawable.filename
In XML:@[package:]drawable/filename
- syntax:
<?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>
- elements:
- example:
- XML file saved at
res/drawable/layers.xml
:<?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>
Notice that this example uses a nested
<bitmap>
element to define the drawable resource for each item with a "center" gravity. This ensures that none of the images are scaled to fit the size of the container, due to resizing caused by the offset images.This layout XML applies the drawable to a View:
<ImageView android:layout_height="wrap_content" android:layout_width="wrap_content" android:src="@drawable/layers" />
The result is a stack of increasingly offset images:
- see also:
LayerDrawable
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.
状态列表在xml中定义几个不同的图片,代表对象的不同状态。例如:按钮存在几个不同的状态(pressed,focused或都不是),你可以为它提供不同的背景图片。
You can describe the state list in an XML file. Each graphic is represented by an <item>
element inside a single <selector>
element. Each <item>
uses various attributes to describe the state in which it should be used as the graphic for the drawable.
During each state change, the state list is traversed top to bottom and the first item that matches the current state is used—the selection is not based on the "best match," but simply the first item that meets the minimum criteria of the state.
当状态改变时,状态列表从上到下扫描,第一个匹配当前状态的图片被使用。
- file location:
res/drawable/filename.xml
The filename is used as the resource ID.- compiled resource datatype:
- Resource pointer to a
StateListDrawable
. - resource reference:
- In Java:
R.drawable.filename
In XML:@[package:]drawable/filename
- syntax:
<?xml version="1.0" encoding="utf-8"?><selector xmlns:android="http://schemas.android.com/apk/res/android" android:constantSize=["true" | "false"] android:dither=["true" | "false"] 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_checked=["true" | "false"] android:state_enabled=["true" | "false"] android:state_activated=["true" | "false"] android:state_window_focused=["true" | "false"] /></selector>
- elements:
- example:
- XML file saved at
res/drawable/button.xml
:<?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>
This layout XML applies the state list drawable to a Button:
<Button android:layout_height="wrap_content" android:layout_width="wrap_content" android:background="@drawable/button" />
- see also:
StateListDrawable
Level List 级别列表
A Drawable that manages a number of alternate Drawables, each assigned a maximum numerical value. Setting the level value of the drawable with setLevel()
loads the drawable resource in the level list that has a android:maxLevel
value greater than or equal to the value passed to the method.
可以管理一下可选绘画,每个分配一个最大数字,可以通过setLevel()获取绘画资源。
- file location:
res/drawable/filename.xml
The filename is used as the resource ID.- compiled resource datatype:
- Resource pointer to a
LevelListDrawable
. - resource reference:
- In Java:
R.drawable.filename
In XML:@[package:]drawable/filename
- syntax:
<?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>
- elements:
- example:
<?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" /></level-list>
Once this is applied to a
View
, the level can be changed withsetLevel()
orsetImageLevel()
.- see also:
LevelListDrawable
Transition Drawable 转换绘画
A TransitionDrawable
is a drawable object that can cross-fade between the two drawable resources.
Each drawable is represented by an <item>
element inside a single <transition>
element. No more than two items are supported. To transition forward, call startTransition()
. To transition backward, call reverseTransition()
.
转换绘画可以在2个图像间淡入淡出,只支持2个图像。
- file location:
res/drawable/filename.xml
The filename is used as the resource ID.- compiled resource datatype:
- Resource pointer to a
TransitionDrawable
. - resource reference:
- In Java:
R.drawable.filename
In XML:@[package:]drawable/filename
- syntax:
<?xml version="1.0" encoding="utf-8"?><transitionxmlns: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>
- elements:
- example:
- XML file saved at
res/drawable/transition.xml
:<?xml version="1.0" encoding="utf-8"?><transition xmlns:android="http://schemas.android.com/apk/res/android"> <item android:drawable="@drawable/on" /> <item android:drawable="@drawable/off" /></transition>
This layout XML applies the drawable to a View:
<ImageButton android:id="@+id/button" android:layout_height="wrap_content" android:layout_width="wrap_content" android:src="@drawable/transition" />
And the following code performs a 500ms transition from the first item to the second:
ImageButton button = (ImageButton) findViewById(R.id.button);TransitionDrawable drawable = (TransitionDrawable) button.getDrawable();drawable.startTransition(500);
- see also:
TransitionDrawable
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.
- file location:
res/drawable/filename.xml
The filename is used as the resource ID.- compiled resource datatype:
- Resource pointer to a
InsetDrawable
. - resource reference:
- In Java:
R.drawable.filename
In XML:@[package:]drawable/filename
- syntax:
<?xml version="1.0" encoding="utf-8"?><inset xmlns:android="http://schemas.android.com/apk/res/android" android:drawable="@drawable/drawable_resource" android:insetTop="dimension" android:insetRight="dimension" android:insetBottom="dimension" android:insetLeft="dimension" />
- elements:
- example:
<?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" />
- see also:
InsetDrawable
Clip Drawable 剪切图像
A drawable defined in XML that clips another drawable based on this Drawable's current level. You can control how much the child drawable gets clipped in width and height based on the level, as well as a gravity to control where it is placed in its overall container. Most often used to implement things like progress bars.
在xml中定义的剪切图像,依据当前的level剪切图像。最常用在像进度条的对象上。
- file location:
res/drawable/filename.xml
The filename is used as the resource ID.- compiled resource datatype:
- Resource pointer to a
ClipDrawable
. - resource reference:
- In Java:
R.drawable.filename
In XML:@[package:]drawable/filename
- syntax:
<?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"] />
- elements:
- example:
- XML file saved at
res/drawable/clip.xml
:<?xml version="1.0" encoding="utf-8"?><clip xmlns:android="http://schemas.android.com/apk/res/android" android:drawable="@drawable/android" android:clipOrientation="horizontal" android:gravity="left" />
The following layout XML applies the clip drawable to a View:
<ImageView android:id="@+id/image" android:background="@drawable/clip" android:layout_height="wrap_content" android:layout_width="wrap_content" />
The following code gets the drawable and increases the amount of clipping in order to progressively reveal the image:
ImageView imageview = (ImageView) findViewById(R.id.image);ClipDrawable drawable = (ClipDrawable) imageview.getDrawable();drawable.setLevel(drawable.getLevel() + 1000);
Increasing the level reduces the amount of clipping and slowly reveals the image. Here it is at a level of 7000:
Note: The default level is 0, which is fully clipped so the image is not visible. When the level is 10,000, the image is not clipped and completely visible.
- see also:
ClipDrawable
Scale Drawable 缩放图像
A drawable defined in XML that changes the size of another drawable based on its current level.
可以根据level改变图像大小。
- file location:
res/drawable/filename.xml
The filename is used as the resource ID.- compiled resource datatype:
- Resource pointer to a
ScaleDrawable
. - resource reference:
- In Java:
R.drawable.filename
In XML:@[package:]drawable/filename
- syntax:
<?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" />
- elements:
- example:
<?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%" />
- see also:
ScaleDrawable
Shape Drawable 形状图像
This is a generic shape defined in XML.
在xml中定义形状。
- file location:
res/drawable/filename.xml
The filename is used as the resource ID.- compiled resource datatype:
- Resource pointer to a
GradientDrawable
. - resource reference:
- In Java:
R.drawable.filename
In XML:@[package:]drawable/filename
- syntax:
<?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>
- elements:
- example:
- XML file saved at
res/drawable/gradient_box.xml
:<?xml version="1.0" encoding="utf-8"?><shape xmlns:android="http://schemas.android.com/apk/res/android" android:shape="rectangle"> <gradient android:startColor="#FFFF0000" android:endColor="#80FF00FF" android:angle="45"/> <padding android:left="7dp" android:top="7dp" android:right="7dp" android:bottom="7dp" /> <corners android:radius="8dp" /></shape>
This layout XML applies the shape drawable to a View:
<TextView android:background="@drawable/gradient_box" android:layout_height="wrap_content" android:layout_width="wrap_content" />
This application code gets the shape drawable and applies it to a View:
Resources res =
getResources()
;Drawable shape = res.getDrawable
(R.drawable.gradient_box);TextView tv = (TextView)findViewByID(R.id.textview);tv.setBackground(shape); - see also:
ShapeDrawable
- Android 开发指南 翻译11 Application Resources -- Drawable Resources
- Android 开发指南 翻译11 Application Resources -- String Resources
- Android 开发指南 翻译4 Application Resources
- Android 开发指南 翻译5 Application Resources: Providing Resources
- Android 开发指南 翻译8 Application Resources: Localization (未完......)
- Android 开发指南 翻译6 Accessing Resources
- Android 开发指南 翻译7 Application Resources: Handling Runtime Changes(未完...)
- 【翻译】Drawable Resources
- Android Shape Drawable Resources
- Android Shape Drawable Resources
- android-Drawable Resources
- Android: Drawable Resources
- Drawable Resources
- Drawable Resources
- Drawable Resources
- Drawable Resources
- Drawable Resources
- Drawable Resources
- 引用头文件的<>与""
- 系统路径问题
- 宏
- 【redhat5.5】软件安装卸载方法
- 位运算
- Android 开发指南 翻译11 Application Resources -- Drawable Resources
- NHibernate Restrictions用法
- QTimer和Qthread的调度时间精度
- 1002
- IPHONE应用开发 - 标签栏
- 变量取值范围
- IP/ARP/ICMP/UDP 一些原理
- Android架构详解
- iPhone开发学习笔记006—— NSNotification自定义通知名