Because the project involves operations on the bitmap (Android. Graphics. Bitmap), we carefully studied the bitmap functions provided by Android against the android reference documentation.
1. Bitmap acquisition
A bitmapfactory class is provided in the android SDK. This method can be used to obtain a bitmap from a file path or input stream.
Package: Android. Graphics
Class: bitmapfactory
Introduction In Android SDK: Creates bitmap objects from various sources, including files, streams, and byte-arrays.
This class provides the following methods to obtain bitmap, which are static:
1. decodebytearray (byte [] data, int offset, int length) converts data from a byte array to a bitmap.
2. decodefile (string pathname) is a bitmap obtained from an image file path (JPG, PNG, and BMP formats are supported)
3. decodefiledescriptor (filedescriptor FD) uses a file descriptor to obtain a bitmap.
4. decoderesource (Resources res, int ID) gets bitmap by resource ID
5. decodestream (inputstream is) gets bitmap from the image input stream.
Here, because the Android system Program When loading large images, an outofmemoryerror occurs. Two solutions can be taken:
Use Android first. provider. mediastore. images. the media query (contentresolver Cr, Uri, string [] projection, string where, string orderby) method obtains the ID of the corresponding image, and then uses android. provider. mediastore. images. queryminithumbnail (contentresolver Cr, long origid, int kind, string [] projection) of the thumbnails class to obtain the path of the thumbnail corresponding to the image, and then use decodefile (string
Pathname) to obtain the thumbnail of the image, so as to avoid memory overflow.
Compress the image by compressing the image. Here we mainly use the bitmapfactory class. The methods mentioned above that can obtain bitmap correspond to another method with one parameter added, such as decodefile (string pathname) corresponds to decodefile (string pathname, bitmapfactory. options opts). For more information, see references. The OPTs parameter is introduced to set it to get a compressed image.
Ii. Obtaining 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. Here, we will explain the following two points:
1. 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;
2. Bitmap also provides the compress () interface to compress images. However, the android SDK only supports compression in PNG and jpg formats. Android Developers must add other formats.
3. bitmap display
The display of Bitmap. Generally, you can place the imageview control on the interface and call the setimagebitmap (Bitmap BM) method of the android. widget. imageview class to display the bitmap. In addition, you can use the drawbirmap () method of the core class android. Graphics. Canvas to display bitmap, or use bitmapdrawable to draw bitmap to the canvas.
Iv. Bitmap Scaling
Bitmap scaling provides two methods in the android SDK:
1. 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 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)
The essence of Bitmap scaling is to display the original bitmap as needed and create 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. Example:"
Matrix matrix = new matrix (); matrix. setrotate (90,120,130); canvas. drawbitmap (mbmptest, matrix, mpaint );
In addition to this method, you can also use the functions provided by bitmap: 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.
6. Bitmap capturing
It is easy to use matrix to intercept bitmap rules such as rectangles. Example:
Bitmap croppedimage;
// Obtain the cropped rectangle
Rect r = mcrop. getcroprect ();
If (r = NULL) return;
Int width = R. Width ();
Int Height = R. Height ();
// Generate the cropped Image
Croppedimage = bitmap. createbitmap (width, height, bitmap. config. rgb_565 );
Canvas canvas = new canvas (croppedimage );
Rect dstrect = new rect (0, 0, width, height );
Canvas. drawbitmap (mbitmap, R, dstrect, null );
However, there is no good solution to the irregular shape of the bitmap. I once thought about directly operating on pixels. Algorithm And give up. By chance, when I read a blog post, I found that I used android. graphics. the canvas clippath method not only achieves the purpose of highlighting the path, but also captures images of any shape, thus solving this problem.
The above is a summary of the bitmap function of Android, as well as some ideas and conclusions obtained in practice. More in-depth content needs to carefully read and study the android SDK documentation and pay for it in practice