Getting Konw the Bitmap of Android

Introduce

Bitmap is used frequently, and too many classes about it to know for us. Now, these classes will be summaried in this article.

Summary about Bitmap of Android

BitmapDrawable is a bitmap package. Directly to the file, that is, an original bitmap package. In Xml mode, the original bitmap can be a series of processing, such as anti-aliasing, stretching, alignment and so on.

To understand the use of BitmapDrawable, but also need to understand Bitmap, BitmapFactory and other categories. Bitmap represents an original bitmap, and can be a series of bitmap transform operation. BitmapFactory provides a set of methods for generating a Bitmap object. Used in the Canvas.

Learn about drawing and bitmap conversion later. BitmapDrawable relatively simple to use, that is, in the other direct reference to the xml file on it, but should pay attention to the definitions in the xml BitmapDrawable the use and meaning of the various properties.

Bitmap, BitmapDrawable, BitmapFactory, BitmapShader

Bitmap

Gets a bitmap from bitmapdrawable

1
2
3
4
5
6
Resources res=getResources();  
Drawable drawable = res.getDrawable(R.drawable.myimage);
//Drawable
BitmapDrawable bitmapDrawable=(BitmapDrawable)drawable;

Bitmap bitmap=bitmapDrawable.getBitmap();

BitmapFactory

The BitmapFactory class provides methods for creating Bitmap objects, which are used to parse and create Bitmap objects from different data sources, as follows:

  1. decodeFile (String pathName); Used to parse from the file specified by the given path to create a Bitmap object

  2. decodeFileDescriptor (FileDescriptor fd); from the FileDescriptor corresponding to the file resolution, create a bitmap object

  3. decodeResource (Resource res, int id); Used to parse the specified resource from the specified resource, creating a bitmap object

  4. decodeStream (InputStream in); Used to parse from the specified input stream, creating a different bitmap object

The first method is the final implementation, the latter two are only the first method of the package.

The second method can be cut from the specified area in the source Bitmap (x, y, width, height) to extract a cut;

The third method is to scale the source Bitmap to a dstWidth x dstHeight Bitmap.

Set the Rotate (via setRotate ()) or Scale (via setScale ()) of the Matrix to pass in the first method to rotate or scale.

1
2
3
4
//example
ImageView imgView = (ImageView)findViewById(R.id.image3);

imgView.setImageBitmap(BitmapFactory.decodeResource(this.getResources(), R.drawable.icon) );