The BitmapDecode example mainly describes the Android platform's image decoding function. The Android platform supports PNG and JPEG image formats and GIF animation.
The Android API mainly uses BitmapFactory (PNG or JPEG static image) and Movie (GIF animation) to decode images ).
Decodes images or animations. The data source can be a byte array, InputStream, resource ID, or a specified file name. For BitmapFactory, you can also use BitmapFactory. Options to specify some decoding settings.
The following code specifies opts. inJustDecodeBounds = true, indicating that only the length and width of the decoded image are obtained during decoding. In this case, the returned value of bm is null, whereas that of opts. outWidth, opts. outHeight returns the width and length of the image. In this usage, the decoder does not need to allocate memory for the decoded image, but the value is to return some information about the image through the output parameters of BitmapFactory. Options.
[Java]
BitmapFactory. Options opts = new BitmapFactory. Options ();
Bitmap bm;
Opts. inJustDecodeBounds = true;
Bm = BitmapFactory. decodeStream (is, null, opts );
// Now opts. outWidth and opts. outHeight are the dimension of
// Bitmap, even though bm is null
BitmapFactory. Options opts = new BitmapFactory. Options ();
Bitmap bm;
Opts. inJustDecodeBounds = true;
Bm = BitmapFactory. decodeStream (is, null, opts );
// Now opts. outWidth and opts. outHeight are the dimension of
// Bitmap, even though bm is null
The following code sets the sampling size to 4, which is equivalent to sampling every four pixels. The result is that the decoded image is 1/4 of the original image (the specific algorithm is determined by the platform, it is not simple to take one of the four pixels ).
[Java]
Opts. inJustDecodeBounds = false; // this will request the bm
Opts. inSampleSize = 4; // scaled down by 4
Bm = BitmapFactory. decodeStream (is, null, opts );
MBitmap = bm;
Opts. inJustDecodeBounds = false; // this will request the bm
Opts. inSampleSize = 4; // scaled down by 4
Bm = BitmapFactory. decodeStream (is, null, opts );
MBitmap = bm;
The following code decodes an image from a resource file through InputStream:
[Java]
// Decode an image with transparency
Is = context. getResources (). openRawResource (R. drawable. frog );
MBitmap2 = BitmapFactory. decodeStream (is );
// Decode an image with transparency
Is = context. getResources (). openRawResource (R. drawable. frog );
MBitmap2 = BitmapFactory. decodeStream (is );
Corresponding to the decoded image Bitmap object, you can use the array of pixel values corresponding to the image obtained by getPixels. With this array of pixel values, Bitmap can create images with different configurations, the following code creates two kinds of configuration images: ARGB_8888 and ARGB_4444. The size of each pixel in the new image is different.
[Java]
Int w = mBitmap2.getWidth ();
Int h = mBitmap2.getHeight ();
Int [] pixels = new int [w * h];
MBitmap2.getPixels (pixels, 0, w, 0, 0, w, h );
MBitmap3 = Bitmap. createBitmap (pixels, 0, w, w, h,
Bitmap. Config. ARGB_8888 );
MBitmap4 = Bitmap. createBitmap (pixels, 0, w, w, h,
Bitmap. Config. ARGB_4444 );
Int w = mBitmap2.getWidth ();
Int h = mBitmap2.getHeight ();
Int [] pixels = new int [w * h];
MBitmap2.getPixels (pixels, 0, w, 0, 0, w, h );
MBitmap3 = Bitmap. createBitmap (pixels, 0, w, w, h,
Bitmap. Config. ARGB_8888 );
MBitmap4 = Bitmap. createBitmap (pixels, 0, w, w, h,
Bitmap. Config. ARGB_4444 );
For Drawable Resources, a more general approach is to directly use the Resources object to obtain the corresponding Drawable Resources:
[Java]
MDrawable = context. getResources (). getDrawable
(R. drawable. button );
MDrawable. setBounds (150, 20,300,100 );
MDrawable = context. getResources (). getDrawable
(R. drawable. button );
MDrawable. setBounds (150, 20,300,100 );
Android.graphics.movie can be used to uncode .gif animation resources. decoding from an array or directly from InputStream: R. drawable. animated_gif is a floating flag animation.
[Java]
Is = context. getResources ()
. OpenRawResource (R. drawable. animated_gif );
If (true ){
MMovie = Movie. decodeStream (is );
} Else {
Byte [] array = streamToBytes (is );
MMovie = Movie. decodeByteArray (array,
0, array. length); www.2cto.com
}
Author: mapdigit