From dfe5c204403bc56c29bb36410574eab8b1950417 Mon Sep 17 00:00:00 2001
From: Scott Main Here's a brief summary of each resource type: You will often use an {@code R.id} integer to handle {@link android.view.View} objects in
-your UI. Although the {@code id} is a subclass of the {@code R} class, it is not considered a
-"resource" because it is not a reference to an externalized application resource. The {@code id}
-is simply a unique identifier that allows you to handle elements in your UI by instantiating
-objects with {@link android.app.Activity#findViewById(int) findViewById()}. For information about using {@code R.id} with your UI, see Declaring Layout.{@code R.id} Is Not a Resource
-
-
diff --git a/docs/html/guide/topics/resources/drawable-resource.jd b/docs/html/guide/topics/resources/drawable-resource.jd
index d8de16a42999..1e4cca7c171d 100644
--- a/docs/html/guide/topics/resources/drawable-resource.jd
+++ b/docs/html/guide/topics/resources/drawable-resource.jd
@@ -18,32 +18,50 @@ and draw on the screen. There are several different types of drawables:
Documentation for the {@link android.graphics.drawable.AnimationDrawable} resource -is in the Animation Resource document.
+Also see the Animation Resource document for how to +create an {@link android.graphics.drawable.AnimationDrawable}.
+ + + -A basic bitmap image. Android supports basic bitmap files in a few different formats: +
A bitmap image. Android supports bitmap files in a three formats: {@code .png} (preferred), {@code .jpg} (acceptable), {@code .gif} (discouraged).
+You can reference a bitmap file directly, using the filename as the resource ID, or create an +alias resource ID in XML.
+Note: Bitmap files may be automatically optimized with lossless
image compression by the aapt tool. For
example, a true-color PNG that does not require more than 256 colors may be converted to an 8-bit
@@ -52,11 +70,18 @@ memory. So be aware that the image binaries placed in this directory can change
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.
A bitmap file is a {@code .png}, {@code .jpg}, or {@code .gif} file. Android creates a {@link +android.graphics.drawable.Drawable} +resource for any of these files when you save them in the {@code res/drawable/} directory.
+res/drawable/filename.png ({@code .png}, {@code .jpg}, or {@code .gif})@[package:]drawable/filename
res/drawable/myimage.png, this layout XML will apply
+
+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" />
+ android:src="@drawable/myimage" />
-This application code will retrieve the image as a {@link +
The following application code retrieves the image as a {@link android.graphics.drawable.Drawable}:
Resources res = {@link android.content.Context#getResources()};
@@ -97,50 +123,218 @@ Drawable drawable = res.{@link android.content.res.Resources#getDrawable(int) ge
+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 {@code <bitmap>} element as a child of
+an {@code <item>} element. For
+example, when creating a state list or layer list,
+you can exclude the {@code android:drawable}
+attribute from an {@code <item>} element and nest a {@code <bitmap>} inside it
+that defines the drawable item.
+
+res/drawable/filename.xmlR.drawable.filename@[package:]drawable/filename
++<?xml version="1.0" encoding="utf-8"?> +<bitmap + xmlns:android="http://schemas.android.com/apk/res/android" + android:src="@[package:]drawable/drawable_resource" + android:antialias=["true" | "false"] + android:dither=["true" | "false"] + android:filter=["true" | "false"] + android:gravity=["top" | "bottom" | "left" | "right" | "center_vertical" | + "fill_vertical" | "center_horizontal" | "fill_horizontal" | + "center" | "fill" | "clip_vertical" | "clip_horizontal"] + android:tileMode=["disabled" | "clamp" | "repeat" | "mirror"] /> ++
+<?xml version="1.0" encoding="utf-8"?> +<bitmap xmlns:android="http://schemas.android.com/apk/res/android" + android:src="@drawable/icon" + android:tileMode="repeat" /> ++ +
A {@link android.graphics.NinePatch} is a PNG image in which you can define stretchable regions -that Android will scale when content within the View exceeds the normal image bounds. You will +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 {@code "wrap_content"}, and when the View grows to accomodate the content, the Nine-Patch image -will also be scaled to match the size of the View. An example use of a Nine-Patch image is the +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 {@link android.widget.Button} widget, which must stretch to accommodate the text (or image) inside the button.
-For a complete discussion about how to define a Nine-Patch file with stretchable regions, +
Same as with a normal bitmap, you can reference a Nine-Patch file directly +or from a resource defined by XML.
+ +For a complete discussion about how to create a Nine-Patch file with stretchable regions, see the 2D Graphics document.
+ +res/drawable/filename.9.pngR.drawable.filename@[package:]drawable/filename
res/drawable/myninepatch.9.png, this layout XML will
-apply the Nine-Patch to a View:
+
+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" />
+ android:background="@drawable/myninepatch" />
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.
+ +res/drawable/filename.xmlR.drawable.filename@[package:]drawable/filename
++<?xml version="1.0" encoding="utf-8"?> +<nine-patch + xmlns:android="http://schemas.android.com/apk/res/android" + android:src="@[package:]drawable/drawable_resource" + android:dither=["true" | "false"] /> ++
+<?xml version="1.0" encoding="utf-8"?> +<nine-patch xmlns:android="http://schemas.android.com/apk/res/android" + android:src="@drawable/myninepatch" + android:dither="false" /> ++
A {@link android.graphics.drawable.LayerDrawable} is a drawable object +that manages an array of other drawables. Each drawable in the list is drawn in the order of the +list—the last drawable in the list is drawn on top.
+ +Each drawable is represented by an {@code <item>} element inside a single {@code +<layer-list>} element.
+ +res/drawable/filename.xmlR.drawable.filename@[package:]drawable/filename
++<?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" /> +</selector> ++
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 {@code <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:
+
+You can describe the state list in an XML file. Each graphic is represented by an {@code <item>} element inside a single {@code <selector>} element. Each {@code <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 will be used—the selection is not based on the "best +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.
res/drawable/filename.xmlR.drawable.filename@[package:]drawable/filename
<?xml version="1.0" encoding="utf-8"?>
@@ -212,6 +641,7 @@ In XML: @[package:]drawable/filename
Note:Remember that the first item in the state list that -matches the current state of the object will be applied. So if the first item in the list contains -none of the state attributes above, then it will be applied every time, which is why your +
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).
res/drawable/button.xml:
<?xml version="1.0" encoding="utf-8"?> @@ -292,12 +723,12 @@ default value should always be last (as demonstrated in the following example).< </selector>-
This layout XML will apply the drawable to a View:
+This layout XML applies the drawable to a View:
<ImageView
android:layout_height="wrap_content"
android:layout_width="wrap_content"
- android:src="@drawable/button" />
+ android:src="@drawable/button" />
This is a color defined in XML that's used as a drawable to fill a rectangular space, -with optionally rounded corners. This kind of drawable behaves like a color fill.
- -Note: A color drawable is a simple resource that is referenced -using the value provided in the {@code name} attribute (not the name of the XML file). As -such, you can combine a color drawable resources with other simple resources in the one XML file, -under one {@code <resources>} element.
- +A Drawable that manages a number of alternate Drawables, each assigned a maximum numerical +value. Setting the level value of the drawable with {@link +android.graphics.drawable.Drawable#setLevel(int) setLevel()} loads the drawable resource in the +level list that has a {@code android:maxLevel} value greater than or equal to the value +passed to the method.
res/drawable/filename.pngres/drawable/filename.xmlR.drawable.color_name@[package:]drawable/color_name
+In Java: R.drawable.filename@[package:]drawable/filename
-<?xml version="1.0" encoding="utf-8"?> -<resources> - <drawable - name="color_name" - >color</drawable> -</resources> +<?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>
res/drawable/colors.xml:
+
+<?xml version="1.0" encoding="utf-8"?> -<resources> - <drawable name="solid_red">#f00</drawable> - <drawable name="solid_blue">#0000ff</drawable> -</resources> --
This layout XML will apply a color drawable to a View:
--<TextView - android:layout_width="fill_parent" - android:layout_height="wrap_content" - android:background="@drawable/solid_blue" /> +<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 {@link android.view.View}, the level can be changed with {@link +android.graphics.drawable.Drawable#setLevel(int) setLevel()} or {@link +android.widget.ImageView#setImageLevel(int) setImageLevel()}.
+ +A {@link android.graphics.drawable.TransitionDrawable} is a drawable object +that can cross-fade between the two drawable resources.
+ +Each drawable is represented by an {@code <item>} element inside a single {@code +<transition>} element. No more than two items are supported. To transition forward, call +{@link android.graphics.drawable.TransitionDrawable#startTransition(int) startTransition()}. To +transition backward, call {@link android.graphics.drawable.TransitionDrawable#reverseTransition(int) +reverseTransition()}.
+ +res/drawable/filename.xmlR.drawable.filename@[package:]drawable/filename
++<?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" /> +</selector> ++
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" /> +</layer-list> ++ +
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);-
This application code will get a color drawable and apply it to a View:
-
-Resources res = {@link android.content.Context#getResources()};
-Drawable redDrawable = res.{@link android.content.res.Resources#getDrawable(int) getDrawable}(R.drawable.solid_red);
-TextView tv = (TextView) findViewByID(R.id.text);
-tv.setBackground(redDrawable);
-
A drawable defined in XML that insets another drawable by a specified distance. This is used when +a View needs a background that is smaller than the View's actual bounds.
+ +res/drawable/filename.xmlR.drawable.filename@[package:]drawable/filename
++<?xml version="1.0" encoding="utf-8"?> +<inset + xmlns:android="http://schemas.android.com/apk/res/android" + android:drawable="@drawable/drawable_resource" + android:insetTop="dimension" + android:insetRight="dimension" + android:insetBottom="dimension" + android:insetLeft="dimension" /> ++
+<?xml version="1.0" encoding="utf-8"?> +<inset xmlns:android="http://schemas.android.com/apk/res/android" + android:drawable="@drawable/background" + android:insetTop="10dp" + android:insetLeft="10dp" /> ++
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.
+ +res/drawable/filename.xmlR.drawable.filename@[package:]drawable/filename
++<?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"] /> ++
res/drawable/clip.xml:
++<?xml version="1.0" encoding="utf-8"?> +<shape xmlns:android="http://schemas.android.com/apk/res/android" + android:drawable="@drawable/android" + android:clipOrientation="horizontal" + android:gravity="left" /> +</shape> ++
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.
+A drawable defined in XML that changes the size of another drawable based on its current +level.
+ +res/drawable/filename.xmlR.drawable.filename@[package:]drawable/filename
++<?xml version="1.0" encoding="utf-8"?> +<scale + xmlns:android="http://schemas.android.com/apk/res/android" + android:drawable="@drawable/drawable_resource" + android:scaleGravity=["top" | "bottom" | "left" | "right" | "center_vertical" | + "fill_vertical" | "center_horizontal" | "fill_horizontal" | + "center" | "fill" | "clip_vertical" | "clip_horizontal"] + android:scaleHeight="percentage" + android:scaleWidth="percentage" /> ++
+<?xml version="1.0" encoding="utf-8"?> +<scale xmlns:android="http://schemas.android.com/apk/res/android" + android:drawable="@drawable/logo" + android:scaleGravity="center_vertical|center_horizontal" + android:scaleHeight="80%" + android:scaleWidth="80%" /> ++
This is a generic shape defined in XML.
@@ -441,23 +1408,32 @@ tv.setBackground(redDrawable);res/drawable/filename.xmlR.drawable.filename@[package:]drawable/filename
<?xml version="1.0" encoding="utf-8"?>
-<shape xmlns:android="http://schemas.android.com/apk/res/android"
+<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"
@@ -467,37 +1443,40 @@ In XML: @[package:]drawable/filename
android:gradientRadius="integer"
android:startColor="color"
android:type=["linear" | "radial" | "sweep"]
- android:usesLevel=["true" | "false"] />
+ android:usesLevel=["true" | "false"] />
+ <padding
+ android:left="integer"
+ android:top="integer"
+ android:right="integer"
+ android:bottom="integer" />
+ <size
+ android:width="integer"
+ android:color="color"
+ android:dashWidth="integer"
+ android:dashGap="integer" />
<solid
- android:color="color" />
+ android:color="color" />
<stroke
android:width="integer"
android:color="color"
android:dashWidth="integer"
- android:dashGap="integer" />
- <padding
- android:left="integer"
- android:top="integer"
- android:right="integer"
- android:bottom="integer" />
- <corners
- android:radius="integer"
- android:topLeftRadius="integer"
- android:topRightRadius="integer"
- android:bottomLeftRadius="integer"
- android:bottomRightRadius="integer" />
+ android:dashGap="integer" />
</shape>