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