Android Application resources-Drawable (1)

Source: Internet
Author: User

A drawn resource is a general concept of a graph that can be drawn to the screen. It can be obtained by using APIs such as getDrawable (int) or using android: the drawable and android: icon attributes apply them to another XML resource. There are several different types of plotting resources:

Bitmap file:

Map graphics file (.png).jpgor .gif ). Each bitmap resource creates a BitmapDrawable object.

Nine-Patch file:

The nine-patchfile is a PNG file with extensibility. It allows you to adjust the size of an image based on the content (.9.png ). Each Nine-Patch resource creates a NinePatchDrawable object.

Layer list:

Layer list manages a resource array. They are drawn in the order of arrays. Therefore, the largest element of the index is drawn at the top. A layer list creates a LayerDrawable object.

Status List:

The status list is an XML file that defines different bitmap images to be referenced for different States (for example, different images to be used when a button is pressed ). A status list creates a StateListDrawable object.

Level list:

A level list is an XML file that defines alternative resource creation. Each resource is assigned with the largest numeric value. A State List creates a LevelListDrawable object.

Transition drawing resources:

It is an XML file that defines the drawing resources. The drawing resources in the file can gradually get in and out between the two drawing resources. An XML file creates a TransitionDrawable object.

Illustration drawing resources:

It is an XML file that defines the drawing resources. The drawing resources in the file can be inserted to another painting resource at a specified distance. This resource is useful when the View object needs to draw a background that is less than the actual border of the View.

Editing and drawing resources:

It is an XML file that defines the resource to be drawn. It uses the definition in the file to edit another resource based on the current value of the resource to be drawn. It creates a ClipDrawable object.

Scale and draw resources:

It is an XML file that defines the resource to be drawn. The definition in this file will change the size of another resource based on the current level value. It creates a ScaleDrawable object.

Shape drawing resources:

It is an XML file that defines ry, including color and ry. It creates a ShapeDrawable object.

Note: In XML, color resources can also be used to draw resources. For example, when creating a status list to draw resources, the android: drawable attribute can reference a color Resource (for example, android: drawable = "@ color/green ").

Bitmap

Android supports three formats of Bitmap files: .png(the first choice is pai.jpg(the second choice is pai.gif (not recommended ).

You can use the resource ID (File Name) to directly reference a bitmap file, or create an alias resource in XML.

Note: during compilation, bitmap files can be automatically optimized by the aapt tool using lossless image compression technology. For example, a real-color PNG image can be converted to an 8-color PNG Image Using A chopping board without more than 256 colors. This will generate images of the same quality, but requires a small amount of memory. Therefore, it should be noted that the binary image placed in this directory can be changed during compilation. If you plan to use Word throttling to read images so that they can be converted into a bitmap, You need to place the images in the res/raw/folder so that they will not be optimized.

Bitmap files

The path file here refers to a file in the format of .png).jpg).gif. When files in these formats are stored in the res/drawable/directory, Android creates a resource for these files.

File location ):

Res/drawable/filename.png(.png).jpgor .gif ).

The file name is used for the resource ID.

The data type of the compiled resource (compiled resource datatype ):

The resource points to a BitmapDrawable object.

Resource reference ):

In Java code: R. drawable. filename;

In XML: @ [package:] drawable/filename

Column (EXAMPLE ):

Save an image in res/drawable/muimage.png. The layout XML applies the image to a View object:

<ImageView
Android: layout_height = "wrap_content"
Android: layout_width = "wrap_content"
Android: src = "@ drawable/myimage"/>

The application uses the getDrawable () method in the code to obtain the Drawable object:

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

XML bitmap

An XML bitmap is a resource defined in XML that points to a bitmap file. It is an alias of the original bitmap file. XML can specify additional properties for bitmaps, such as imitation colors and pasters.

Note: The <bitmap> element can be used as the child element of the <item> element. For example, when creating a status list or layer list, you can exclude the android: drawable attribute from the <item> element, A <bitmap> element is embedded in the <item> element to define the drawing project.

File location ):

Res/drawable/filename. xml

The file name is used as the resource ID.

The data type of the compiled resource (compiled resource datatype ):

The resource points to a BitmapDrawable object.

Resource reference ):

In Java code: R. drawable. filename

In XML: @ [package:] drawable/filename

SYNTAX (SYNTAX ):

<? Xml version = "1.0" encoding = "UTF-8"?>
<Bitmap
Xmlns: android = "http://schemas.android.com/apk/res/android"
Android: src = "@ [package:] drawable/drawable_resource"
Android: antialias = ["true" | "false"]
Android: dither = ["true" | "false"]
Android: filter = ["true" | "false"]
Android: gravity = ["top" | "bottom" | "left" | "right" | "center_vertical" |
"Fill_vertical" | "center_horizontal" | "fill_horizontal" |
"Center" | "fill" | "clip_vertical" | "clip_horizontal"]
Android: tileMode = ["disabled" | "clamp" | "repeat" | "mirror"]/>

Element (ELEMENTS ):

<Bitmap>

Defines bitmap resources and resource attributes.

ATTRIBUTES (ATTRIBUTES ):

Xmlns: android

The string value that defines the namespace of XML. It must be "http://schemas.android.com/apk/res/android ". If <bitmap> is a root element, this attribute is required. When <bitmap> is nested in <item>, it is not required.

Android: src

Draw resources. Required. It points to a resource to be drawn.

Android: antialias

Boolean value. Enable or disable the anti-tooth effect.

Android: dither

Boolean value. If the bitmap does not have the same pixel configuration as the screen, this attribute indicates that the bitmap's imitation color processing is enabled or disabled.

Android: filter

Boolean value to enable or disable bitmap filtering. Filter the bitmap to make the appearance smoother when it is reduced or stretched.

Android: gravity

Key attributes. Defines the center of gravity of a bitmap. If the bitmap is smaller than the container, the center of gravity indicates the position of the drawn resource in the container.

Must be a combination of the following constant values (separated by the "|" symbol when setting the combination ):

 
 

Value Description
Top Place the object on the top of its container without changing the object size.
Bottom Place the object at the bottom of its container without changing the object size.
Left Place the object on the left edge of its container without changing the object size.
Right Place the object on the right edge of its container without changing the object size.
Center_vertical Place the object in the vertical center of its container without changing the object size.
Fill_vertical If needed, the vertical size of the object increases so that the vertical direction can fully fill its container.
Center_horizontal Place the object in the horizontal center of its container without changing the object size.
Fill_horizontal If needed, the horizontal size of the object increases so that its container can be fully filled in the horizontal direction.
Center Place the object in the center of the container without changing the size.
Fill If needed, the horizontal and vertical dimensions of the object will grow so that the container can be fully filled with it.
Clip_vertical Additional options to crop the top or bottom edge of the Child object to the container border position. Crop is based on the vertical center setting. When top is selected, crop the bottom edge. When bottom is selected, crop the top edge instead of the top or bottom edge.
Clip_horizontal Additional options to crop the left or right edge of the Child object to the container border position. The cropping is based on the horizontal center setting, the right edge must be cropped when left, and the left edge must be cropped when right, instead of both sides.
 
Android: tileMode
Key Attribute: specifies that resources are drawn in tile mode. When this mode is enabled, bitmap will be painted repeatedly to fully fill the painting area. When tiled mode is enabled, the center of gravity is ignored.
In this mode, you must set one of the following constant values:
Value Description
Disabled Non-tiled bitmap. This is the default value.
Clamp If you want to draw a shadow outside the original border, copy the color of the edge.
Repeat Draw a shadow image in both the horizontal and vertical directions.
Mirror Draw a shadow image in both the horizontal and vertical directions, and copy the image alternately so that there is always a gap between adjacent images.
 
 

EXAMPLE ):

<? Xml version = "1.0" encoding = "UTF-8"?>
<Bitmapxmlns: android = "http://schemas.android.com/apk/res/android"
Android: src = "@ drawable/icon"
Android: tileMode = "repeat"/>

 

From FireOfStar's column

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.