Drawable Resources
2016-04-05 12:36
706 查看
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
apply to another XML resource with attributes such as
Bitmap FileA bitmap graphic file (
Also see the Animation Resource document for
how to create an
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
(
A bitmap image. Android supports bitmap files in three formats:
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
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
A bitmap file is a
for any of these files when you save them in the
FILE LOCATION:
The filename is used as the resource ID.COMPILED RESOURCE DATATYPE:Resource pointer to a
In XML:
The following application code retrieves the image as a
Drawable drawable = res.
SEE ALSO:
2D Graphics
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
list or layer list, you can exclude the
from an
FILE LOCATION:
The filename is used as the resource ID.COMPILED RESOURCE DATATYPE:Resource pointer to a
In XML:
ELEMENTS:
attributes:
when the
Must be one or more (separated by '|') of the following constant values:
more information. Default value is false.
Must be one of the following constant values:
EXAMPLE:
SEE ALSO:
Creating alias resources
A
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
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
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.
FILE LOCATION:
The filename is used as the resource ID.COMPILED RESOURCE DATATYPE:Resource pointer to a
In XML:
SEE ALSO:
2D Graphics
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:
The filename is used as the resource ID.COMPILED RESOURCE DATATYPE:Resource pointer to a
In XML:
ELEMENTS:
attributes:
EXAMPLE:
A
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
FILE LOCATION:
The filename is used as the resource ID.COMPILED RESOURCE DATATYPE:Resource pointer to a
In XML:
ELEMENTS:
attributes:
attributes:
a new ID. You can use this identifier to retrieve and modify the drawable with
All drawable items are scaled to fit the size of the containing View, by default. Thus, placing your images in a layer list at different positions might increase the size of the View and some images scale as appropriate.
To avoid scaling items in the list, use a
the following
To avoid scaling, the following example uses a
EXAMPLE:XML file saved at
Notice that this example uses a nested
of the container, due to resizing caused by the offset images.
This layout XML applies the drawable to a View:
The result is a stack of increasingly offset images:
SEE ALSO:
A
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
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
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:
The filename is used as the resource ID.COMPILED RESOURCE DATATYPE:Resource pointer to a
In XML:
ELEMENTS:
attributes:
Default is true.
requires that you deal with performing layout when the state changes, which is often not supported. Default is false.
attributes:
for the "focused" state.
Introduced in API level 14.
be used when the object is not selected.
The selected state is used when focus (
widget.)
be used when the object is not activated.
Introduced in API level 11.
example, if the notification shade is pulled down or a dialog appears).
Note: Remember that Android applies the first item in the state list that matches the current state of the object. So, if the first item in the list contains none of the state attributes above, then it is applied every time, which is why your
default value should always be last (as demonstrated in the following example).
EXAMPLE:XML file saved at
This layout XML applies the state list drawable to a Button:
SEE ALSO:
A Drawable that manages a number of alternate Drawables, each assigned a maximum numerical value. Setting the level value of the drawable with
the drawable resource in the level list that has a
FILE LOCATION:
The filename is used as the resource ID.COMPILED RESOURCE DATATYPE:Resource pointer to a
In XML:
ELEMENTS:
attributes:
attributes:
EXAMPLE:
Once this is applied to a
be changed with
SEE ALSO:
A
a drawable object that can cross-fade between the two drawable resources.
Each drawable is represented by an
To transition backward, call
FILE LOCATION:
The filename is used as the resource ID.COMPILED RESOURCE DATATYPE:Resource pointer to a
In XML:
ELEMENTS:
attributes:
attributes:
a new ID. You can use this identifier to retrieve and modify the drawable with
EXAMPLE:XML file saved at
This layout XML applies the drawable to a View:
And the following code performs a 500ms transition from the first item to the second:
SEE ALSO:
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:
The filename is used as the resource ID.COMPILED RESOURCE DATATYPE:Resource pointer to a
In XML:
ELEMENTS:
attributes:
resource
resource
resource
resource
EXAMPLE:
SEE ALSO:
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:
The filename is used as the resource ID.COMPILED RESOURCE DATATYPE:Resource pointer to a
In XML:
ELEMENTS:
attributes:
Must be one of the following constant values:
Must be one or more (separated by '|') of the following constant values:
EXAMPLE:XML file saved at
The following layout XML applies the clip drawable to a View:
The following code gets the drawable and increases the amount of clipping in order to progressively reveal the image:
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:
A drawable defined in XML that changes the size of another drawable based on its current level.
FILE LOCATION:
The filename is used as the resource ID.COMPILED RESOURCE DATATYPE:Resource pointer to a
In XML:
ELEMENTS:
attributes:
Must be one or more (separated by '|') of the following constant values:
EXAMPLE:
SEE ALSO:
This is a generic shape defined in XML.
FILE LOCATION:
The filename is used as the resource ID.COMPILED RESOURCE DATATYPE:Resource pointer to a
In XML:
ELEMENTS:
attributes:
The following attributes are used only when
resource.
by 5. This value is overridden by
resource.
overridden by
This should normally be "false" or your shape may not appear.
attributes:
resource. This is overridden for each corner by the following attributes.
resource.
resource.
resource.
resource.
Note: Every corner must (initially) be provided a corner radius greater than 1, or else no corners are rounded. If you want specific corners to not be rounded, a work-around is to use
corner radius greater than 1, but then override each and every corner with the values you really want, providing zero ("0dp") where you don't want rounded corners.
attributes:
resource.
resource.
resource.
attributes:
resource.
resource.
resource.
resource.
attributes:
resource.
resource.
Note: The shape scales to the size of the container View proportionate to the dimensions defined here, by default. When you use the shape in an
you can restrict scaling by setting the
attributes:
resource.
attributes:
resource.
resource.
resource. Only valid if
resource. Only valid if
EXAMPLE:XML file saved at
This layout XML applies the shape drawable to a View:
This application code gets the shape drawable and applies it to a View:
Drawable shape = res.
TextView tv = (TextView)findViewByID(R.id.textview);
tv.setBackground(shape);[/code]
SEE ALSO:
可绘制资源是可以被绘制到屏幕上,哪些是你可以用的API,如检索图形的一般概念
位图文件位图图形文件(
也看到了动画资源如何创建一个文档
注:一个颜色资源,也可以作为XML的绘制。例如,在创建时状态列表绘制,可以参考的颜色资源
位图图像。Android支持三种格式的位图文件:
您可以直接引用的位图文件,使用文件名作为资源ID,或创建XML别名资源ID。
注:位图文件可能由无损图像压缩自动优化
位图文件是一个
文件位置:
的文件名 作为资源ID。编译的资源数据类型:资源指针
在XML:
下面的应用程序代码检索图像作为
可绘制可绘制= 资源。
也可以看看:
2D图形
一个XML位图是XML定义的资源指向位图文件。效果为原始位图文件的别名。该XML可以指定位图的附加属性,如抖动和瓷砖。
注意:您可以使用
文件位置:
文件名 作为资源ID。编译的资源数据类型:资源指针
在XML:
内容:
属性:
必须是一个或多个(由分隔'|')以下的恒定值:
必须是以下常数值之一:
例:
也可以看看:
创建别名资源
一个
同与正常的位图,你可以直接或通过XML定义的资源引用九宫文件。
有关如何创建具有拉伸区域的九宫文件的完整讨论,请参阅2D图形 文件。
文件位置:
的文件名 被用作资源ID。编译的资源数据类型:资源指针
在XML:
也可以看看:
2D图形
一个XML九宫格是XML指向一个九宫文件中定义的资源。该XML可以指定抖动的图像。
文件位置:
文件名 作为资源ID。编译的资源数据类型:资源指针
在XML:
内容:
属性:
例:
一个
每个绘制由一个代表
文件位置:
文件名 作为资源ID。编译的资源数据类型:资源指针
在XML:
内容:
属性:
属性:
所有的绘制项目缩放到适合包含视图的大小,默认情况下。因此,在不同位置放置图像在图层列表可能会增加视图的大小和一些图像缩放为宜。为了避免在列表中缩放项目,使用
为了避免结垢,下面的示例使用
例:在保存XML文件
请注意,此示例使用嵌套的
这种布局XML应用绘制到View:
结果是日益偏移图像的堆栈:
也可以看看:
一个
你可以这样描述XML文件的状态列表。每个图形由一个代表
在每个状态变化,状态列表遍历从上到下,符合当前的状态下使用,在选择的第一个项目不是基于“最佳匹配”,但仅仅是符合国家的最低标准的第一个项目。
文件位置:
文件名 作为资源ID。编译的资源数据类型:资源指针
在XML:
内容:
属性:
属性:
介绍了API级别14。
选择状态时,使用对焦(
介绍了API级别11。
注:请记住,在Android的匹配对象的当前状态的状态列表适用中的第一项。因此,如果在该列表中的第一项包含任何状态的上述属性,那么它被施加每一次,这就是为什么预设值应始终是最后(如下面的示例所示)。
例:在保存XML文件
这种布局XML应用状态列表绘制到按钮:
也可以看看:
一个管理若干备用可绘制的绘制对象,每人分到的最大数值。设置与绘制的电平值
文件位置:
文件名 作为资源ID。编译的资源数据类型:资源指针
在XML:
内容:
属性:
属性:
例:
一旦这个被施加到一个
也可以看看:
一个
每个绘制由一个代表
文件位置:
文件名 作为资源ID。编译的资源数据类型:资源指针
在XML:
内容:
属性:
属性:
例:在保存XML文件
这种布局XML应用绘制到View:
而下面的代码执行从第一项到第二500ms的转变:
也可以看看:
在XML的镶石按指定距离的另一绘制定义的绘制。当一个视图需要一个背景是不是查看实际边界较小,这非常有用。
文件位置:
文件名 作为资源ID。编译的资源数据类型:资源指针
在XML:
内容:
属性:
例:
也可以看看:
在XML中定义的可绘制的剪辑另一个可绘制在此基础上绘制对象的当前水平。可以控制多少子抽拉得到在宽度和高度根据该电平,以及一个重力来控制在其被放置在其整体容器裁剪。最常用的实现之类的进度条。
文件位置:
文件名 作为资源ID。编译的资源数据类型:资源指针
在XML:
内容:
属性:
必须是以下常数值之一:
必须是一个或多个(由分隔'|')以下的恒定值:
例:在保存XML文件
下面的布局XML应用剪辑绘制一个观点:
以下代码获取被拉伸并增加以便剪裁逐步显示图像的量:
增加水平降低削波的量和慢慢揭示了图像。这是在7000的水平:
注:默认级别为0,这是完全修剪,因此图像是不可见的。当电平是10,000时,图像不裁剪和完全可见。
也可以看看:
在XML改变基于当前水平的另一个绘制的大小定义的绘制。
文件位置:
文件名 作为资源ID。编译的资源数据类型:资源指针
在XML:
内容:
属性:
必须是一个或多个(由分隔'|')以下的恒定值:
例:
也可以看看:
这是在XML中定义的通用形状。
文件位置:
文件名 作为资源ID。编译的资源数据类型:资源指针
在XML:
内容:
属性:
下面的属性只用于
属性:
注:每一个角落,必须在开始阶段被超过1提供一个拐角半径大,否则没有边角圆润。如果你想具体的角落,不进行四舍五入,一个解决办法是使用
属性:
属性:
属性:
注意:形状扩展到容器视图相称这里定义的尺寸的大小,在默认情况下。当您在使用的形状
属性:
属性:
例:在保存XML文件
这种布局XML应用形状绘制到View:
此应用程序代码获取形状绘制,并将其应用于一个观点:
Drawable shape = res .
TextView tv = ( TextView ) findViewByID ( R . id . textview );
tv . setBackground ( shape );[/code]
也可以看看:
getDrawable(int)or
apply to another XML resource with attributes such as
android:drawableand
android:icon. There are several different types of drawables:
Bitmap FileA bitmap graphic file (
.png,
.jpg, or
.gif). Creates a
BitmapDrawable.Nine-Patch FileA PNG file with stretchable regions to allow image resizing based on content (
.9.png). Creates a
NinePatchDrawable.Layer ListA 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 ListAn 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 ListAn XML file that defines a drawable that manages a number of alternate Drawables, each assigned a maximum numerical value. Creates a
LevelListDrawable.Transition DrawableAn XML file that defines a drawable that can cross-fade between two drawable resources. Creates a
TransitionDrawable.Inset DrawableAn 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 DrawableAn XML file that defines a drawable that clips another Drawable based on this Drawable's current level value. Creates a
ClipDrawable.Scale DrawableAn XML file that defines a drawable that changes the size of another Drawable based on its current level value. Creates a
ScaleDrawableShape DrawableAn 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:drawableattribute
(
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
aapttool 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
.giffile. Android creates a
Drawableresource
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/filenameEXAMPLE: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 = [code]getResources();
Drawable drawable = res.
getDrawable(R.drawable.myimage);[/code]
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.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:drawableattribute
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/filenameSYNTAX:
<?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:
<bitmap>Defines the bitmap source and its properties.
attributes:
xmlns:androidString. Defines the XML namespace, which must be
"http://schemas.android.com/apk/res/android". This is required only if the
<bitmap>is the root element—it is not needed
when the
<bitmap>is nested inside an
<item>.
android:srcDrawable resource. Required. Reference to a drawable resource.
android:antialiasBoolean. Enables or disables antialiasing.
android:ditherBoolean. Enables or disables dithering of the bitmap if the bitmap does not have the same pixel configuration as the screen (for instance: a ARGB 8888 bitmap with an RGB 565 screen).
android:filterBoolean. Enables or disables bitmap filtering. Filtering is used when the bitmap is shrunk or stretched to smooth its apperance.
android:gravityKeyword. Defines the gravity for the bitmap. The gravity indicates where to position the drawable in its container if the bitmap is smaller than the container.
Must be one or more (separated by '|') of the following constant values:
Value | Description |
---|---|
top | Put the object at the top of its container, not changing its size. |
bottom | Put the object at the bottom of its container, not changing its size. |
left | Put the object at the left edge of its container, not changing its size. |
right | Put the object at the right edge of its container, not changing its size. |
center_vertical | Place object in the vertical center of its container, not changing its size. |
fill_vertical | Grow the vertical size of the object if needed so it completely fills its container. |
center_horizontal | Place object in the horizontal center of its container, not changing its size. |
fill_horizontal | Grow the horizontal size of the object if needed so it completely fills its container. |
center | Place the object in the center of its container in both the vertical and horizontal axis, not changing its size. |
fill | Grow the horizontal and vertical size of the object if needed so it completely fills its container. This is the default. |
clip_vertical | Additional option that can be set to have the top and/or bottom edges of the child clipped to its container's bounds. The clip is based on the vertical gravity: a top gravity clips the bottom edge, a bottom gravity clips the top edge, and neither clips both edges. |
clip_horizontal | Additional option that can be set to have the left and/or right edges of the child clipped to its container's bounds. The clip is based on the horizontal gravity: a left gravity clips the right edge, a right gravity clips the left edge, and neither clips both edges. |
android:mipMapBoolean. Enables or disables the mipmap hint. See
setHasMipMap()for
more information. Default value is false.
android:tileModeKeyword. Defines the tile mode. When the tile mode is enabled, the bitmap is repeated. Gravity is ignored when the tile mode is enabled.
Must be one of the following constant values:
Value | Description |
---|---|
disabled | Do not tile the bitmap. This is the default value. |
clamp | Replicates the edge color if the shader draws outside of its original bounds |
repeat | Repeats the shader's image horizontally and vertically. |
mirror | Repeats the shader's image horizontally and vertically, alternating mirror images so that adjacent images always seam. |
<?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 NinePatchis 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
Buttonwidget,
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/filenameEXAMPLE: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/filenameSYNTAX:
<?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:
<nine-patch>Defines the Nine-Patch source and its properties.
attributes:
xmlns:androidString. Required. Defines the XML namespace, which must be
"http://schemas.android.com/apk/res/android".
android:srcDrawable resource. Required. Reference to a Nine-Patch file.
android:ditherBoolean. Enables or disables dithering of the bitmap if the bitmap does not have the same pixel configuration as the screen (for instance: a ARGB 8888 bitmap with an RGB 565 screen).
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 LayerDrawableis 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/filenameSYNTAX:
<?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:
<layer-list>Required. This must be the root element. Contains one or more
<item>elements.
attributes:
xmlns:androidString. Required. Defines the XML namespace, which must be
"http://schemas.android.com/apk/res/android".
<item>Defines a drawable to place in the layer drawable, in a position defined by its attributes. Must be a child of a
<selector>element. Accepts child
<bitmap>elements.
attributes:
android:drawableDrawable resource. Required. Reference to a drawable resource.
android:idResource ID. A unique resource ID for this drawable. To create a new resource ID for this item, use the form:
"@+id/name". The plus symbol indicates that this should be created as
a new ID. You can use this identifier to retrieve and modify the drawable with
View.findViewById()or
Activity.findViewById().
android:topInteger. The top offset in pixels.
android:rightInteger. The right offset in pixels.
android:bottomInteger. The bottom offset in pixels.
android:leftInteger. The left offset in pixels.
All drawable items are scaled to fit the size of the containing View, by default. Thus, placing your images in a layer list at different positions might increase the size of the View and some images scale as appropriate.
To avoid scaling items in the list, use a
<bitmap>element inside the
<item>element to specify the drawable and define the gravity to something that does not scale, such as
"center". For example,
the following
<item>defines an item that scales to fit its container View:
<item android:drawable="@drawable/image" />
To avoid scaling, the following example uses a
<bitmap>element with centered gravity:
<item> <bitmap android:src="@drawable/image" android:gravity="center" /> </item>
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 StateListDrawableis
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
Buttonwidget
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/filenameSYNTAX:
<?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:
<selector>Required. This must be the root element. Contains one or more
<item>elements.
attributes:
xmlns:androidString. Required. Defines the XML namespace, which must be
"http://schemas.android.com/apk/res/android".
android:constantSizeBoolean. "true" if the drawable's reported internal size remains constant as the state changes (the size is the maximum of all of the states); "false" if the size varies based on the current state. Default is false.
android:ditherBoolean. "true" to enable dithering of the bitmap if the bitmap does not have the same pixel configuration as the screen (for instance, an ARGB 8888 bitmap with an RGB 565 screen); "false" to disable dithering.
Default is true.
android:variablePaddingBoolean. "true" if the drawable's padding should change based on the current state that is selected; "false" if the padding should stay the same (based on the maximum padding of all the states). Enabling this feature
requires that you deal with performing layout when the state changes, which is often not supported. Default is false.
<item>Defines a drawable to use during certain states, as described by its attributes. Must be a child of a
<selector>element.
attributes:
android:drawableDrawable resource. Required. Reference to a drawable resource.
android:state_pressedBoolean. "true" if this item should be used when the object is pressed (such as when a button is touched/clicked); "false" if this item should be used in the default, non-pressed state.
android:state_focusedBoolean. "true" if this item should be used when the object has input focus (such as when the user selects a text input); "false" if this item should be used in the default, non-focused state.
android:state_hoveredBoolean. "true" if this item should be used when the object is being hovered by a cursor; "false" if this item should be used in the default, non-hovered state. Often, this drawable may be the same drawable used
for the "focused" state.
Introduced in API level 14.
android:state_selectedBoolean. "true" if this item should be used when the object is the current user selection when navigating with a directional control (such as when navigating through a list with a d-pad); "false" if this item should
be used when the object is not selected.
The selected state is used when focus (
android:state_focused) is not sufficient (such as when list view has focus and an item within it is selected with a d-pad).
android:state_checkableBoolean. "true" if this item should be used when the object is checkable; "false" if this item should be used when the object is not checkable. (Only useful if the object can transition between a checkable and non-checkable
widget.)
android:state_checkedBoolean. "true" if this item should be used when the object is checked; "false" if it should be used when the object is un-checked.
android:state_enabledBoolean. "true" if this item should be used when the object is enabled (capable of receiving touch/click events); "false" if it should be used when the object is disabled.
android:state_activatedBoolean. "true" if this item should be used when the object is activated as the persistent selection (such as to "highlight" the previously selected list item in a persistent navigation view); "false" if it should
be used when the object is not activated.
Introduced in API level 11.
android:state_window_focusedBoolean. "true" if this item should be used when the application window has focus (the application is in the foreground), "false" if this item should be used when the application window does not have focus (for
example, if the notification shade is pulled down or a dialog appears).
Note: Remember that Android applies the first item in the state list that matches the current state of the object. So, if the first item in the list contains none of the state attributes above, then it is applied every time, which is why your
default value should always be last (as demonstrated in the following example).
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:maxLevelvalue 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/filenameSYNTAX:
<?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:
<level-list>This must be the root element. Contains one or more
<item>elements.
attributes:
xmlns:androidString. Required. Defines the XML namespace, which must be
"http://schemas.android.com/apk/res/android".
<item>Defines a drawable to use at a certain level.
attributes:
android:drawableDrawable resource. Required. Reference to a drawable resource to be inset.
android:maxLevelInteger. The maximum level allowed for this item.
android:minLevelInteger. The minimum level allowed for this item.
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 with
setLevel()or
setImageLevel().
SEE ALSO:
LevelListDrawable
Transition Drawable
A TransitionDrawableis
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().
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/filenameSYNTAX:
<?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:
<transition>Required. This must be the root element. Contains one or more
<item>elements.
attributes:
xmlns:androidString. Required. Defines the XML namespace, which must be
"http://schemas.android.com/apk/res/android".
<item>Defines a drawable to use as part of the drawable transition. Must be a child of a
<transition>element. Accepts child
<bitmap>elements.
attributes:
android:drawableDrawable resource. Required. Reference to a drawable resource.
android:idResource ID. A unique resource ID for this drawable. To create a new resource ID for this item, use the form:
"@+id/name". The plus symbol indicates that this should be created as
a new ID. You can use this identifier to retrieve and modify the drawable with
View.findViewById()or
Activity.findViewById().
android:topInteger. The top offset in pixels.
android:rightInteger. The right offset in pixels.
android:bottomInteger. The bottom offset in pixels.
android:leftInteger. The left offset in pixels.
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/filenameSYNTAX:
<?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:
<inset>Defines the inset drawable. This must be the root element.
attributes:
xmlns:androidString. Required. Defines the XML namespace, which must be
"http://schemas.android.com/apk/res/android".
android:drawableDrawable resource. Required. Reference to a drawable resource to be inset.
android:insetTopDimension. The top inset, as a dimension value or dimension
resource
android:insetRightDimension. The right inset, as a dimension value or dimension
resource
android:insetBottomDimension. The bottom inset, as a dimension value or dimension
resource
android:insetLeftDimension. The left inset, as a dimension value or dimension
resource
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 gravityto 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/filenameSYNTAX:
<?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:
<clip>Defines the clip drawable. This must be the root element.
attributes:
xmlns:androidString. Required. Defines the XML namespace, which must be
"http://schemas.android.com/apk/res/android".
android:drawableDrawable resource. Required. Reference to a drawable resource to be clipped.
android:clipOrientationKeyword. The orientation for the clip.
Must be one of the following constant values:
Value | Description |
---|---|
horizontal | Clip the drawable horizontally. |
vertical | Clip the drawable vertically. |
android:gravityKeyword. Specifies where to clip within the drawable.
Must be one or more (separated by '|') of the following constant values:
Value | Description |
---|---|
top | Put the object at the top of its container, not changing its size. When clipOrientationis "vertical", clipping occurs at the bottom of the drawable. |
bottom | Put the object at the bottom of its container, not changing its size. When clipOrientationis "vertical", clipping occurs at the top of the drawable. |
left | Put the object at the left edge of its container, not changing its size. This is the default. When clipOrientationis "horizontal", clipping occurs at the right side of the drawable. This is the default. |
right | Put the object at the right edge of its container, not changing its size. When clipOrientationis "horizontal", clipping occurs at the left side of the drawable. |
center_vertical | Place object in the vertical center of its container, not changing its size. Clipping behaves the same as when gravity is"center". |
fill_vertical | Grow the vertical size of the object if needed so it completely fills its container. When clipOrientationis "vertical", no clipping occurs because the drawable fills the vertical space (unless the drawable level is 0, in which case it's not visible). |
center_horizontal | Place object in the horizontal center of its container, not changing its size. Clipping behaves the same as when gravity is"center". |
fill_horizontal | Grow the horizontal size of the object if needed so it completely fills its container. When clipOrientationis "horizontal", no clipping occurs because the drawable fills the horizontal space (unless the drawable level is 0, in which case it's not visible). |
center | Place the object in the center of its container in both the vertical and horizontal axis, not changing its size. WhenclipOrientationis "horizontal", clipping occurs on the left and right. When clipOrientationis "vertical", clipping occurs on the top and bottom. |
fill | Grow the horizontal and vertical size of the object if needed so it completely fills its container. No clipping occurs because the drawable fills the horizontal and vertical space (unless the drawable level is 0, in which case it's not visible). |
clip_vertical | Additional option that can be set to have the top and/or bottom edges of the child clipped to its container's bounds. The clip is based on the vertical gravity: a top gravity clips the bottom edge, a bottom gravity clips the top edge, and neither clips both edges. |
clip_horizontal | Additional option that can be set to have the left and/or right edges of the child clipped to its container's bounds. The clip is based on the horizontal gravity: a left gravity clips the right edge, a right gravity clips the left edge, and neither clips both edges. |
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.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/filenameSYNTAX:
<?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:
<scale>Defines the scale drawable. This must be the root element.
attributes:
xmlns:androidString. Required. Defines the XML namespace, which must be
"http://schemas.android.com/apk/res/android".
android:drawableDrawable resource. Required. Reference to a drawable resource.
android:scaleGravityKeyword. Specifies the gravity position after scaling.
Must be one or more (separated by '|') of the following constant values:
Value | Description |
---|---|
top | Put the object at the top of its container, not changing its size. |
bottom | Put the object at the bottom of its container, not changing its size. |
left | Put the object at the left edge of its container, not changing its size. This is the default. |
right | Put the object at the right edge of its container, not changing its size. |
center_vertical | Place object in the vertical center of its container, not changing its size. |
fill_vertical | Grow the vertical size of the object if needed so it completely fills its container. |
center_horizontal | Place object in the horizontal center of its container, not changing its size. |
fill_horizontal | Grow the horizontal size of the object if needed so it completely fills its container. |
center | Place the object in the center of its container in both the vertical and horizontal axis, not changing its size. |
fill | Grow the horizontal and vertical size of the object if needed so it completely fills its container. |
clip_vertical | Additional option that can be set to have the top and/or bottom edges of the child clipped to its container's bounds. The clip is based on the vertical gravity: a top gravity clips the bottom edge, a bottom gravity clips the top edge, and neither clips both edges. |
clip_horizontal | Additional option that can be set to have the left and/or right edges of the child clipped to its container's bounds. The clip is based on the horizontal gravity: a left gravity clips the right edge, a right gravity clips the left edge, and neither clips both edges. |
android:scaleHeightPercentage. The scale height, expressed as a percentage of the drawable's bound. The value's format is XX%. For instance: 100%, 12.5%, etc.
android:scaleWidthPercentage. The scale width, expressed as a percentage of the drawable's bound. The value's format is XX%. For instance: 100%, 12.5%, etc.
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.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/filenameSYNTAX:
<?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:
<shape>The shape drawable. This must be the root element.
attributes:
xmlns:androidString. Required. Defines the XML namespace, which must be
"http://schemas.android.com/apk/res/android".
android:shapeKeyword. Defines the type of shape. Valid values are:
Value | Desciption |
---|---|
"rectangle" | A rectangle that fills the containing View. This is the default shape. |
"oval" | An oval shape that fits the dimensions of the containing View. |
"line" | A horizontal line that spans the width of the containing View. This shape requires the <stroke>element to define the width of the line. |
"ring" | A ring shape. |
android:shape="ring":
android:innerRadiusDimension. The radius for the inner part of the ring (the hole in the middle), as a dimension value or dimension
resource.
android:innerRadiusRatioFloat. The radius for the inner part of the ring, expressed as a ratio of the ring's width. For instance, if
android:innerRadiusRatio="5", then the inner radius equals the ring's width divided
by 5. This value is overridden by
android:innerRadius. Default value is 9.
android:thicknessDimension. The thickness of the ring, as a dimension value or dimension
resource.
android:thicknessRatioFloat. The thickness of the ring, expressed as a ratio of the ring's width. For instance, if
android:thicknessRatio="2", then the thickness equals the ring's width divided by 2. This value is
overridden by
android:innerRadius. Default value is 3.
android:useLevelBoolean. "true" if this is used as a
LevelListDrawable.
This should normally be "false" or your shape may not appear.
<corners>Creates rounded corners for the shape. Applies only when the shape is a rectangle.
attributes:
android:radiusDimension. The radius for all corners, as a dimension value or dimension
resource. This is overridden for each corner by the following attributes.
android:topLeftRadiusDimension. The radius for the top-left corner, as a dimension value or dimension
resource.
android:topRightRadiusDimension. The radius for the top-right corner, as a dimension value or dimension
resource.
android:bottomLeftRadiusDimension. The radius for the bottom-left corner, as a dimension value or dimension
resource.
android:bottomRightRadiusDimension. The radius for the bottom-right corner, as a dimension value or dimension
resource.
Note: Every corner must (initially) be provided a corner radius greater than 1, or else no corners are rounded. If you want specific corners to not be rounded, a work-around is to use
android:radiusto set a default
corner radius greater than 1, but then override each and every corner with the values you really want, providing zero ("0dp") where you don't want rounded corners.
<gradient>Specifies a gradient color for the shape.
attributes:
android:angleInteger. The angle for the gradient, in degrees. 0 is left to right, 90 is bottom to top. It must be a multiple of 45. Default is 0.
android:centerXFloat. The relative X-position for the center of the gradient (0 - 1.0).
android:centerYFloat. The relative Y-position for the center of the gradient (0 - 1.0).
android:centerColorColor. Optional color that comes between the start and end colors, as a hexadecimal value orcolor
resource.
android:endColorColor. The ending color, as a hexadecimal value or color
resource.
android:gradientRadiusFloat. The radius for the gradient. Only applied when
android:type="radial".
android:startColorColor. The starting color, as a hexadecimal value or color
resource.
android:typeKeyword. The type of gradient pattern to apply. Valid values are:
Value | Description |
---|---|
"linear" | A linear gradient. This is the default. |
"radial" | A radial gradient. The start color is the center color. |
"sweep" | A sweeping line gradient. |
android:useLevelBoolean. "true" if this is used as a
LevelListDrawable.
<padding>Padding to apply to the containing View element (this pads the position of the View content, not the shape).
attributes:
android:leftDimension. Left padding, as a dimension value or dimension
resource.
android:topDimension. Top padding, as a dimension value or dimension
resource.
android:rightDimension. Right padding, as a dimension value or dimension
resource.
android:bottomDimension. Bottom padding, as a dimension value or dimension
resource.
<size>The size of the shape.
attributes:
android:heightDimension. The height of the shape, as a dimension value or dimension
resource.
android:widthDimension. The width of the shape, as a dimension value or dimension
resource.
Note: The shape scales to the size of the container View proportionate to the dimensions defined here, by default. When you use the shape in an
ImageView,
you can restrict scaling by setting the
android:scaleTypeto
"center".
<solid>A solid color to fill the shape.
attributes:
android:colorColor. The color to apply to the shape, as a hexadecimal value or color
resource.
<stroke>A stroke line for the shape.
attributes:
android:widthDimension. The thickness of the line, as a dimension value or dimension
resource.
android:colorColor. The color of the line, as a hexadecimal value or color
resource.
android:dashGapDimension. The distance between line dashes, as a dimension value or dimension
resource. Only valid if
android:dashWidthis set.
android:dashWidthDimension. The size of each dash line, as a dimension value or dimension
resource. Only valid if
android:dashGapis set.
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 = [code]getResources();
Drawable shape = res.
getDrawable(R.drawable.gradient_box);
TextView tv = (TextView)findViewByID(R.id.textview);
tv.setBackground(shape);[/code]
SEE ALSO:
ShapeDrawable
可绘制资源是可以被绘制到屏幕上,哪些是你可以用的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 = [code]getResources ();
可绘制可绘制= 资源。
getDrawable(ř 。绘制。MYIMAGE );[/code]
也可以看看:
2D图形
BitmapDrawable
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" ] />
内容:
<位图>定义位图的源和它的属性。
属性:
的xmlns:机器人字符串。定义了XML命名空间,它必须是
“http://schemas.android.com/apk/res/android”。这是必需的仅在
<位图>是根元素,它的时无需
<位图>嵌套内侧
的<item>。
机器人:SRC绘制资源,必需。参照绘制资源。
机器人:反锯齿布尔。启用或禁用抗锯齿。
机器人:抖动布尔。启用或禁用抖动位图,如果位图不具有相同的像素结构的屏幕(例如:一个ARGB 8888位图与一个RGB 565屏幕)。
机器人:过滤器布尔。启用或禁用位图筛选。当位图收缩或伸展平滑的外观过滤使用。
安卓重力关键字。定义该位图的严重性。重力表示,其中以定位可绘在其容器如果位图是比容器更小。
必须是一个或多个(由分隔'|')以下的恒定值:
值 | 描述 |
---|---|
最佳 | 放对象在其容器的顶部,不改变其大小。 |
底部 | 放对象在其容器的底部,不改变其大小。 |
剩下 | 放对象在其容器的左边缘,不改变其大小。 |
对 | 放对象在其容器的右边缘,不改变其大小。 |
center_vertical | 放置在其容器的垂直中心对象,不改变其大小。 |
fill_vertical | 如果需要的话,以便完全填满其容器生长的物体的垂直尺寸。 |
CENTER_HORIZONTAL | 放置在其容器的水平中心对象,不改变其大小。 |
fill_horizontal | 如果需要的话,以便完全填满其容器生长的物体的水平尺寸。 |
中央 | 放置在其容器中的垂直和水平轴两个中心的对象,不改变其大小。 |
填 | 如果需要的话,以便完全填满其容器成长对象的水平和垂直尺寸。这是默认的。 |
clip_vertical | 可以设置附加的选项,使顶部和/或儿童的底部边缘夹在其容器的边界。夹子基于垂直重力:顶部重力夹子的底部边缘,一个底重力夹子的顶部边缘,并且既不剪辑两边缘。 |
clip_horizontal | 可以设置附加的选项,使左和/或儿童的右边缘夹在其容器的边界。夹子基于水平比重:左重力剪辑的右边缘,右重力夹子的左边缘,并且既不剪辑两边缘。 |
机器人:MIPMAP布尔。启用或禁用的mipmap提示。见
setHasMipMap()了解更多信息。默认值是假的。
机器人:TILEMODE关键字。定义平铺模式。当启用瓦模式,该位图被重复。启用平铺模式时,重力会被忽略。
必须是以下常数值之一:
值 | 描述 |
---|---|
残 | 不平铺的位图。这是默认值。 |
钳 | 复制边缘颜色,如果着色器绘制其原有的边界外 |
重复 | 水平和垂直方向重复着色器的形象。 |
镜子 | 水平和垂直方向重复着色器的形象,交替镜像,使相邻图像总是缝。 |
<?xml的version = "1.0" encoding = "utf-8" ?> <bitmap xmlns:android = "http://schemas.android.com/apk/res/android" android:src = "@drawable/icon" android:tileMode = "repeat" />
也可以看看:
BitmapDrawable
创建别名资源
九宫格
一个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" />
也可以看看:
2D图形
NinePatchDrawable
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" ] />
内容:
<九补丁>定义九宫格来源和它的属性。
属性:
的xmlns:机器人字符串,必需。定义XML命名空间,它必须是
“http://schemas.android.com/apk/res/android”。
机器人:SRC绘制资源,必需。参照九宫文件。
机器人:抖动布尔。启用或禁用抖动位图,如果位图不具有相同的像素结构的屏幕(例如:一个ARGB 8888位图与一个RGB 565屏幕)。
例:
<?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>
内容:
<层列表>必选项。这必须是根元素。包含一个或多个
的<item>元素。
属性:
的xmlns:机器人字符串,必需。定义XML命名空间,它必须是
“http://schemas.android.com/apk/res/android”。
<项目>定义绘制的层绘制到另一个地方,在它的属性定义的位置。必须是一个的子
<选择>元素。接受孩子
<位图>元素。
属性:
机器人:可绘制绘制资源,必需。参照绘制资源。
机器人:ID资源ID。为此绘制一个独特的资源ID。要为这个项目创建一个新的资源ID,使用以下形式:
“@ + ID / 名称 ”。加符号表示,本应作为一个新的ID来创建。您可以使用此标识来检索和修改与绘制
View.findViewById()或
Activity.findViewById()。
机器人:顶部整。顶部的像素偏移。
机器人:右整。正确的像素偏移量。
机器人:底部整。底部的像素偏移。
机器人:左整。在像素,左偏移。
所有的绘制项目缩放到适合包含视图的大小,默认情况下。因此,在不同位置放置图像在图层列表可能会增加视图的大小和一些图像缩放为宜。为了避免在列表中缩放项目,使用
<位图>元素里面
的<item>元素指定绘制并定义重力的东西,不结垢,如
“中心”。例如,下面
的<item>定义进行缩放以适应其容器查看一个项目:
<项目 机器人:可绘制= “@绘制/图像” />
为了避免结垢,下面的示例使用
<位图>元素与中心的重心:
<项目> <位图 的android:SRC = “ @绘制/图像 ” 机器人:重力= “中心” /> </项目>
例:在保存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" />
结果是日益偏移图像的堆栈:
也可以看看:
LayerDrawable
国家名单
一个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>
内容:
<选择>必选项。这必须是根元素。包含一个或多个
的<item>元素。
属性:
的xmlns:机器人字符串,必需。定义XML命名空间,它必须是
“http://schemas.android.com/apk/res/android”。
机器人:constantSize布尔。“真”,如果绘制的内部报告的大小仍然是状态的改变常数(尺寸是最大的所有国家的);“假”,如果尺寸变化基于当前的状态。默认为false。
机器人:抖动布尔。“真”,以使该位图的抖动,如果位图不具有相同的像素结构的屏幕(例如,一个ARGB 8888位图与一个RGB 565屏幕); “假”以禁用抖动。默认值为true。
机器人:variablePadding布尔。“真”,如果绘制的填充应根据所选择的当前状态的变化; “假”如果填充应保持不变(基于所有国家的最大填充)。启用此功能,您需要处理时,这往往是不支持的状态变化,进行布局。默认为false。
<项目>定义绘制到在某些国家使用,正如它的属性描述。必须是一个的子
<选择>元素。
属性:
机器人:可绘制绘制资源,必需。参照绘制资源。
机器人:state_pressed布尔。“真”,如果当按下对象应使用此产品的(当按钮被触摸诸如/点击); “假”,如果这个项目应该在默认使用,非按下状态。
机器人:state_focused布尔。“真”如果这个项目时,应使用对象具有输入焦点(当用户选择一个文本输入等); “假”,如果这个项目应该在默认使用,非聚焦状态。
机器人:state_hovered布尔。“真”,如果当物体被由光标悬停应该使用这个产品 “假”,如果这个项目应该在默认使用,非悬停状态。通常,这可拉伸可以是用于“聚焦”状态的相同可拉伸。
介绍了API级别14。
机器人:state_selected布尔。“真”,如果当物体是用方向控制导航时(例如通过与一个d-垫列表导航时)当前用户选择这个项目应该使用; “假”如果在没有选择的对象应使用此项目。
选择状态时,使用对焦(
安卓state_focused)是不够的(例如,当列表视图具有焦点,并在其中一个项目中选择了D-PAD)。
机器人:state_checkable布尔。“真”,如果当对象是可勾选应该使用这个产品,“假”,如果当对象不是可勾选此项目应该被使用。(只有有用的,如果该对象可以可检查的和非可检查的小部件之间转换。)
机器人:state_checked布尔。“真”,如果当对象被选中应该使用这个产品,“假”,如果它应当对象是使用未检查。
机器人:state_enabled布尔。“真”,如果当物体被启用应使用此产品的(能够接收触摸/点击事件); “假”,如果当物体被禁用它应该被使用。
机器人:state_activated布尔。“真”,如果当对象被激活为持久的选择应该使用这个项目(如“亮点”以前选择列表中一个持久的导航视图的项目); “假”,如果当物体不激活它应该被使用。
介绍了API级别11。
机器人:state_window_focused布尔。“真”,如果当应用程序窗口已关注这个项目应该使用(应用程序是在前台),“假”,如果这个项目时,应使用应用程序窗口没有焦点(例如,如果通知栏是拉下来,或者出现一个对话框)。
注:请记住,在Android的匹配对象的当前状态的状态列表适用中的第一项。因此,如果在该列表中的第一项包含任何状态的上述属性,那么它被施加每一次,这就是为什么预设值应始终是最后(如下面的示例所示)。
例:在保存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" />
也可以看看:
StateListDrawable
级别列表
一个管理若干备用可绘制的绘制对象,每人分到的最大数值。设置与绘制的电平值执行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>
内容:
<级列表>这必须是根元素。包含一个或多个
的<item>元素。
属性:
的xmlns:机器人字符串,必需。定义XML命名空间,它必须是
“http://schemas.android.com/apk/res/android”。
<项目>限定了可拉伸在一定的水平来使用。
属性:
机器人:可绘制绘制资源,必需。参考可绘制资源被插图。
机器人:maxLevel整。最高级别允许为这个项目。
机器人:中定义的MinLevel整。最低级别允许为这个项目。
例:
<?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()。
也可以看看:
LevelListDrawable
过渡绘制对象
一个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>
内容:
<过渡>必选项。这必须是根元素。包含一个或多个
的<item>元素。
属性:
的xmlns:机器人字符串,必需。定义XML命名空间,它必须是
“http://schemas.android.com/apk/res/android”。
<项目>定义绘制的绘制转变的一部分使用。必须是一个孩子
的<transition>元素。接受孩子
<位图>元素。
属性:
机器人:可绘制绘制资源,必需。参照绘制资源。
机器人:ID资源ID。为此绘制一个独特的资源ID。要为这个项目创建一个新的资源ID,使用以下形式:
“@ + ID / 名称 ”。加符号表示,本应作为一个新的ID来创建。您可以使用此标识来检索和修改与绘制
View.findViewById()或
Activity.findViewById()。
机器人:顶部整。顶部的像素偏移。
机器人:右整。正确的像素偏移量。
机器人:底部整。底部的像素偏移。
机器人:左整。在像素,左偏移。
例:在保存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 );
也可以看看:
TransitionDrawable
插图绘制对象
在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 " />
内容:
<插图>定义插图绘制。这必须是根元素。
属性:
的xmlns:机器人字符串,必需。定义XML命名空间,它必须是
“http://schemas.android.com/apk/res/android”。
机器人:可绘制绘制资源,必需。参考可绘制资源被插图。
机器人:insetTop尺寸。顶部插入,作为一个尺寸值或尺寸资源
机器人:insetRight尺寸。右小图,作为一个尺寸值或尺寸资源
机器人:insetBottom尺寸。底部插入,作为一个尺寸值或尺寸资源
机器人:insetLeft尺寸。左小图,作为一个尺寸值或尺寸资源
例:
<?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" />
也可以看看:
InsetDrawable
剪辑绘制对象
在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" ] />
内容:
<片段>定义剪辑绘制。这必须是根元素。
属性:
的xmlns:机器人字符串,必需。定义XML命名空间,它必须是
“http://schemas.android.com/apk/res/android”。
机器人:可绘制绘制资源,必需。参考可绘制资源进行裁剪。
机器人:clipOrientation关键字。该方向的剪辑。
必须是以下常数值之一:
值 | 描述 |
---|---|
横 | 水平剪辑的绘制。 |
垂直 | 垂直夹的绘制。 |
安卓重力关键字。指定在哪里可绘制内夹。
必须是一个或多个(由分隔'|')以下的恒定值:
值 | 描述 |
---|---|
最佳 | 放对象在其容器的顶部,不改变其大小。当clipOrientation是 “垂直”,剪辑出现在绘制的底部。 |
底部 | 放对象在其容器的底部,不改变其大小。当clipOrientation是 “垂直”,剪辑出现在绘制的顶部。 |
剩下 | 放对象在其容器的左边缘,不改变其大小。这是默认的。当clipOrientation是 “横”,剪辑出现在绘制的右侧。这是默认的。 |
对 | 放对象在其容器的右边缘,不改变其大小。当clipOrientation是 “横”,剪辑出现在绘制的左侧。 |
center_vertical | 放置在其容器的垂直中心对象,不改变其大小。裁剪的行为一样,当重力“中心”。 |
fill_vertical | 如果需要的话,以便完全填满其容器生长的物体的垂直尺寸。当clipOrientation是 “垂直”,没有发生削波,因为被拉伸填充的垂直空间(除非可绘制的水平为0,在这种情况下,它不可见)。 |
CENTER_HORIZONTAL | 放置在其容器的水平中心对象,不改变其大小。裁剪的行为一样,当重力“中心”。 |
fill_horizontal | 如果需要的话,以便完全填满其容器生长的物体的水平尺寸。当clipOrientation是 “水平”,没有发生削波,因为被拉伸填充水平空间(除非可绘制的水平为0,在这种情况下,它不可见)。 |
中央 | 放置在其容器中的垂直和水平轴两个中心的对象,不改变其大小。当clipOrientation是 “水平”,裁剪发生在左边和右边。当 clipOrientation是 “垂直”,限幅发生在顶部和底部。 |
填 | 如果需要的话,以便完全填满其容器成长对象的水平和垂直尺寸。没有发生削波,因为被拉伸填充水平和垂直空间(除非可绘制的水平为0,在这种情况下,它不可见)。 |
clip_vertical | 可以设置附加的选项,使顶部和/或儿童的底部边缘夹在其容器的边界。夹子基于垂直重力:顶部重力夹子的底部边缘,一个底重力夹子的顶部边缘,并且既不剪辑两边缘。 |
clip_horizontal | 可以设置附加的选项,使左和/或儿童的右边缘夹在其容器的边界。夹子基于水平比重:左重力剪辑的右边缘,右重力夹子的左边缘,并且既不剪辑两边缘。 |
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时,图像不裁剪和完全可见。
也可以看看:
ClipDrawable
规模可绘制
在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 " />
内容:
<规模>定义绘制规模。这必须是根元素。
属性:
的xmlns:机器人字符串,必需。定义XML命名空间,它必须是
“http://schemas.android.com/apk/res/android”。
机器人:可绘制绘制资源,必需。参照绘制资源。
机器人:scaleGravity关键字。指定缩放后的重心位置。
必须是一个或多个(由分隔'|')以下的恒定值:
值 | 描述 |
---|---|
最佳 | 放对象在其容器的顶部,不改变其大小。 |
底部 | 放对象在其容器的底部,不改变其大小。 |
剩下 | 放对象在其容器的左边缘,不改变其大小。这是默认的。 |
对 | 放对象在其容器的右边缘,不改变其大小。 |
center_vertical | 放置在其容器的垂直中心对象,不改变其大小。 |
fill_vertical | 如果需要的话,以便完全填满其容器生长的物体的垂直尺寸。 |
CENTER_HORIZONTAL | 放置在其容器的水平中心对象,不改变其大小。 |
fill_horizontal | 如果需要的话,以便完全填满其容器生长的物体的水平尺寸。 |
中央 | 放置在其容器中的垂直和水平轴两个中心的对象,不改变其大小。 |
填 | 如果需要的话,以便完全填满其容器成长对象的水平和垂直尺寸。 |
clip_vertical | 可以设置附加的选项,使顶部和/或儿童的底部边缘夹在其容器的边界。夹子基于垂直重力:顶部重力夹子的底部边缘,一个底重力夹子的顶部边缘,并且既不剪辑两边缘。 |
clip_horizontal | 可以设置附加的选项,使左和/或儿童的右边缘夹在其容器的边界。夹子基于水平比重:左重力剪辑的右边缘,右重力夹子的左边缘,并且既不剪辑两边缘。 |
机器人:scaleHeight百分比。规模高度,表示为可绘制的结合的百分比。该值的格式为XX%。例如:100%,12.5%等
机器人:scaleWidth百分比。刻度宽度,表示为可绘制的结合的百分比。该值的格式为XX%。例如:100%,12.5%等
例:
<?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%" />
也可以看看:
ScaleDrawable
形状绘制对象
这是在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>
内容:
<形状>形状绘制。这必须是根元素。
属性:
的xmlns:机器人字符串,必需。定义XML命名空间,它必须是
“http://schemas.android.com/apk/res/android”。
机器人:造型关键字。限定形状的类型。有效值为:
值 | 应将描述 |
---|---|
“长方形” | 填充包含查看的矩形。这是默认的形状。 |
“椭圆” | 一个椭圆形的形状,其适合的含视图的尺寸。 |
“线” | 水平线跨越包含视图的宽度。这种形状需要<冲程>元素来定义线的宽度。 |
“环” | 环形。 |
机器人:形状=“环”:
机器人:innerRadius尺寸。用于环(中间孔)的内部的半径,作为尺寸值或尺寸资源。
机器人:innerRadiusRatio浮动。为环的内部的半径,表示为环的宽度的比率。例如,如果
机器人:innerRadiusRatio =“5”,然后内径等于环的宽度由5分这个值被覆盖
的android:innerRadius。默认值为9。
机器人:厚度尺寸。环的厚度,作为尺寸值或尺寸资源。
机器人:thicknessRatio浮动。环的厚度,表示为环的宽度的比率。例如,如果
机器人:thicknessRatio =“2”,则厚度等于环的宽度除以2。此值由覆盖
安卓innerRadius。默认值是3。
机器人:useLevel布尔。如果此被用作“真”
LevelListDrawable。这通常应该是“假”还是你的形状可能不会出现。
<角>创建圆角的形状。仅适用于当形状为长方形。
属性:
机器人:半径尺寸。所有边角半径,作为一个尺寸值或尺寸资源。这被覆盖由以下属性的每个角落。
机器人:topLeftRadius尺寸。为左上角的半径,作为尺寸值或尺寸资源。
机器人:topRightRadius尺寸。为右上角的半径,作为尺寸值或尺寸资源。
机器人:bottomLeftRadius尺寸。为左下角的半径,作为尺寸值或尺寸资源。
机器人:bottomRightRadius尺寸。对于右下角的半径,作为尺寸值或尺寸资源。
注:每一个角落,必须在开始阶段被超过1提供一个拐角半径大,否则没有边角圆润。如果你想具体的角落,不进行四舍五入,一个解决办法是使用
安卓半径比1设置默认的圆角半径大,但覆盖你真正想要的值每一个角落,提供零(“0dp” ),您不希望圆角。
<坡度>指定形状的渐变色。
属性:
机器人:角整。渐变的角度,以度。0为从左到右,90是底部至顶部。它必须是45默认的倍数为0。
安卓的centerX浮动。相对X位置为梯度(0 - 1.0)的中心。
机器人:centerY浮动。相对Y位置的梯度(0 - 1.0)的中心。
机器人:centerColor颜色。可选颜色自带的起点和终点之间的颜色,为十六进制值或颜色资源。
机器人:ENDCOLOR颜色。结束颜色,为十六进制值或颜色资源。
机器人:gradientRadius浮动。渐变的半径。只有当应用
机器人:TYPE =“放射状”。
机器人:startColor颜色。起始色,为十六进制值或颜色资源。
机器人:类型关键字。类型渐变图案的应用。有效值为:
值 | 描述 |
---|---|
“线性” | 线性渐变。这是默认的。 |
“径向” | 径向渐变。起始颜色为中心的颜色。 |
“扫” | 所述的清扫线梯度。 |
机器人:useLevel布尔。如果此被用作“真”
LevelListDrawable。
<填充>填充要应用到含有视图元件(此焊盘视图内容的位置,而不是形状)。
属性:
机器人:左尺寸。左填充,作为一个尺寸值或尺寸资源。
机器人:顶部尺寸。顶部填充,作为一个尺寸值或尺寸资源。
机器人:右尺寸。右侧填充,作为一个尺寸值或尺寸资源。
机器人:底部尺寸。底部填充,作为一个尺寸值或尺寸资源。
<大小>形状的尺寸。
属性:
机器人:身高尺寸。形状的高度,作为尺寸值或尺寸资源。
机器人:宽尺寸。形状的宽度,作为尺寸值或尺寸资源。
注意:形状扩展到容器视图相称这里定义的尺寸的大小,在默认情况下。当您在使用的形状
ImageView的,你可以通过设置限制比例
scaleType:机器人到
“中心”。
<固体>纯色填充的形状。
属性:
机器人:颜色颜色。颜色应用到形状,以十六进制值或颜色资源。
<行程>中风线的形状。
属性:
机器人:宽尺寸。线的厚度,作为尺寸值或尺寸资源。
机器人:颜色颜色。线的颜色,如十六进制值或颜色资源。
机器人:dashGap尺寸。线之间的距离破折号,作为尺寸值或尺寸资源。只有有效的,如果
机器人:dashWidth设置。
机器人:dashWidth尺寸。各划线的大小,作为尺寸值或尺寸资源。只有有效的,如果
机器人:dashGap设置。
例:在保存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 = [code]getResources ();
Drawable shape = res .
getDrawable( R . drawable . gradient_box );
TextView tv = ( TextView ) findViewByID ( R . id . textview );
tv . setBackground ( shape );[/code]
也可以看看:
ShapeDrawable
相关文章推荐
- 使用C++实现JNI接口需要注意的事项
- Android IPC进程间通讯机制
- Android Manifest 用法
- [转载]Activity中ConfigChanges属性的用法
- Android之获取手机上的图片和视频缩略图thumbnails
- Android之使用Http协议实现文件上传功能
- Android学习笔记(二九):嵌入浏览器
- android string.xml文件中的整型和string型代替
- i-jetty环境搭配与编译
- android之定时器AlarmManager
- android wifi 无线调试
- Android Native 绘图方法
- Android java 与 javascript互访(相互调用)的方法例子
- android 代码实现控件之间的间距
- android FragmentPagerAdapter的“标准”配置
- Android"解决"onTouch和onClick的冲突问题
- android:installLocation简析
- android searchView的关闭事件