Document directory
- 1. Retrieve bitmap from resource
- 2. Obtain bitmap Information
- 3. Display bitmap
- 4. Bitmap Scaling
- 5. Bitmap Rotation
- Summary
Through the previous article
Of
Learning, for Android
The core part of the painting has some knowledge. Later, we will introduce in detail the use of various painting objects in Android. First, we will introduce the most commonly used Bitmap (Bitmap ). Bitmap is enabled
The most commonly used resources in development. After all, a beautiful interface is the most attractive to users. Bitmap operations are divided into the following functions:
- Retrieve bitmap from resource
- Obtain bitmap Information
- Display bitmap
- Bitmap Scaling
- Bitmap Rotation
1. Retrieve bitmap from resource
In the previous article, we introduced: Get resource first, then obtain drawable through resource ID, or get the data stream of resource file through resource ID. Use the first method
It is relatively easy. The second method is described in detail below. Use the resource function: inputstream openrawresource (int
After obtaining the data stream of the resource file, you can also obtain bitmap in two ways, as shown below:
Use bitmapdrawable (A drawable that wraps a bitmap and can be tiled, stretched, or aligned .) |
- Use bitmapdrawable (inputstream is) to construct a bitmapdrawable;
- Use getbitmap () of the bitmapdrawable class to obtain the bitmap;
Bitmapdrawable also provides operations such as display bitmap. |
Use bitmapfactory (Creates bitmap objects from various sources, including files, streams, and byte-arrays .) |
- Bitmapfactory class decodestream (inputstream is) is used to decode bitmap resources and obtain bitmap
All functions of bitmapfactory are static. This helper class can obtain bitmap through resource ID, path, file, and data stream. |
The above methods can be freely selected during programming. In the android SDK, the supported image formats are as follows: PNG (preferred), JPG
(Acceptable), GIF (discouraged), although the BMP format is not clearly described, but in the android SDK support
Media format is clearly stated.
2. Obtain bitmap Information
To obtain bitmap information, such as the bitmap size, transparency, and color format, you can easily obtain bitmap information. The android SDK provides a detailed description of bitmap, which is easy to read and not detailed here. Here, we will only help you to explain the following two points:
- Bitmap is used for RGB color formats in bitmap. the Config definition only includes alpha_8, argb_4444, argb_8888, and rgb_565. Some other problems are missing, such as rgb_555. You may need to pay attention to this problem during development;
- Bitmap also provides the compress () interface to compress images. However, androidsak only supports compression in PNG and jpg formats. Android Developers need to add other formats.
3. Display bitmap
To display bitmap, you must use the core canvas class. You can use the drawbirmap () class of the canvas class to display the bitmap, or use bitmapdrawable
Draw a bitmap to the canvas. How to display bitmap is not the main problem. The main problem is how to obtain canvas. Refer to the method in snake and give a simple example.
Testview, available for download
.
Testview example: it contains two classes: testactivity, testview; testactivity inherited from
Activity, testview inheritance and view. In this example, testview is directly used as the window of testactivity, so that we can directly
Testview draws a picture. For more information, see the code in oncreate () of testactivity and
. In testview, The ondraw () is used to draw a picture directly. The result is displayed directly on the interface after the example program is run.
4. Bitmap Scaling
Bitmap scaling provides two methods in the android SDK:
- Redraw a bitmap as needed. The bitmap after painting is what we need. It is almost the same as the display of the bitmap:
Drawbitmap (Bitmap bitmap, rect SRC, rect DST, paint)
- Based on the original bitmap, scale the original bitmap to create a new bitmap:
Createbitmap (bitmap source, int X, int y, int width, int height, matrix m, Boolean filter)
You can understand the 2nd methods at a Glance. For the first method, a simple example is provided to illustrate:
Int W = 320, H = 240;
String mstrtitle = "Feel the new experience Android brings to us ";
Bitmap mbmptest = bitmap. createbitmap (W, H, config. argb_8888 );
Canvas canvastemp = new canvas (mbmptest );
Canvastemp. drawcolor (color. White );
Paint P = new paint ();
String familyname = "";
Typeface font = typeface. Create (familyname, typeface. Bold );
P. setcolor (color. Red );
P. settypeface (font );
P. settextsize (22 );
Canvastemp. Fig (mstrtitle, 0,100, P );
When the bitmap mbmptest is displayed, A 320x240 image with a white background and a red "" text image is displayed, as shown below:
In this example, there is no bitmap scaling operation? Yes, but this was the first thing I thought of when I was thinking about how to write a simple bitmap scaling applet. After reading this example, I think you should understand how to scale down the bitmap.
. Don't underestimate this example. Although it has little to do with bitmap scaling, you can understand the nature of Bitmap Scaling: display the original bitmap as needed, creates a new bitmap.
5. Bitmap Rotation
Bitmap rotation is inseparable from matrix. Matrix has been learned in linear algebra. The Android SDK provides the matrix class, which can be set through various interfaces. In combination with the above example program, add the bitmap rotation function before the bitmap scaling example program displays the bitmap. The modification code is as follows:
Matrix matrix =New
Matrix ();
// Matrix. postscale (0.5f, 0.5f );
Matrix. setrotate (90,120,130 );
Canvas. drawbitmap (mbmptest, matrix, mpaint );
The rotated bitmap is displayed as follows:
In addition to this method, we can also use the bitmap function as follows:
Public static bitmap createbitmap (bitmap source, int X, int y, int
Width, int height, matrix m, Boolean filter), create a new Bitmap Based on the original bitmap rotation.
Summary
The bitmap operations, combined with the classes in the android SDK, are described in detail. Finally, we need to emphasize that this article only analyzes the code reading and analysis in the android SDK. It cannot replace your reading of the android SDK. You should carefully read the android SDK for further study.