A drawable resource is a general concept for a graphic that can be drawn to the screen and which you can retrieve with APIs such as getDrawable(int)
or apply to another XML resource with attributes such as android:drawable
andandroid: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 three formats: .png
(preferred), .jpg
(acceptable), .gif
(discouraged).
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 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:
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.
Note: You can use a <bitmap>
element as a child of an <item>
element. For example, when creating astate 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:mipMap=["true" | "false"] 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:
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 Graphicsdocument.
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:
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:
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 neither) and, using a state list drawable, you can provide a different background image for each state.
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:
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 aandroid:maxLevel
value greater than or equal to the value passed to the method.
-
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:
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, callreverseTransition()
.
-
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"?> <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>
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:
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:
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.
-
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:
Scale Drawable
A drawable defined in XML that changes the size of another drawable based on its current 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:
Shape Drawable
This is a generic shape defined in 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:
-
可绘制资源是可以被绘制到屏幕上,哪些是你可以用的API,如检索图形的一般概念
getDrawable(INT)
或应用到另一个XML资源与属性,如机器人:绘制
和安卓图标
。有几种不同类型的可绘制的:-
位图文件
-
位图图形文件(
巴纽
,JPG格式
,或.gif注意
)。创建一个BitmapDrawable
。
九宫格文件
-
与伸缩区域的PNG文件,使图像大小调整基于内容(
.9.png
)。创建一个NinePatchDrawable
。
层列表
-
被拉伸,管理等可绘制的数组。这些绘制在阵列顺序,因此具有最大索引的元件在上面绘制。创建一个
LayerDrawable
。
国家名单
-
引用不同的位图图形的不同状态的XML文件(例如,使用不同的图像时,按下一个按钮)。创建一个
StateListDrawable
。
级别列表
-
定义用于管理若干备用可绘的可拉伸的XML文件,每个分配的最大数值。创建一个
LevelListDrawable
。
过渡绘制对象
-
定义可以淡入淡出2绘图资源之间绘制的XML文件。创建一个
TransitionDrawable
。
插图绘制对象
- 它定义了镶石按指定距离的另一绘制的可绘制的XML文件。当需要查看背景drawble比视图的实际范围较小,这非常有用。 剪辑绘制对象
-
它定义了一个绘制一个XML文件,该文件夹在此基础上绘制对象的当前电平值另一个可绘制。创建一个
ClipDrawable
。
规模可绘制
-
定义改变另一个可绘制的是根据它的电流电平值的大小被拉伸的XML文件。创建一个
ScaleDrawable
形状绘制对象
-
限定了几何形状,其中包括颜色和渐变的XML文件。创建一个
ShapeDrawable
。
也看到了动画资源如何创建一个文档
AnimationDrawable
。注:一个颜色资源,也可以作为XML的绘制。例如,在创建时状态列表绘制,可以参考的颜色资源
的android:可绘制
属性(机器人:可绘制=“@色/绿”
)。位图
位图图像。Android支持三种格式的位图文件:
png格式
(首选),.JPG
(接受),.gif注意
(灰心)。您可以直接引用的位图文件,使用文件名作为资源ID,或创建XML别名资源ID。
注:位图文件可能由无损图像压缩自动优化
AAPT
在生成过程中的工具。例如,一个真彩色的PNG,不需要超过256种颜色可以用颜色调色板转换成一个8位的PNG。这将导致相等质量的图像,但它需要较少的存储器英寸所以,要知道,放在这个目录下的图像二进制文件可以在生成过程中发生改变。如果你计划,以将其转换为位图读取图像作为比特流,把你的照片在RES /生/
文件夹而不是,在那里他们将不会被优化。位图文件
位图文件是一个
。PNG
,JPG格式
,或.gif注意
文件。Android的创建一个可绘制
了这些文件,当您将它们保存在资源RES /绘制/
目录下。-
文件位置:
-
RES /绘制/ 文件名 巴纽
(巴纽
,JPG格式
,或.gif注意
)
的文件名 作为资源ID。
编译的资源数据类型:
-
资源指针
BitmapDrawable
。
资源引用:
-
在Java:
。R.drawable 名
在XML:@ [ 包:]绘制/ 文件名
例:
-
随着在保存的图像
RES /绘制/ myimage.png
,这种布局XML应用图像视图:<ImageView android:layout_height = "wrap_content" android:layout_width = "wrap_content" android:src = "@drawable/myimage" />
下面的应用程序代码检索图像作为
绘制对象
:资源RES =
getResources ()
; 可绘制可绘制= 资源。getDrawable
(ř 。绘制。MYIMAGE );
也可以看看:
XML位图
一个XML位图是XML定义的资源指向位图文件。效果为原始位图文件的别名。该XML可以指定位图的附加属性,如抖动和瓷砖。
注意:您可以使用
<位>
元素作为一个孩子的<item>
元素。例如,在创建时的状态列表或图层列表,你可以排除的android:绘制
从属性的<item>
元素和嵌套<位图>
里面定义绘制项目。-
文件位置:
-
RES /绘制/ 文件名 的.xml
文件名 作为资源ID。
编译的资源数据类型:
-
资源指针
BitmapDrawable
。
资源引用:
-
在Java:
。R.drawable 名
在XML:@ [ 包:]绘制/ 文件名
句法:
-
<?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:mipMap = ["true" | "false" ] android:tileMode = ["disabled" | "clamp" | "repeat" | "mirror" ] />
内容:
- 例:
-
<?xml的version = "1.0" encoding = "utf-8" ?> <bitmap xmlns:android = "http://schemas.android.com/apk/res/android" android:src = "@drawable/icon" android:tileMode = "repeat" />
也可以看看:
九宫格
一个
NinePatch
是一个PNG图像,其中您可以定义当视图中含量超过正常范围的图像缩放的Android伸缩区域。您通常指定此类型的图像为具有至少一个尺寸设定为观的背景“WRAP_CONTENT”
,并且当在视图增长以适应内容,九宫图像也缩放以匹配视图的大小。一个例子利用九宫图像是Android的标准中使用的背景按钮
小部件,它必须伸展以适应按钮中的文本(或图像)。同与正常的位图,你可以直接或通过XML定义的资源引用九宫文件。
有关如何创建具有拉伸区域的九宫文件的完整讨论,请参阅2D图形 文件。
九宫格文件
-
文件位置:
-
水库/抽拉/ 文件名 .9.png
的文件名 被用作资源ID。
编译的资源数据类型:
-
资源指针
NinePatchDrawable
。
资源引用:
-
在Java:
。R.drawable 名
在XML:@ [ 包:]绘制/ 文件名
例:
-
随着在保存的图像
RES /绘制/ myninepatch.9.png
,这种布局XML应用九宫到View:<Button android:layout_height = "wrap_content" android:layout_width = "wrap_content" android:background = "@drawable/myninepatch" />
也可以看看:
XML九宫格
一个XML九宫格是XML指向一个九宫文件中定义的资源。该XML可以指定抖动的图像。
-
文件位置:
-
RES /绘制/ 文件名 的.xml
文件名 作为资源ID。
编译的资源数据类型:
-
资源指针
NinePatchDrawable
。
资源引用:
-
在Java:
。R.drawable 名
在XML:@ [ 包:]绘制/ 文件名
句法:
-
<?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" ] />
内容:
- 例:
-
<?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" />
层列表
一个
LayerDrawable
是管理其他可绘制阵列的可绘制对象。列表中的每个可绘在列表中的列表的最后一个可拉伸的顺序绘制绘制在顶部。每个绘制由一个代表
的<item>
一个元素中<层列表>
元素。-
文件位置:
-
RES /绘制/ 文件名 的.xml
文件名 作为资源ID。
编译的资源数据类型:
-
资源指针
LayerDrawable
。
资源引用:
-
在Java:
。R.drawable 名
在XML:@ [ 包:]绘制/ 文件名
句法:
-
<?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>
内容:
- 例:
-
在保存XML文件
RES /绘制/ 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>
请注意,此示例使用嵌套的
<位图>
元素与“中心”重力定义每个项目的绘制资源。这确保了没有图像被缩放到适合容器的大小,由于由偏移的图像调整大小而引起的。这种布局XML应用绘制到View:
<ImageView android:layout_height = "wrap_content" android:layout_width = "wrap_content" android:src = "@drawable/layers" />
结果是日益偏移图像的堆栈:
也可以看看:
国家名单
一个
StateListDrawable
是使用几种不同的图像来表示相同的图形,根据对象的状态中的XML定义的可绘制的对象。例如,一个按钮
控件可以在几种不同的状态(压,突出重点,或两者都不是),并使用状态列表绘制的一个存在,你可以为每个国家不同的背景图片。你可以这样描述XML文件的状态列表。每个图形由一个代表
的<item>
一个元素中<选择>
元素。每个的<item>
使用不同的属性来描述,其中它应该被用来作为图形为可绘制的状态。在每个状态变化,状态列表遍历从上到下,符合当前的状态下使用,在选择的第一个项目不是基于“最佳匹配”,但仅仅是符合国家的最低标准的第一个项目。
-
文件位置:
-
RES /绘制/ 文件名 的.xml
文件名 作为资源ID。
编译的资源数据类型:
-
资源指针
StateListDrawable
。
资源引用:
-
在Java:
。R.drawable 名
在XML:@ [ 包:]绘制/ 文件名
句法:
-
<?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>
内容:
- 例:
-
在保存XML文件
RES /绘制/ button.xml
:<?xml的 按- > <项目 的android:state_focused = “真正的” 机器人:可绘制= “@绘制/ button_focused” /> < -集中- >! <项目 的android:state_hovered = “真正的” 机器人:可绘制= “@绘制/ button_focused“ /> < -徘徊- >! <项目 机器人:可绘制= ”@绘制/ button_normal“ /> <! -默认- > </选择>
这种布局XML应用状态列表绘制到按钮:
<Button android:layout_height = "wrap_content" android:layout_width = "wrap_content" android:background = "@drawable/button" />
也可以看看:
级别列表
一个管理若干备用可绘制的绘制对象,每人分到的最大数值。设置与绘制的电平值
执行setLevel()
加载在具有水平列表绘制资源的android:maxLevel
值大于或等于传递给方法的值。-
文件位置:
-
RES /绘制/ 文件名 的.xml
文件名 作为资源ID。
编译的资源数据类型:
-
资源指针
LevelListDrawable
。
资源引用:
-
在Java:
。R.drawable 名
在XML:@ [ 包:]绘制/ 文件名
句法:
-
<?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>
内容:
- 例:
-
<?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>
一旦这个被施加到一个
视图
,该电平可以被改变执行setLevel()
或setImageLevel()
。
也可以看看:
过渡绘制对象
一个
TransitionDrawable
是绘制对象,可以淡入淡出两个可绘制资源之间。每个绘制由一个代表
的<item>
单一元素中的<transition>
元素。没有两个以上的项目支持。过渡转接,呼叫startTransition()
。为了向后过渡,调用reverseTransition()
。-
文件位置:
-
RES /绘制/ 文件名 的.xml
文件名 作为资源ID。
编译的资源数据类型:
-
资源指针
TransitionDrawable
。
资源引用:
-
在Java:
。R.drawable 名
在XML:@ [ 包:]绘制/ 文件名
句法:
-
<?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>
内容:
- 例:
-
在保存XML文件
RES /绘制/ 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>
这种布局XML应用绘制到View:
<ImageButton android:id = "@+id/button" android:layout_height = "wrap_content" android:layout_width = "wrap_content" android:src = "@drawable/transition" />
而下面的代码执行从第一项到第二500ms的转变:
ImageButton button = ( ImageButton ) findViewById ( R . id . button ); TransitionDrawable drawable = ( TransitionDrawable ) button . getDrawable (); drawable . startTransition ( 500 );
也可以看看:
插图绘制对象
在XML的镶石按指定距离的另一绘制定义的绘制。当一个视图需要一个背景是不是查看实际边界较小,这非常有用。
-
文件位置:
-
RES /绘制/ 文件名 的.xml
文件名 作为资源ID。
编译的资源数据类型:
-
资源指针
InsetDrawable
。
资源引用:
-
在Java:
。R.drawable 名
在XML:@ [ 包:]绘制/ 文件名
句法:
-
<?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 " />
内容:
- 例:
-
<?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" />
也可以看看:
剪辑绘制对象
在XML中定义的可绘制的剪辑另一个可绘制在此基础上绘制对象的当前水平。可以控制多少子抽拉得到在宽度和高度根据该电平,以及一个重力来控制在其被放置在其整体容器裁剪。最常用的实现之类的进度条。
-
文件位置:
-
RES /绘制/ 文件名 的.xml
文件名 作为资源ID。
编译的资源数据类型:
-
资源指针
ClipDrawable
。
资源引用:
-
在Java:
。R.drawable 名
在XML:@ [ 包:]绘制/ 文件名
句法:
-
<?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" ] />
内容:
- 例:
-
在保存XML文件
RES /绘制/ 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" />
下面的布局XML应用剪辑绘制一个观点:
<ImageView android:id = "@+id/image" android:background = "@drawable/clip" android:layout_height = "wrap_content" android:layout_width = "wrap_content" />
以下代码获取被拉伸并增加以便剪裁逐步显示图像的量:
ImageView imageview = ( ImageView ) findViewById ( R . id . image ); ClipDrawable drawable = ( ClipDrawable ) imageview . getDrawable (); drawable . setLevel ( drawable . getLevel () + 1000 );
增加水平降低削波的量和慢慢揭示了图像。这是在7000的水平:
注:默认级别为0,这是完全修剪,因此图像是不可见的。当电平是10,000时,图像不裁剪和完全可见。
也可以看看:
规模可绘制
在XML改变基于当前水平的另一个绘制的大小定义的绘制。
-
文件位置:
-
RES /绘制/ 文件名 的.xml
文件名 作为资源ID。
编译的资源数据类型:
-
资源指针
ScaleDrawable
。
资源引用:
-
在Java:
。R.drawable 名
在XML:@ [ 包:]绘制/ 文件名
句法:
-
<?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%" />
也可以看看:
形状绘制对象
这是在XML中定义的通用形状。
-
文件位置:
-
RES /绘制/ 文件名 的.xml
文件名 作为资源ID。
编译的资源数据类型:
-
资源指针
GradientDrawable
。
资源引用:
-
在Java:
。R.drawable 名
在XML:@ [ 包:]绘制/ 文件名
句法:
-
<?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>
内容:
- 例:
-
在保存XML文件
RES /绘制/ 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>
这种布局XML应用形状绘制到View:
<TextView android:background = "@drawable/gradient_box" android:layout_height = "wrap_content" android:layout_width = "wrap_content" />
此应用程序代码获取形状绘制,并将其应用于一个观点:
Resources res =
getResources ()
; Drawable shape = res .getDrawable
( R . drawable . gradient_box ); TextView tv = ( TextView ) findViewByID ( R . id . textview ); tv . setBackground ( shape );
也可以看看:
-
位图图形文件(