Connections between pixel, bitmap, drawable, canvas, paint, and matrix in the android Display System

Source: Internet
Author: User
Tags mathematical functions color gamut

/*************************************** **************************************** *************
* Author: conowen @ Dazhong
* E-mail: conowen@hotmail.com
* Http://blog.csdn.net/conowen
* Note: This article is original and only used for learning and communication. For more information, indicate the author and its source.

**************************************** **************************************** ************/

1. pixel and bitmap

Pixel

Pixels, also known as pixels, are the basic unit for image display. Each pixel can have its own color value, which can be displayed in three primary colors, and thus be divided into three seed pixels (RGB Color Gamut), red, yellow, and black (CMYK color gamut, common in the printing industry and printers ). A photo is a collection of sample points. Therefore, the higher the Resolution, the closer the displayed image is to the real object. An image composed of pixels is called bitmap ). Generally, for a display screen, a point corresponds to a pixel. (For details about pixel DPI and dip and PX in layout, refer to the next blog)

Bitmap

Bitmap

Extends object
Implements
Parcelable

Java. Lang. Object
Bytes Android. Graphics. Bitmap

1.1 Definition:

Bitmap is called a bitmap or raster graphics. It is an image represented by a pixel array. The color information of each pixel is represented by an RGB combination or a gray value. The data bits required for color information are divided into 1, 4, 8, 16, 24, and 32 bits. The higher the number of BITs, the richer the color, and the larger the corresponding data volume. One-Bit Bitmap represents a pixel color. Because a data bit can only represent two colors, it is also called a binary bitmap. A 24-bit RGB bitmap is usually called a true color bitmap. In general, bitmap is not compressed, and the size of the bitmap file is relatively large. (The commonly used bitmap compression algorithm is implemented through the "index color table"). Most bitmaps support alpha channels (transparent channels ).

1.2. encoding method:


RGB Encoding

A Bitmap color encoding method, which uses the optical intensity of the three primary colors of red, green, and blue to represent a color. This is the most common bitmap encoding method and can be directly used for screen display.

CMYK Encoding

A Bitmap color encoding method that uses four pigment content: blue, magenta, yellow, and black to represent a color. One of the commonly used bitmap encoding methods can be directly used for color printing.

1.3. Color Depth

The color depth is also called the number of digits of the color, that is, how many binary bits are used to represent the color of each vertex in the bitmap, which is an important indicator of resolution. Commonly used one (monochrome), two (4 color, CGA), four (16 color, VGA), eight (256 color), 16 (enhanced color ), 24-bit and 32-bit (true color. Bitmap with a color depth of more than 16 digits can be further classified based on the digits indicating RGB primary colors or CMYK primary colors (some also include Alpha channels). For example, a 16-Bit Bitmap can also be divided into r5g6b5, r5g5b5x1 (one bit does not carry information), r5g5b5a1, r4g4b4a4, and so on.

1.4. You have to mention the vector image here:

Vector graph definition:

Vector graph [vector] is also called a vector graph. Simply put, it means scaling without losing its real image format. Vector graphs are generated by combining multiple objects. The recording method of each object is implemented by mathematical functions. That is to say, in fact, a vector chart does not record every bit of information on the screen as a bitmap, but records the element shape and color algorithms. When you open a vector chart, the software calculates the function corresponding to the image, and displays the [shape and color] of the image. No matter whether the display screen is large or small, the algorithms corresponding to the objects on the screen remain unchanged. Therefore, even if the screen is scaled in multiples, the display effect is still the same [no distortion]. (Bitmap scaling will be distorted)

(For more information, see Wikipedia)

1.5 obtain a bitmap object in Android


1.5.1 obtain bitmap objects using common static methods:

static Bitmap     createBitmap(Bitmap source, int x, int y, int width, int height, Matrix m, boolean filter)//Returns an immutable bitmap from subset of the source bitmap, transformed by the optional matrix.static Bitmap     createBitmap(int width, int height, Bitmap.Config config)//Returns a mutable bitmap with the specified width and height.static Bitmap     createBitmap(Bitmap source, int x, int y, int width, int height)//Returns an immutable bitmap from the specified subset of the source bitmap.static Bitmap     createBitmap(int[] colors, int offset, int stride, int width, int height, Bitmap.Config config)//Returns a immutable bitmap with the specified width and height, with each pixel value set to the corresponding value in the colors array.static Bitmap     createBitmap(Bitmap src)//Returns an immutable bitmap from the source bitmap.static Bitmap     createBitmap(int[] colors, int width, int height, Bitmap.Config config)//Returns a immutable bitmap with the specified width and height, with each pixel value set to the corresponding value in the colors array.static Bitmap     createScaledBitmap(Bitmap src, int dstWidth, int dstHeight, boolean filter)//Creates a new bitmap, scaled from an existing bitmap, when possible.

1.5.2 use the bitmapfactory class to obtain bitmap objects

The bitmapfactory factory class is a tool class that provides a large number of methods. Most of them decode and create bitmap objects from different data sources. The typical method is as follows.

Static bitmap decodebytearray (byte [] data, int offset, int length, bitmapfactory. options opts) // decode an immutable bitmap from the specified byte array. // parse byte [] Static bitmap decodebytearray (byte [] data, int offset, int length) // decode an immutable bitmap from the specified byte array. static bitmap decodefile (string pathname) // decode a file path into a bitmap. static bitmap decodefile (string pathname, bitmapfactory. options opts) // decode a file path into a bitmap. static bitmap decodefiledescriptor (filedescriptor FD) // decode a bitmap from the file descriptor. static bitmap decodefiledescriptor (filedescriptor FD, rect outpadding, bitmapfactory. options opts) // decode a bitmap from the file descriptor. static bitmap decoderesource (Resources res, int ID, bitmapfactory. options opts) // synonym for opening the given resource and calling decoderesourcestream (resources, typedvalue, inputstream, rect, bitmapfactory. options ). static bitmap decoderesource (Resources res, int ID) // synonym for decoderesource (resources, Int, android. graphics. bitmapfactory. options) Will null options. static bitmap decoderesourcestream (Resources res, typedvalue value, inputstream is, rect pad, bitmapfactory. options opts) // decode a new bitmap from an inputstream. static bitmap decodestream (inputstream is) // decode an input stream into a bitmap. static bitmap decodestream (inputstream is, rect outpadding, bitmapfactory. options opts) // decode an input stream into a bitmap.

1.5.3 use bitmapdrawable to obtain bitmap objects

Bitmapdrawable inherits from drawable

// Method 1 resources res; inputstream is = res. openrawresource (R. drawable. PIC); bitmapdrawable = new bitmapdrawable (is); bitmap BMP = bitmapdrawable. getbitmap (); // method 2 resources res; bitmapdrawable = (bitmapdrawable) res. getdrawable (R. drawable. PIC); bitmap BMP = bitmapdrawable. getbitmap (); // method 3 imageview image; image. setimagebitmap (bitmapfactory. decodestream (~~~~)); Bitmapdrawable = (bitmapdrawable) image. getdrawable (); bitmap BMP = bitmapdrawable. getbitmap ();

1.6 attach the Conversion Relationship Between bitmap and byte []

1.6.1 bitmap2bytes

public byte[] Bitmap2Bytes(Bitmap bmp) {          ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();          //public boolean compress (Bitmap.CompressFormat format, int quality, OutputStream stream)        bmp.compress(Bitmap.CompressFormat.PNG, 100, byteArrayOutputStream );          return byteArrayOutputStream.toByteArray();      }  

1.6.2. bytes2bitmap

Static bitmap decodebytearray (byte [] data, int offset, int length, bitmapfactory. Options opts) // decode an immutable bitmap from the specified byte array. // parse byte []

2. drawable

When you import an image file to the drawable folder of the android project, the android SDK generates a drawable object for this file. You can access this object through R. drawable. It is generally obtained directly by calling resource. getdrawable (int id.

The drawable folder supports the following image formats: GIF, PNG, JPG, and BMP.

2.1 Conversion Relationship Between bitmap and drawable

2.1.1 convert bitmap to drawable:

Bitmapdrawable = new bitmapdrawable (Bitmap) Because btimapdrawable is a child class of drawable, bitmapdrawable is used directly.

2.1.2. Convert drawable to bitmap

Refer to method 1.5.3 for obtaining bitmap.

3. Canvas and paint

The canvas object can be understood as a canvas. Most of the canvas methods are to set the size, shape, and background color of the canvas. To draw a canvas, it is generally used in combination with the paint object. As the name suggests, the paint is the style of the paint, the color of the paint, and so on.

 

4. Matrix

Matrix

Extends object

Java. Lang. Object
Bytes Android. Graphics. Matrix

Matrix is the meaning of a matrix. It is generally used in combination with bitmap to achieve image scaling, deformation, distortion, and other operations.

Public static bitmap scalebitmap (Bitmap bitmap, int scalwidth, int scaleheight) {int W = bitmap. getwidth (); int H = bitmap. getheight (); // The Matrix object matrix = new matrix () used to create an operation image; // calculates the scaling ratio float SX = (float) scalewidth/W ); float Sy = (float) scaleheight/h); // sets the scale matrix. postscale (sx, Sy); // create a new bitmap. Its content is bitmap scalebmp = bitmap. createbitmap (bitmap, 0, 0, W, H, matrix, true); Return scalebmp ;}

Other typical methods of the matrix class.

Boolean postscale (float Sx, float Sy) // scale Boolean postskew (float kx, float KY) // distort Boolean posttranslate (float dx, float Dy) // convert Boolean preconcat (matrix other) // merge Boolean prerotate (float degrees) // rotate

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.