Example analysis of bitmap usage (display, save, zoom, rotate) in Android _android

Source: Internet
Author: User

This example describes the bitmap usage in Android. Share to everyone for your reference. Specifically as follows:

The image formats you can support in the Android SDK are as follows: PNG, JPG, GIF, and BMP.

Creation of 1.Bitmap

With the help of Bitmapfactory.

1 The picture in the resource

Using Bitmapfactory to get bitmaps

Copy Code code as follows:
Bitmap bmp = Bitmapfactory.decoderesource (This.getresources (), r.drawable.testimg);

Or is:

Resources res=getresources ();
Use Bitmapdrawable to get the bitmap
/////Use Bitmapdrawable (InputStream is) to construct a bitmapdrawable;
Use the Getbitmap () of the Bitmapdrawable class to obtain the
bitmap;//Read the InputStream and get the bitmaps
InputStream Is=res.openrawresource ( R.DRAWABLE.TESTIMG); 
Bitmapdrawable bmpdraw=new bitmapdrawable (IS);
Bitmap Bmp=bmpdraw.getbitmap ();

2 Image in SD card

Copy Code code as follows:
Bitmap bmp = Bitmapfactory.decodefile ("/sdcard/testbitmap/testimg.png")

2. Keep the Bitmap in the SDcard

File Fimage = new file ("/sdcard/testbitmap/testimg.png");  
Fimage.createnewfile ();
FileOutputStream IStream = new FileOutputStream (fimage); 
Bmp.compress (Compressformat.png, iStream); 
Istream.close ();
Fimage.close ();
IStream =null;
Fimage =null;
Write to the output stream and save it to the file.

3. Using pictures from the network

The link address of the picture  
String imgurlstr = "yun_qi_img/990e6271796a7a6c170c.jpg";  
URL imgurl = new URL (imgurlstr);  
URLConnection conn = Imgurl.openconnection ();  
Conn.connect ();  
InputStream is = Conn.getinputstream ();  
Bufferedinputstream bis = new Bufferedinputstream (is);
Download picture
Bitmap bmp = Bitmapfactory.decodestream (bis);
Close Stream
bis.close ();  
Is.close (); 
Imgurl =null;

4. Display Picture

1) Convert to Bitmapdrawable object display bitmap

Convert to Bitmapdrawable object
bitmapdrawable bmpdraw=new bitmapdrawable (BMP);
Display bitmap
ImageView iv2 = (imageview) Findviewbyid (r.id.imageview02);
Iv2.setimagedrawable (Bmpdraw);

2 using the Canvas class to display the bitmap

Copy Code code as follows:
Canvas.drawbitmap (BMP, 0, 0, NULL);

5. Zoom Bitmap

1 will be a bitmap in accordance with the requirements of the redraw, the picture after the bitmap is what we need, and the display of the bitmap almost the same:

Copy Code code as follows:
Drawbitmap (Bitmap Bitmap, Rect src, Rect DST, Paint Paint)

2 on the basis of the original bitmap, scale in-situ diagram, create a new bitmap:

Copy Code code as follows:
CreateBitmap (Bitmap source, int x, int y, int width, int height, Matrix m, Boolean filter)

3) with the help of Canvas's scale (float SX, float sy), be aware that the entire canvas is scaled at this time.

4) Using matrix:

Matrix matrix=new Matrix ();
Matrix.postscale (0.2f, 0.2f);
Bitmap Dstbmp=bitmap.createbitmap (Bmp,0,0,bmp.getwidth (), Bmp.getheight (), matrix,true);
Canvas.drawbitmap (dstbmp, ten, NULL); 

6. Rotate Bitmap

Using the matrix or canvas to achieve.

Matrix matrix=new Matrix ();
Matrix.postrotate ();
Bitmap Dstbmp=bitmap.createbitmap (Bmp,0,0,bmp.getwidth (), Bmp.getheight (), matrix,true);
Canvas.drawbitmap (dstbmp, ten, NULL);

I hope this article will help you with your Android program.

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