46. Bitmap in android

Source: Internet
Author: User

The following formats are supported in the Android SDK: png, jpg, gif, and bmp.

1. Create a Bitmap

BitmapFactory.
1) images in resources
Use BitmapFactory to obtain bitmap
Bitmap bmp = BitmapFactory. decodeResource (this. getResources (), R. drawable. testImg );
Or
Resources res = getResources ();
// Use BitmapDrawable to obtain the bitmap
// Use BitmapDrawable (InputStream is) to construct a BitmapDrawable;
// Use getBitmap () of the BitmapDrawable class to obtain the bitmap;
// Read InputStream and obtain the bitmap
InputStream is = res. openRawResource (R. drawable. testImg );
BitmapDrawable BMP draw = new BitmapDrawable (is );
Bitmap bmp = bmp draw. getBitmap ();

2) pictures in the SD card
Bitmap bmp = BitmapFactory. decodeFile ("/sdcard/testBitmap/testImg.png ")

2. Save Bitmap in sdcard
File fImage = new File ("/sdcard/testBitmap/testImg.png ");
FImage. createNewFile ();
FileOutputStream iStream = new FileOutputStream (fImage );
Bmp. compress (CompressFormat. PNG, 100, iStream );
IStream. close ();
FImage. close ();
IStream = null;
FImage = null;
// Write it to the output stream and save it to the file.

3. Use images in the Network

// URL of the image
String imgURLStr = "http://tx.bdimg.com/sys/portrait/item/990e6271796a7a6c170c.jpg ";
URL imgURL = new URL (imgURLStr );
URLConnection conn = imgURL. openConnection ();
Conn. connect ();
InputStream is = conn. getInputStream ();
BufferedInputStream bis = new BufferedInputStream (is );

// Download the image
Bitmap bmp = BitmapFactory. decodeStream (bis );

// Close Stream
Bis. close ();
Is. close ();
ImgURL = null;

4. display images
1) convert to BitmapDrawable object display bitmap
// Convert to BitmapDrawable object
BitmapDrawable bmp draw = new BitmapDrawable (bmp );
// Display bitmap
ImageView iv2 = (ImageView) findViewById (R. id. ImageView02 );
Iv2.setImageDrawable (BMP draw );
2) use the Canvas class to display bitmap
Canvas. drawBitmap (bmp, 0, 0, null );

5. Zoom bitmap
1) redraw a Bitmap as needed. The bitmap after the 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 ).

2) 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)

3) use the scale (float sx, float sy) of the Canvas, but note that the entire Canvas is scaled at this time.

4) 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, 10, 10, null );
6. Rotating bitmap
Implemented Using Matrix or Canvas.

Matrix matrix = new Matrix ();
Matrix. postRotate (45 );
Bitmap dstbmp = Bitmap. createBitmap (bmp, 0, 0, bmp. getWidth (), bmp. getHeight (), matrix, true );
Canvas. drawBitmap (dstbmp, 10, 10, null );

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.