Android bitmap and canvas small notes (GO)

Source: Internet
Author: User

1. Getting a bitmap from a resource (BITMAP)

You can use bitmapdrawable or bitmapfactory to get a bitmap from a resource.

Of course, the first resource needs to be obtained: Resources res=getresources ();

Get a bitmap using bitmapdrawable

(1) Use Bitmapdrawable (InputStream is) to construct a bitmapdrawable;

(2) the use of the Bitmapdrawable class of Getbitmap () obtained in place of the map;

// read the InputStream and get the bitmap InputStream is=Res.openrawresource (r.drawable.pic180); bitmapdrawable Bmpdraw=new  bitmapdrawable (IS); Bitmap bmp=bmpdraw.getbitmap ();

Or use the following method:

Bitmapdrawable bmpdraw=(bitmapdrawable) res.getdrawable (r.drawable.pic180); Bitmap bmp=bmpdraw.getbitmap ();

Get a bitmap using Bitmapfactory

(creates Bitmap objects from various sources, including files, streams, and byte-arrays.)

Use the Bitmapfactory class Decodestream (InputStream is) to decode the bitmap resource and get the bitmap.

Bitmap Bmp=bitmapfactory.decoderesource (res, r.drawable.pic180);

All the functions of bitmapfactory are static, and this helper class can get bitmaps through resource IDs, paths, files, data streams, and so on.

The following image formats are described in the Android SDK: PNG (preferred), JPG (acceptable), GIF (discouraged), and BMP (Android SDK support Media format).

2. Get information about a bitmap

Gets to bitmap the bitmap size, pixels, density, transparency, color format, and so on, through the interface of the bitmap class

    • Using the Bitmap.config definition for RGB color format in bitmap, only includes Alpha_8, argb_4444, argb_8888, rgb_565, missing some other, such as rgb_555, may need to pay attention to this small problem in the development;
    • Bitmap also provides a compress () interface to compress images, but Androidsak only supports PNG, JPG compression, and other formats that need to be supplemented by Android developers themselves.

3. Displaying bitmaps

There are three ways to display bitmaps to achieve

(1) The core class canvas can be used to display bitmaps through the Drawbirmap () of the canvas class;

(2) Draw the bitmap to canvas with the help of bitmapdrawable;

(3) display the bitmap to the view via bitmapdrawable.

Convert to Bitmapdrawable object display bitmap  

         // Get bitmap        Bitmap bmp=Bitmapfactory.decoderesource (res, r.drawable.pic180);         // Convert to bitmapdrawable object        Bitmapdrawable bmpdraw=New  bitmapdrawable (BMP);         // Show Bitmap        ImageView iv2 = (ImageView) Findviewbyid (;       Iv2.setimagedrawable (Bmpdraw);        

displaying bitmaps using the canvas class

  Here is a subclass panel that inherits from view, which is displayed in the OnDraw of the subclass

 Public classMainactivityextendsActivity {/**Called when the activity is first created.*/@Override Public voidonCreate (Bundle savedinstancestate) {Super. OnCreate (savedinstancestate); Setcontentview (NewPanel ( This)); }        classPanelextendsview{ PublicPanel (Context context) {Super(context); }               Public voidOnDraw (canvas canvas) {Bitmap BMP=Bitmapfactory.decoderesource (Getresources (), r.drawable.pic180);              Canvas.drawcolor (Color.Black); Canvas.drawbitmap (BMP,10, 10,NULL); }      } }

4. Bitmap Scaling

(1) Re-draw a bitmap as required, the bitmap is what we need, and the bitmap display almost the same: Drawbitmap (Bitmap Bitmap, rect src, rect dst, paint paint).

(2) on the basis of the original bitmap, scale the in-place map to create a new bitmap: CreateBitmap (Bitmap source, int x, int y, int width, int height, Matrix m, Boolean filter)

(3) with the help of the canvas scale (float SX, float sy) (Preconcat the present matrix with the specified scales), but be aware that the entire canvas is scaled at this time.

(4) with matrix:

Bitmap bmp = Bitmapfactory.decoderesource (getresources (), r.drawable.pic180);  Matrix Matrix=new  matrix (); Matrix.postscale (0.2f, 0.2f); Bitmap dstbmp=bitmap.createbitmap (bmp,0,0, Bmp.getwidth (), Bmp.getheight (), Matrix,true  ); Canvas.drawcolor (;   null);  

5. Bitmap rotation

Rotation implementations are similar to scaling implementations, or they can be implemented with the help of a matrix or canvas;

Bitmap bmp = Bitmapfactory.decoderesource (getresources (), r.drawable.pic180);  Matrix Matrix=new  matrix (); Matrix.postscale (0.8f, 0.8f); Matrix.postrotate ; Bitmap dstbmp=bitmap.createbitmap (bmp,0,0, Bmp.getwidth (), Bmp.getheight (), Matrix,true   NULL

6.Save and restore for canvas

The OnDraw method passes in a canvas object, which is the one you use to draw the visual interface of the control.

In the OnDraw method, we often see calls to the Save and restore methods, what exactly are they used for?

? Save: Used to save the state of the canvas. After save, you can invoke canvas panning, indenting, rotating, trimming, cropping, and so on.

? Restore: The state used to restore the canvas before it is saved. Preventing the actions that are performed on the canvas after save has an impact on subsequent drawing.

Save and restore are paired (restore can be less than save, but not many), and error will be raised if the restore is called more times than save. between save and restore, there is often a special manipulation of the canvas.

Original link:


Android bitmap and canvas small notes (GO)

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: 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.