Android plotting learning Summary (2) -- bitmap

Source: Internet
Author: User

Through the previous study, I have a certain understanding of the core part of Android painting. 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 the most commonly used resource in our development. After all, a beautiful interface is the most attractive to users. Bitmap operations are divided into the following functions:

  1. Retrieve bitmap from resource
  2. Obtain bitmap Information
  3. Display bitmap
  4. Bitmap Scaling
  5. 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. The first method is easier to use. The second method is described in detail below. After obtaining the data stream of the resource file through the resource function: inputstream openrawresource (int id), 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 .)

  1. Use bitmapdrawable (inputstream is) to construct a bitmapdrawable;
  2. 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 .)

  1. 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 stated, it is clearly stated in the android SDK support media format.

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 directly use the drawbirmap () class of the canvas class to display bitmap, or use bitmapdrawable to draw bitmap to the canvas. The specific display of bitmap is not the main problem. The main problem is how to obtain the canvas. Refer to the method in snake and provide a simple example testview for you to download.

The example testview contains two classes: testactivity, testview, testactivity, and activity. testview and view. In this example, testview is directly used as the window of testactivity, so that we can draw a picture directly in testview. For more information, see the code in oncreate () of testactivity and settings in layout/Main. xml. 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 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 =NewMatrix ();
// 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) creates a new Bitmap Based on the original bitmap rotation.

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.