Android drawable resource learning (2), bitmapdrawable and bitmap

Source: Internet
Author: User
1. How to Create a bitmapdrawable object

Bitmap represents a bitmap image. Android supports bitmap images in three formats:. PNG (preferred}.jpg (acceptable) and. GIF (discouraged ).

The secondary node has the worst support.

You can directly use the image name as the resource ID to directly reference a bitmap image. You can also create a resource alias ID in the XML file.

When building an application, bitmap files may be automatically compressed by the APPT tool and optimized to lossless images. For example, a real-color PNG can be converted into an 8-bit PNG and a palette without more than 256 colors. This results in the same image quality, but requires less memory. So be aware that the binary files of images in the drawable directory can be changed during program construction. If you want to read an image as a byte stream and convert it into a bitmap, put your image in the Res/raw/folder where they will not be optimized.

1. Use bitmap file

A bitmapfile contains a .png).jpg, .gif file. Android creates a drawable resource for these files stored in the Res/drawable/directory.

File Location:

       res/drawable/filename.png(.png,
.jpg
, Or.gif) The file name is the resource ID.

Compile resource data type:
A pointer to the bitmapdrawable type.
Resource reference:

In Java: R.drawable.filename
In XML: @[package:]drawable/filename
Example:
Images stored in this way res/drawable/myimage.pngIn the layout XML file, it is displayed in the view.
<ImageView    android:layout_height="wrap_content"    android:layout_width="wrap_content"    android:src="@drawable/myimage" />

It is retrieved as a drawable object in Java code.

Resources res = getresources (); drawable = res. getdrawable (R. drawable. myimage); // This is actually a bitmapdrawable object bitmapdrawable = (bitmapdrawable) drawable; // you can call the getbitmap method to obtain the bitmap Bitmap bitmap = bitmapdrawable. getbitmap ();
Refer:
  • 2d graphics
  • BitmapDrawable

2. xml bitmap

An XML bitmap is a resource defined in an XML file that points to a bitmap file. It acts as an alias for the original bitmap file and can specify some additional attributes.

Note: You can use <bitmap> as its subnode in the <item> node. For example, when you define a State List or layer list, you can includeandroid:drawableAttribute

Note:You can use<bitmap>Element as a child of<item>Element. Forexample, when creating astate list orlayer list, you can exclude
android:drawableAttribute from<item>Element and nest<bitmap>Inside it that defines the drawable item.

File Location:
res/drawable/filename.xml
Filename is the resource ID.
Compile Resource Type
Pointer to bitmapdrawable type
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:tileMode=["disabled" | "clamp" | "repeat" | "mirror"] />
Node introduction:
<bitmap>
Define the source and attribute of a bitmap

Attribute:

xmlns:android
Type: string. The namespace that defines XML must be "http://schemas.android.com/apk/res/android ". If <bitmap> is a root element, it is required. If it is nested in <itme>, it is not necessary.
android:src
Type: drawable resource. Required. References A drawableresource.
android:antialias
Type: Boolean. Whether to enable anti-aliasing.
android:dither
Type: Boolean. Whether to allow jitter if the bitmap and the screen pixel configuration are different (for example, the pixel setting of a bitmap is argb 8888, but the screen setting is RGB 565)
android:filter
Type: Boolean. Whether bitmap filtering is allowed. Filter the bitmap to reduce or extend the bitmap to achieve smooth appearance.
android:gravity
Type:Keyword. Defines the gravity of a bitmap. If the bitmap is smaller than its container, use gravity to specify where to draw it.

Must be the following attributes, separated by |

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, notchanging its size.
fill Grow the horizontal and vertical size of the object if needed so it completely fills itscontainer. This is the default.
clip_vertical Additional option that can be set to have the top and/or bottom edges of the Child clipped toits container's bounds. the clip is based on the vertical gravity: A top gravity clips thebottom 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 toits container's bounds. the clip is based on the horizontal gravity: A left gravity clipsthe right edge, a right gravity clips the left edge, and neither clips
Both edges.
android:tileMode
Type: keyword.
The tile mode is defined. When tile mode is enabled, bitmap is repeated and the gravity attribute is ignored.

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 thatadjacent 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" />
Refer:
  • BitmapDrawable
  • Creatingalias Resources
Ii. Use of bitmapdrawable

A bitmapdrawable encapsulates a bitmap. Directly Using a file encapsulates an original bitmap. In XML format, you can perform a series of processing on the original bitmap, such as anti-aliasing, stretching, and alignment.

To understand the usage of bitmapdrawable, you also need to understand bitmap, bitmapfactory, and other classes. Bitmap represents an original bitmap and can perform a series of conversion operations on the bitmap. Bitmapfactory provides a series of methods for generating a bitmap object. It is used in canvas.

Learn more about drawing and bitmap conversion. Bitmapdrawable is easy to use, that is, it can be directly referenced in other XML files. However, pay attention to defining the usage and meaning of each bitmapdrawable attribute in XML.

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.