【翻译】Drawable Resources

转载请注明出处:http://blog.csdn.net/kester_/article/details/52680197

参考地址:https://developer.android.com/guide/topics/resources/drawable-resource.html

Drawable Resources

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 and android:icon. There are several different types of drawables:
Drawable资源是一个对图像来说很普遍的概念,你可以通过像getDrawable(int)这种API来检索或者使用XML资源的android:drawable,android:icon属性来运用到屏幕上。有几种不同类型的Drawable:

  • Bitmap File
    A bitmap graphic file (.png, .jpg, or .gif). Creates a BitmapDrawable.
    一个位图文件(.png,.jpg,或者.gif)。创建一个BitmapDrawable。

  • Nine-Patch File
    A PNG file with stretchable regions to allow image resizing based on content (.9.png). Creates a NinePatchDrawable.
    一个可以伸展的PNG文件,能根据内容改变大小(.9.png)。创建一个NinePatchDrawable。

  • 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.
    一个管理其他一组Drawable的Drawable。按顺序被画出来,越后面的元素被画在越上面。创建一个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.
    一个在不同状态引用不同位图的XML文件(例如,在按钮点击的时候使用不同的图片)。创建一个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.
    定义一个管理交替的Drawable的Drawable的XML文件,每一个都指定了最大值。创建一个LevelListDrawable。

  • Transition Drawable
    An XML file that defines a drawable that can cross-fade between two drawable resources. Creates a TransitionDrawable.
    定义一个可以在两个Drawable资源淡入淡出的Drawable的XML文件。创建一个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.
    定义一个通过特定距离嵌入另一个Drawable的Drawable的XML文件。当一个View需要一个比View实际边界小的背景时很有用。

  • Clip Drawable
    An XML file that defines a drawable that clips another Drawable based on this Drawable’s current level value. Creates a ClipDrawable.
    定义一个基于Drawable当前等级值裁剪另一个Drawable的Drawable的XML文件。创建一个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
    定义一个基于当前等级值改变另一个Drawable大小的Drawable的XML文件。创建一个ScaleDrawable。

  • Shape Drawable
    An XML file that defines a geometric shape, including colors and gradients. Creates a ShapeDrawable.
    定义一个包括颜色和渐变的几何形状。创建一个ShpaeDrawable。

Also see the Animation Resource document for how to create an AnimationDrawable.
可以参考Animation Resource文档来了解如何创建一个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").

注意:一个颜色资源也可以使用在XML中的一个Drawable。例如,当创建一个状态列表Drawable时,你可以引用颜色
资源给android:drawable属性(android:drawable="@color/green")。

Bitmap

A bitmap image. Android supports bitmap files in three formats: .png (preferred), .jpg (acceptable), .gif (discouraged).
一个位图。Android支持三种格式的位图文件:.png(最好的),.jpg(可接受),.gif(不鼓励)。

You can reference a bitmap file directly, using the filename as the resource ID, or create an alias resource ID in XML.
你可以使用在XML里使用文件名作为资源ID,或者创建别名资源ID来直接引用位图文件。

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.

注意:位图文件可能会在编译阶段被自动无损压缩。例如,一个不需要256色的真彩PNG可能会被转换成8位PNG调色
板。这会使得一张相同质量的图片只要更少的内存。所以要意识到图片的二进制在编译时会改变。如果你打算以字节流
方式转换一张图片成位图,记得把图片放在res/raw文件夹,那里不会被优化掉。

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.
一个位图文件就是一个.png/.jpg/.gif文件。当你将这些文件保存在res/drawable目录时,Android会给它们创建一个Drawable资源。

  • FILE LOCATION:
    文件路径
    res/drawable/filename.png (.png, .jpg, or .gif)
    The filename is used as the resource ID.
    filename被用作资源ID。

  • COMPILED RESOURCE DATATYPE:
    编译之后的资源数据类型
    Resource pointer to a BitmapDrawable.
    资源指向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:
    布局文件应用图片(res/drawable/myimage.png)到一个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:
下面代码索引一个图片作为Drawable:

Resources res = getResources();
Drawable drawable = res.getDrawable(R.drawable.myimage);
  • SEE ALSO:
    2D Graphics
    BitmapDrawable

XML Bitmap

An XML bitmap is a resource defined in XML that points to a bitmap file. The effect is an alias for a raw bitmap file. The XML can specify additional properties for the bitmap such as dithering and tiling.
一个XML位图指的是一个定义在XML中指向位图文件的资源。它的作用是作为raw位图文件的一个别名。XML可以指定额外的属性给位图,例如dithering和tiling。

Note: You can use a <bitmap> element as a child of an <item> element. For example, when 
creating a state list or layer list, you can exclude the android:drawable attribute from 
an <item> element and nest a <bitmap> inside it that defines the drawable item.
注意:你可以使用<bitmap>元素作为<item>的一个孩子。例如,当创建一个状态列表或者图层列表时,你可以从
<item>元素中排除android:drawable属性,并且嵌套一个<bitmap>进去来定义drawable item
  • FILE LOCATION:
    文件路径
    res/drawable/filename.xml
    The filename is used as the resource ID.
    filename用作资源ID。

  • COMPILED RESOURCE DATATYPE:
    编译之后资源数据类型:
    Resource pointer to a BitmapDrawable.
    资源指向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:
    元素
<bitmap>
Defines the bitmap source and its properties.
定义位图来源和属性。

attributes:
属性:

xmlns:android
String. 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>.
字符串。定义XML命名空间,必须是"http://schemas.android.com/apk/res/android"。
只要<bitmap>是根元素就必须要,当<bitmap>嵌套进<item>时就不需要。


android:src
Drawable resource. Required. Reference to a drawable resource.
Drawable资源。必须的。引用Drawable资源。

android:antialias
Boolean. Enables or disables antialiasing.
布尔值。反锯齿开关。

android:dither
Boolean. 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).

布尔值。如果位图没有和屏幕同样像素的配置时抖动开关(例如:一个ARGB 8888位图配上RGB 565屏幕)。

android:filter
Boolean. Enables or disables bitmap filtering. Filtering is used when the bitmap is 
shrunk or stretched to smooth its apperance.

布尔值。位图过滤开关。当位图被放缩时过滤会被使用。

android:gravity
Keyword. 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:
必须是一个或多个(用'|'分隔)以下常量值:
ValueDescription
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可以让子控件上下裁剪到容器大小。裁剪是基于垂直重心的:top重心的话裁底部,bottom重心的话裁顶部,都不是的话两边都裁。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可以让子控件左右裁剪到容器大小。裁剪是基于水平重心的:left重心裁剪右边,right重心裁剪左边,都不是的话两边都裁。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:mipMap
Boolean. Enables or disables the mipmap hint. See setHasMipMap() for more information. Default value is false.
布尔值。mipmap提示开关。查看setHasMipMap()来获取更多信息。默认值false。

android:tileMode
Keyword. Defines the tile mode. When the tile mode is enabled, the bitmap is repeated. 
Gravity is ignored when the tile mode is enabled.

关键词。定义搬砖模式。当搬砖模式开启时,位图会重复。当搬砖模式开启时Gravity属性会被忽略。
Must be one of the following constant values:
必须是以下一个常量:
ValueDescription
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.

EXAMPLE:

<?xml version="1.0" encoding="utf-8"?>
<bitmap xmlns:android="http://schemas.android.com/apk/res/android"
    android:src="@drawable/icon"
    android:tileMode="repeat" />
  • SEE ALSO:
    BitmapDrawable
    Creating alias resources

Nine-Patch

A NinePatch is a PNG image in which you can define stretchable regions that Android scales when content within the View exceeds the normal image bounds. You typically assign this type of image as the background of a View that has at least one dimension set to “wrap_content”, and when the View grows to accomodate the content, the Nine-Patch image is also scaled to match the size of the View. An example use of a Nine-Patch image is the background used by Android’s standard Button widget, which must stretch to accommodate the text (or image) inside the button.
点九图是一种你可以定义在拉伸区域的PNG图片,Android放大时当View里的内容超过普通图片边界时会进行拉伸。你可以这种类型的图片作为View的背景,这个View的尺寸至少有一个设置成”wrap_content”,当View拉伸适配内容时,点九图会放大来匹配View的大小。一个使用点九图的例子是作为Android标准按钮控件的背景,它必须在按钮内拉伸来适配文字(或图片)。

Same as with a normal bitmap, you can reference a Nine-Patch file directly or from a resource defined by XML.
和普通位图一样,你可以通过XML定义的资源直接引用点九图文件。

For a complete discussion about how to create a Nine-Patch file with stretchable regions, see the 2D Graphics document.
查看2D Graphics文档来获得更多关于如何创建一个点九图文件的可拉伸区域。


Nine-Patch File

  • FILE LOCATION:
    文件路径
    res/drawable/filename.9.png
    The filename is used as the resource ID.
    filename是用作资源ID的。

  • COMPILED RESOURCE DATATYPE:
    编译后的资源数据类型:
    Resource pointer to a NinePatchDrawable.
    资源指向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:
    这个布局文件应用点九图到一个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.
一个XML点九是定义在XML中的指向点九文件的资源。XML可以给图片指定抖动。

  • FILE LOCATION:
    文件路径
    res/drawable/filename.xml
    The filename is used as the resource ID.
    filename用作资源ID。

  • COMPILED RESOURCE DATATYPE:
    编译后的资源数据类型:
    Resource pointer to a NinePatchDrawable.
    资源指向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:
    元素
<nine-patch>
Defines the Nine-Patch source and its properties.
定义点九图资源和属性。

attributes:
属性:

xmlns:android
String. Required. Defines the XML namespace, which must be "http://schemas.android.com/apk/res/android".
字符串。必须的。定义XML命名空间,必须是"http://schemas.android.com/apk/res/android"。

android:src
Drawable resource. Required. Reference to a Nine-Patch file.
Drawable资源。必须的。引用点九文件。

android:dither
Boolean. 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).
布尔值。当位图和屏幕有不同像素配置时的抖动开关(例如:一个ARGB 8888位图和一个RGB 565屏幕)。
  • 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.
LayerDrawable是一个管理一组其他Drawable的Drawable对象。越排后面的Drawable画在越上面。

Each drawable is represented by an <item> element inside a single <layer-list> element.
每一个Drawable被一个嵌在<layer-list> 里的<item>代表。

  • FILE LOCATION:
    文件路径:
    res/drawable/filename.xml
    The filename is used as the resource ID.
    filename用作资源ID。

  • COMPILED RESOURCE DATATYPE:
    编译后资源数据类型:
    Resource pointer to a LayerDrawable.
    资源指向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:
    元素
<layer-list>
Required. This must be the root element. Contains one or more <item> elements.
必须的。必须是根元素。包含一个或多个<item>元素。

attributes:
属性:

xmlns:android
String. Required. Defines the XML namespace, which must be 
"http://schemas.android.com/apk/res/android".
字符串。必须的。定义XML命名空间,必须是"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.

定义一个放在图层Drawable里的Drawable,在它的属性里定义了位置。必须是<selector>元素里的孩子。接受孩子
<bitmap>元素。

attributes:
属性:

android:drawable
Drawable resource. Required. Reference to a drawable resource.
Drawable资源。必须的。引用Drawable资源。

android:id
Resource 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().
资源ID.这个Drawable唯一的资源ID。为了给这个item创建一个新的资源ID,使用格式"@+id/name"。加号暗示着
这应该创建一个新的ID。你可以使用这个标识符来检索和修改Drawable,通过View.findViewById()或者
Activity.findViewById()。


android:top
Integer. The top offset in pixels.
整数。向上偏移的像素。

android:right
Integer. The right offset in pixels.
整数。向右偏移的像素。

android:bottom
Integer. The bottom offset in pixels.
整数。向下偏移的像素。

android:left
Integer. 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:
所有Drawable item都会默认放大来适配包含它的View。因此,把你的图片放在layer list的不同位置可能会增加View的大小,并且合适地放大一些图片。为了避免放大列表里的item,在<item> 里使用<bitmap> 元素来指定Drawable并且给不放大的东西定义重心,例如“center”。举个例子,下面的<item>定义了一个放大来适配容器View的item。

<item android:drawable="@drawable/image" />

To avoid scaling, the following example uses a <bitmap> element with centered gravity:
为了避免放大,下面的例子使用<bitmap> 元素定义居中的重心:

<item>
  <bitmap android:src="@drawable/image"
          android:gravity="center" />
</item>
  • EXAMPLE:
    例子:
    XML file saved at res/drawable/layers.xml:
    保存在res/drawable/layers.xml的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.
需要注意的是这个例子使用嵌套的<bitmap>元素来定义Drawable资源,每个item都使用的”center”重心。这个确保了没有图片会因为图片偏移改变大小而被放大来适配容器。

This layout XML applies the drawable to a View:
这个XML布局使用Drawable方式运用到一个View:

<ImageView
    android:layout_height="wrap_content"
    android:layout_width="wrap_content"
    android:src="@drawable/layers" />

The result is a stack of increasingly offset images:
这个是堆叠递增偏移图片的结果:

这里写图片描述

  • SEE ALSO:
    LayerDrawable

State List

A StateListDrawable is a drawable object defined in XML that uses a several different images to represent the same graphic, depending on the state of the object. For example, a Button widget can exist in one of several different states (pressed, focused, or neither) and, using a state list drawable, you can provide a different background image for each state.
StateListDrawable是在XML里定义的使用几个不同的图片来代表同一个图片的Drawable对象,这个依赖于对象的状态。举个例子,一个按钮控件可以是几个不同的状态之一(点击,聚焦,或都不是)并且,使用状态列表Drawable,你可以给每个状态提供一个不同的背景图片。

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.
你可以在XML文件声明状态列表。每一个图片都被一个在<slector> 里的<item>代表。每一个<item> 使用不同的属性来描述状态。

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.
    filename代表资源ID。

  • COMPILED RESOURCE DATATYPE:
    编译后资源数据类型:
    Resource pointer to a StateListDrawable.
    资源指向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:
    元素
<selector>
Required. This must be the root element. Contains one or more <item> elements.
必须的。必须是根元素。包含一个或多个<item>元素。

attributes:
属性:

xmlns:android
String. Required. Defines the XML namespace, which must be "http://schemas.android.com/apk/res/android".
字符串。必须的。定义XML命名空间,必须是"http://schemas.android.com/apk/res/android"。

android:constantSize
Boolean. "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.
布尔值。"true"表示Drawable在不同状态之间切换大小不变(以最大值为准);"false"表示Drawable的大小会不
同。默认false。

android:dither
Boolean. "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.
布尔值。抖动开关,当位图和屏幕像素配置不同时是否需要抖动。默认true。

android:variablePadding
Boolean. "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.
布尔值。如果基于当前选择的状态的Drawable的padding需要改变的话,使用"true";如果保持不变的话使
用"false"(以最大值为准).打开这个特性的话要求你在状态改变时处理执行layout,一般来说不支持。默认
false。

<item>
Defines a drawable to use during certain states, as described by its attributes. Must be 
a child of a <selector> element.
定义一个Drawable来在合适的状态使用,由它的属性描述决定。必须是一个<selector>元素的孩子。

attributes:
属性:

android:drawable
Drawable resource. Required. Reference to a drawable resource.
Drawable资源。必须的。引用一个Drawable资源。

android:state_pressed
Boolean. "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.
布尔值。当对象被按下的时候使用"true"(例如当一个按钮被触发/点击);默认不按下情况使用"false"。

android:state_focused
Boolean. "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.
布尔值。当对象有输入焦点时使用"true"(例如当用户选择一个文字输入);默认没有焦点时使用"false"。

android:state_hovered
Boolean. "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.
布尔值。被鼠标盘旋时使用"true";默认没盘旋时使用"false"。通常情况下,这个和"focused"相同。
在API 14引入。

android:state_selected
Boolean. "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).
布尔值。当使用定向控制(例如方向键)选择中时“true”,没选中时“false”。当
focus(android:state_focused)没被使用时选择状态会被使用。

android:state_checkable
Boolean. "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.)
布尔值。当对象可选中时“true”,不可选中时“false”(只有对象在变成可选中或不可选中时有用)。

android:state_checked
Boolean. "true" if this item should be used when the object is checked; "false" if it 
should be used when the object is un-checked.
布尔值。当对象被选中时“true”,未被选中时“false”。

android:state_enabled
Boolean. "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.
布尔值。当对象是能够使用时“true”(可接收触碰/点击事件);当对象不能使用时“false”。

android:state_activated
Boolean. "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.
布尔值。当对象被激活成持久选中时“true”(例如在一个持久导航的View里“强调”之前选中的列表item);当对象没
激活时“false”。
在API 11引入。

android:state_window_focused
Boolean. "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).
布尔值。当应用窗口拥有焦点时“true”(应用在前台),当应用窗口没有焦点时“false”(举个例子,通知栏阴影被拉
下或者一个对话框出现)。
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).

注意:需要注意的是Android使用状态列表里的第一个匹配对象当前状态的item。所以,如果列表里第一个item不包
含以上任何属性,它每次都会被应用,这就是为什么你的默认值要放在最后面(正如下面的例子演示)。
  • EXAMPLE:
    例子:
    XML file saved at res/drawable/button.xml:
    XML文件保存在res/drawable/button.xml:
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
    <item android:state_pressed="true"
          android:drawable="@drawable/button_pressed" /> <!-- pressed -->
    <item android:state_focused="true"
          android:drawable="@drawable/button_focused" /> <!-- focused -->
    <item android:state_hovered="true"
          android:drawable="@drawable/button_focused" /> <!-- hovered -->
    <item android:drawable="@drawable/button_normal" /> <!-- default -->
</selector>

This layout XML applies the state list drawable to a Button:

<Button
    android:layout_height="wrap_content"
    android:layout_width="wrap_content"
    android:background="@drawable/button" />
  • SEE ALSO:
    StateListDrawable

Level List

A Drawable that manages a number of alternate Drawables, each assigned a maximum numerical
value. Setting the level value of the drawable with setLevel() loads the drawable resource in the level list that has a android:maxLevel value greater than or equal to the value passed to the method.
Level List是一个管理若干交替的Drawable的Drawable,每一个指定了一个最大值。通过level list里android:maxLevel值设置最大值。Drawable的level、minLevel、maxLevel匹配第一个满足条件的item。

  • FILE LOCATION:
    文件路径
    res/drawable/filename.xml
    The filename is used as the resource ID.
    filename用作资源ID。

  • COMPILED RESOURCE DATATYPE:
    编译后资源数据类型:
    Resource pointer to a LevelListDrawable.
    资源指向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:
    元素
<level-list>
This must be the root element. Contains one or more <item> elements.
必须是根元素。包含一个或多个<item>元素。

attributes:
属性:

xmlns:android
String. Required. Defines the XML namespace, which must be "http://schemas.android.com/apk/res/android".
字符串。必须的。定义XML命名空间,必须是"http://schemas.android.com/apk/res/android"。

<item>
Defines a drawable to use at a certain level.
定义一个Drawable在合适的等级使用。

attributes:
属性:

android:drawable
Drawable resource. Required. Reference to a drawable resource to be inset.
Drawable资源。必须的。引用一个Drawable资源来被嵌入。

android:maxLevel
Integer. The maximum level allowed for this item.
整数。这个item允许的最大等级值。

android:minLevel
Integer. The minimum level allowed for this item.
整数。这个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().
一旦这个应用到一个View中,等级可以通过setLevel()或者setImageLevel()改变。

  • SEE ALSO:
    LevelListDrawable

Transition Drawable

A TransitionDrawable is a drawable object that can cross-fade between the two drawable resources.
TransitionDrawable是可以在两个Drawable资源间淡入淡出的Drawable对象。

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().
每一个Drawable被一个<transition> 里的<item>元素代替。不支持超过两个item。可以调用reverseTransition()来往回过渡。

  • FILE LOCATION:
    文件路径:
    res/drawable/filename.xml
    The filename is used as the resource ID.
    filename作为资源ID。

  • COMPILED RESOURCE DATATYPE:
    编译后的资源数据类型:
    Resource pointer to a TransitionDrawable.
    资源指向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:
    元素:
<transition>
Required. This must be the root element. Contains one or more <item> elements.
必须的。必须是根元素。包含一个或多个<item>元素。

attributes:
属性:

xmlns:android
String. Required. Defines the XML namespace, which must be 
"http://schemas.android.com/apk/res/android".
字符串。必须的。定义了XML命名空间,必须是"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.
定义了一个Drawable作为Drawable过渡的一部分。必须是<transition>元素的孩子。接受<bitmap>元素。

attributes:
属性:

android:drawable
Drawable resource. Required. Reference to a drawable resource.
Drawable资源。必须的。引用Drawable资源。

android:id
Resource 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().
资源ID。Drawable唯一的资源ID。为了给这个item创建一个新的资源ID,使用"@+id/name"格式。加号表明了创建
一个新的ID。你可以使用这个标识符通过View.findViewById()或者Activity.findViewById()来检索和修改这
个Drawable。

android:top
Integer. The top offset in pixels.
整数。上部偏移像素。

android:right
Integer. The right offset in pixels.
整数。右边偏移像素。

android:bottom
Integer. The bottom offset in pixels.
整数。下部偏移像素。

android:left
Integer. The left offset in pixels.
整数。左部偏移像素。
  • EXAMPLE:
    例子:
    XML file saved at res/drawable/transition.xml:
    XML文件保存在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:
XML布局文件应用Drawable到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:
下面的代码执行了从第一个item到第二个item500ms的过渡:

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.
Inset Drawable是一个定义在XML通过指定距离嵌入到另一个Drawable的Drawable。当一个View需要一个比View实际边界小的背景时很有用。

FILE LOCATION:
文件路径:
res/drawable/filename.xml
The filename is used as the resource ID.
filename用作资源ID。

COMPILED RESOURCE DATATYPE:
编译后的资源数据类型:
Resource pointer to a InsetDrawable.
资源指向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:
元素:

<inset>
Defines the inset drawable. This must be the root element.
定义inset drawable。必须是根元素。

attributes:
属性:

xmlns:android
String. Required. Defines the XML namespace, which must be "http://schemas.android.com/apk/res/android".
字符串。必须的。定义XML命名空间,必须是"http://schemas.android.com/apk/res/android"。

android:drawable
Drawable resource. Required. Reference to a drawable resource to be inset.
Drawable资源。必须的。引用Drawable资源来被嵌入。

android:insetTop
Dimension. The top inset, as a dimension value or dimension resource
尺寸。上部嵌入,可以使用一个尺寸值或者尺寸资源。

android:insetRight
Dimension. The right inset, as a dimension value or dimension resource
尺寸。右边嵌入,可以使用一个尺寸值或者尺寸资源。

android:insetBottom
Dimension. The bottom inset, as a dimension value or dimension resource
尺寸。下部嵌入,可以使用一个尺寸值或者尺寸资源。

android:insetLeft
Dimension. 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 gravity to control where it is placed in its overall container. Most often used to implement things like progress bars.
Clip Drawable是一个定义在XML中基于当前Drawable等级值修剪另一个Drawable的Drawable。你可以基于等级值来控制修剪Drawable宽度和高度,就像用重心来控制放置在容器的位置。常常用来实现像进度条一样的东西。

FILE LOCATION:
文件路径
res/drawable/filename.xml
The filename is used as the resource ID.
filename作为资源ID。

COMPILED RESOURCE DATATYPE:
编译后的资源数据类型:
Resource pointer to a ClipDrawable.
资源指向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:
元素:

<clip>
Defines the clip drawable. This must be the root element.
定义clip drawable。必须是根元素。

attributes:
元素:

xmlns:android
String. Required. Defines the XML namespace, which must be "http://schemas.android.com/apk/res/android".
字符串。必须的。定义XML命名空间,必须是"http://schemas.android.com/apk/res/android"。

android:drawable
Drawable resource. Required. Reference to a drawable resource to be clipped.
Drawable资源。必须的。引用需要被裁减的资源。

android:clipOrientation
Keyword. The orientation for the clip.
Must be one of the following constant values:
关键词。裁减方向。必须是以下其中一个常量值:
ValueDescription
horizontal水平裁减。Clip the drawable horizontally.
vertical垂直裁减。Clip the drawable vertically.
android:gravity
Keyword. Specifies where to clip within the drawable.
Must be one or more (separated by '|') of the following constant values:
关键词。指定Drawable哪里进行裁减。必须是以下一个或多个(用“|”分割)常量值:
ValueDescription
top把对象放在容器顶部,不改变大小。当clipOrientation是“vertical”时,裁减发生在Drawable底部。Put the object at the top of its container, not changing its size. When clipOrientation is “vertical”, clipping occurs at the bottom of the drawable.
bottom把对象放在容器底部,不改变大小。当clipOrientation是“vertical”时,裁减发生在Drawable顶部。Put the object at the bottom of its container, not changing its size. When clipOrientation is “vertical”, clipping occurs at the top of the drawable.
left把对象放在容器左边,不改变大小。默认值。当clipOrientation是“horizontal”时,裁减发生在Drawable右边。这是默认值。Put the object at the left edge of its container, not changing its size. This is the default. When clipOrientation is “horizontal”, clipping occurs at the right side of the drawable. This is the default.
right把对象放在容器右边,不改变大小。当clipOrientation是“horizontal”时,裁减发生在Drawable左边。Put the object at the right edge of its container, not changing its size. When clipOrientation is “horizontal”, clipping occurs at the left side of the drawable.
center_vertical把对象放在容器垂直居中位置,不改变大小。裁减行为和gravity是“center”一样。Place object in the vertical center of its container, not changing its size. Clipping behaves the same as when gravity is “center”.
fill_vertical依靠增加垂直大小来完全填充容器。当clipOrientation是“vertical”时,不进行裁减因为Drawable填充了垂直空间(除非Drawable等级值是0,不可见)。Grow the vertical size of the object if needed so it completely fills its container. When clipOrientation is “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把对象放在容器水平居中位置,不改变大小。裁减行为和gravity是“center”一样。Place object in the horizontal center of its container, not changing its size. Clipping behaves the same as when gravity is “center”.
fill_horizontal依靠增加水平大小来完全填充容器。当clipOrientation是“horizontal”时,不进行裁减因为Drawable填充了水平空间(除非Drawable等级值是0,不可见)。Grow the horizontal size of the object if needed so it completely fills its container. When clipOrientation is “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把对象放在容器水平和垂直居中位置,不改变大小。当clipOrientation是“horizontal”时,左右裁减。当clipOrientation是“vertical”时,上下裁减。Place the object in the center of its container in both the vertical and horizontal axis, not changing its size. When clipOrientation is “horizontal”, clipping occurs on the left and right. When clipOrientation is “vertical”, clipping occurs on the top and bottom.
fill水平和垂直增加大小来填充容器。不会裁减因为Drawable填充了水平和垂直空间(除非Drawable等级值是0,不可见)。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额外的选项,可以设置来让孩子上和/或下裁减到容器的边界。裁减是基于垂直重心:gravity是top时裁减底部,gravity是bottom时裁减顶部,都不是时顶部底部都裁减。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额外的选项,可以设置来让孩子左和/或右裁减到容器的边界。裁减是基于水平重心:gravity是left时裁减右边,gravity是right时裁减左边,都不是时左边右边都裁减。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.

EXAMPLE:
例子:
XML file saved at res/drawable/clip.xml:
XML文件保存在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:
下面的XML布局文件应用来裁减一个View的Drawable:

<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:
下面的代码获得Drawable并且增加裁减总数来渐进地显示图片:

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:
增加等级值降低了裁减量并且慢慢地显示了图片。这里是等级值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.
注意:默认等级值是0,表示完全裁减图片不可见。当等级值是10000时,图片没有裁减完全可见。

SEE ALSO:
ClipDrawable


Scale Drawable

A drawable defined in XML that changes the size of another drawable based on its current level.
Scale Drawable是定义在XML基于当前等级值来改变另一个Drawable的大小的Drawable。

FILE LOCATION:
文件路径:
res/drawable/filename.xml
The filename is used as the resource ID.

COMPILED RESOURCE DATATYPE:
编译后的资源数据类型:
Resource pointer to a ScaleDrawable.
资源指向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:
元素:

<scale>
Defines the scale drawable. This must be the root element.
定义scale drawable。必须是根元素。

attributes:
属性:

xmlns:android
String. Required. Defines the XML namespace, which must be "http://schemas.android.com/apk/res/android".
字符串。必须的。定义XML命名空间,必须是"http://schemas.android.com/apk/res/android"。

android:drawable
Drawable resource. Required. Reference to a drawable resource.
Drawable资源。必须的。引用Drawable资源。

android:scaleGravity
Keyword. Specifies the gravity position after scaling.
Must be one or more (separated by '|') of the following constant values:
关键词。指定放缩后的重心位置。必须是一个或多个(用“|”分隔)以下常量值:
ValueDescription
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额外的选择来让孩子的顶部和/或底部裁剪到容器的边界。裁剪是基于垂直重心:gravity是top时裁剪底部,gravity是bottom时裁剪顶部,都不是时两边裁剪。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额外的选择来让孩子的左部和/或右边裁剪到容器的边界。裁剪是基于水平重心:gravity是left时裁剪右部,gravity是right时裁剪左部,都不是时两边裁剪。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:scaleHeight
Percentage. The scale height, expressed as a percentage of the drawable's bound. The 
value's format is XX%. For instance: 100%, 12.5%, etc.
百分数。放缩高度,表示成Drawable边界的百分数。值的格式是 XX%。举个例子:100%,12.5%,等等。

android:scaleWidth
Percentage. The scale width, expressed as a percentage of the drawable's bound. The 
value's format is XX%. For instance: 100%, 12.5%, etc.
百分数。放缩宽度,表示成Drawable边界的百分数。值的格式是 XX%。举个例子:100%,12.5%,等等。

EXAMPLE:
例子:

<?xml version="1.0" encoding="utf-8"?>
<scale xmlns:android="http://schemas.android.com/apk/res/android"
    android:drawable="@drawable/logo"
    android:scaleGravity="center_vertical|center_horizontal"
    android:scaleHeight="80%"
    android:scaleWidth="80%" />

SEE ALSO:
ScaleDrawable


Shape Drawable

This is a generic shape defined in XML.
这个定义在XML的基本的形状。

FILE LOCATION:
文件路径:
res/drawable/filename.xml
The filename is used as the resource ID.
filename用作资源ID。

COMPILED RESOURCE DATATYPE:
编译后的资源数据类型:
Resource pointer to a GradientDrawable.
资源指向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="float"
        android:centerY="float"
        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.
shape drawable。必须是根元素。

attributes:
属性:

xmlns:android
String. Required. Defines the XML namespace, which must be 
"http://schemas.android.com/apk/res/android".
字符串。必须的。定义XML命名空间,必须是"http://schemas.android.com/apk/res/android"。

android:shape
Keyword. Defines the type of shape. Valid values are:
关键词。定义shape类型。有效值是:
ValueDesciption
“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”线,要求<stroke>定义线宽。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.
The following attributes are used only when android:shape="ring":
下面的属性只有在android:shape="ring"时才有效:

android:innerRadius
Dimension. The radius for the inner part of the ring (the hole in the middle), as a 
dimension value or dimension resource.
尺寸。环的内半径(中间的洞),作为尺寸值或尺寸资源。

android:innerRadiusRatio
Float. 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:innerRadiusRatio="5",表示内半径等于环的宽度
除以5.这个值会被android:innerRadius覆盖。默认值是9.

android:thickness
Dimension. The thickness of the ring, as a dimension value or dimension resource.
尺寸。环的厚度,作为尺寸值或者尺寸资源。

android:thicknessRatio
Float. 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:thicknessRatio="2",表示厚度等于环的宽度除以2.
值会被android:innerRadius覆盖。默认值是3。

android:useLevel
Boolean. "true" if this is used as a LevelListDrawable. This should normally be "false" 
or your shape may not appear.
布尔值。如果使用LevelListDrawable的话值为"true"。一般使用"false"否则你的形状可能不出来。

<corners>
Creates rounded corners for the shape. Applies only when the shape is a rectangle.
创建圆角形状。只有当形状是矩形时应用。

attributes:
属性:

android:radius
Dimension. The radius for all corners, as a dimension value or dimension resource. This 
is overridden for each corner by the following attributes.
尺寸。所有角的半径,作为尺寸值或者尺寸资源。会被每一个角的以下的属性覆盖。

android:topLeftRadius
Dimension. The radius for the top-left corner, as a dimension value or dimension 
resource.
尺寸。左上角半径,作为尺寸值或者尺寸资源。

android:topRightRadius
Dimension. The radius for the top-right corner, as a dimension value or dimension 
resource.
尺寸。右上角半径,作为尺寸值或者尺寸资源。

android:bottomLeftRadius
Dimension. The radius for the bottom-left corner, as a dimension value or dimension 
resource.
尺寸。左下角半径,作为尺寸值或者尺寸资源。

android:bottomRightRadius
Dimension. 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:radius to 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.
注意:每个角必须(最初)提供一个大于1的半径,或者没有一个角是圆角。如果你想要指定某个角不圆角,一个变通方
案是使用android:radius来设置一个默认的角半径大于1,之后使用你想要的值覆盖每一个角,不想圆角的地方使用
0
<gradient>
Specifies a gradient color for the shape.
指定形状的渐变颜色。

attributes:
属性:

android:angle
Integer. 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.
证书。渐变角度。0是从左到右,90是从下到上。必须是45的倍数,默认是0.

android:centerX
Float. The relative X-position for the center of the gradient (0 - 1.0).
浮点型。渐变中心的相对X坐标(0-1.0)。

android:centerY
Float. The relative Y-position for the center of the gradient (0 - 1.0).
浮点型。渐变中心的相对Y坐标(0-1.0)。

android:centerColor
Color. Optional color that comes between the start and end colors, as a hexadecimal 
value or color resource.
颜色。开始到结束的颜色中间颜色,作为一个16进制值或者颜色资源。

android:endColor
Color. The ending color, as a hexadecimal value or color resource.
颜色。结束颜色,作为一个16进制值或者颜色资源。

android:gradientRadius
Float. The radius for the gradient. Only applied when android:type="radial".
浮点型。渐变半径。只有当android:type="radial"时应用。

android:startColor
Color. The starting color, as a hexadecimal value or color resource.
颜色。开始颜色,作为一个16进制值或者颜色资源。

android:type
Keyword. The type of gradient pattern to apply. Valid values are:
关键词。渐变模式类型。有效值是:
ValueDescription
“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:useLevel
Boolean. "true" if this is used as a LevelListDrawable.
布尔值。如果使用LevelListDrawable的话为"true"。

<padding>
Padding to apply to the containing View element (this pads the position of the View content, not the shape).
View元素的填充(填补View的内容,而不是形状)。

attributes:
属性:

android:left
Dimension. Left padding, as a dimension value or dimension resource.
尺寸。左填充,作为尺寸值或者尺寸资源。

android:top
Dimension. Top padding, as a dimension value or dimension resource.
尺寸。上填充,作为尺寸值或者尺寸资源。

android:right
Dimension. Right padding, as a dimension value or dimension resource.
尺寸。右填充,作为尺寸值或者尺寸资源。

android:bottom
Dimension. Bottom padding, as a dimension value or dimension resource.
尺寸。下填充,作为尺寸值或者尺寸资源。

<size>
The size of the shape.
形状的大小。

attributes:
属性:

android:height
Dimension. The height of the shape, as a dimension value or dimension resource.
尺寸。形状的高度,作为尺寸值或者尺寸资源。

android:width
Dimension. 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:scaleType to "center".
注意:默认在这里定义形状放缩适配容器View的尺寸。当你在ImageView使用形状时,你可以通过
android:scaleType的"center"限制放缩。
<solid>
A solid color to fill the shape.
填充形状的颜色。

attributes:
属性:

android:color
Color. The color to apply to the shape, as a hexadecimal value or color resource.
颜色。应用到形状的颜色,作为一个16进制值或者颜色资源。

<stroke>
A stroke line for the shape.
形状的画线。

attributes:
属性:

android:width
Dimension. The thickness of the line, as a dimension value or dimension resource.
尺寸。线的厚度,作为尺寸值或者尺寸资源。

android:color
Color. The color of the line, as a hexadecimal value or color resource.
颜色。线的颜色,作为16进制值或者颜色资源。

android:dashGap
Dimension. The distance between line dashes, as a dimension value or dimension resource. 
Only valid if android:dashWidth is set.
尺寸。线之间破折距离,作为尺寸值或者尺寸资源。只有当android:dashWidth设置时有效。

android:dashWidth
Dimension. The size of each dash line, as a dimension value or dimension resource. Only 
valid if android:dashGap is set.
尺寸。每一个破折线大小,作为尺寸值或者尺寸资源。只有android:dashGap设置时有效。

EXAMPLE:
例子:
XML file saved at res/drawable/gradient_box.xml:
XML文件保存在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:
这个布局文件形状到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:
这个应用代码获取shape drawable并应用到View:

Resources res = getResources();
Drawable shape = res. getDrawable(R.drawable.gradient_box);

TextView tv = (TextView)findViewByID(R.id.textview);
tv.setBackground(shape);

SEE ALSO:
ShapeDrawable

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值