Android Bitmap Usage Summary Normal 0 7.8 lb 0 2 false false MicrosoftInternetExplorer4
1, Drawable→bitmap
public static Bitmap Drawabletobitmap (drawable drawable) {
Bitmap Bitmap = Bitmap
. CreateBitmap (
Drawable.getintrinsicwidth (),
Drawable.getintrinsicheight (),
Drawable.getopacity ()! = Pixelformat.opaque? Bitmap.Config.ARGB_8888
: Bitmap.Config.RGB_565);
Canvas canvas = new canvas (bitmap);
Canvas.setbitmap (bitmap);
Drawable.setbounds (0, 0, drawable.getintrinsicwidth (),
Drawable.getintrinsicheight ());
Drawable.draw (canvas);
return bitmap;
}
2. Get bitmap from the resources
Resources res=getresources ();
Bitmap Bmp=bitmapfactory.decoderesource (res, r.drawable.pic);
3, bitmap→byte[]
Private byte[] Bitmap2bytes (Bitmap BM) {
Bytearrayoutputstream BAOs = new Bytearrayoutputstream ();
Bm.compress (Bitmap.CompressFormat.PNG, BAOs);
return Baos.tobytearray ();
}
4, Byte[]→bitmap
Private Bitmap Bytes2bimap (byte[] b) {
if (b.length!=0) {
Return Bitmapfactory.decodebytearray (b, 0, b.length);
}
else {
return null;
}
}
5. Save Bitmap
Static Boolean savebitmap2file (Bitmap bmp,string filename) {
Compressformat format= Bitmap.CompressFormat.JPEG;
int quality = 100;
OutputStream stream = null;
try {
stream = new FileOutputStream ("/sdcard/" + filename);
} catch (FileNotFoundException e) {
TODO auto-generated Catch block
Generated by Foxit PDF creator©foxit Software
Http://www.foxitsoftware.com for evaluation only.
E.printstacktrace ();
}
return bmp.compress (format, quality, stream);
}
6, the picture according to their own requirements to zoom
Picture Source
Bitmap BM = Bitmapfactory.decodestream (Getresources ()
. Openrawresource (R.drawable.dog));
Get the picture's width high
int width = bm.getwidth ();
int height = bm.getheight ();
Set the desired size
int newwidth = 320;
int newheight = 480;
Calculating the zoom ratio
float ScaleWidth = ((float) newwidth)/width;
float ScaleHeight = ((float) newheight)/height;
Get the Matrix parameter you want to scale
Matrix matrix = new Matrix ();
Matrix.postscale (ScaleWidth, ScaleHeight);
Get a new picture
Bitmap NEWBM = bitmap.createbitmap (BM, 0, 0, width, height, matrix,
true);
Put it on the canvas.
Canvas.drawbitmap (NEWBM, 0, 0, paint);
Related knowledge Link: http://www.eoeandroid.com/thread-3162-1-1.html
7. Summary of usage of bitmap
bitmapfactory.options option = new Bitmapfactory.options ();
Option.insamplesize = 2; Set the image to 1/2 of its original width to prevent memory overflow
Bitmap BM = Bitmapfactory.decodefile ("", option);//File stream
URL url = new URL ("");
InputStream is = Url.openstream ();
Bitmap BM = Bitmapfactory.decodestream (IS);
Android:scaletype:
Android:scaletype is a size that controls how the picture resized/moved to the ImageView. Imageview.scaletype/
The meaning difference of Android:scaletype value:
Center/center is centered on the original size of the image, and when the picture is longer/wider than the length/width of the view, the center portion of the picture is captured
Show
Center_crop/centercrop proportionally enlarges the size of the image so that the image is long (wide) equal to or greater than the length of the view
Wide
Center_inside/centerinside Displays the contents of the picture in full, by scaling it down or the original size to make the picture
Length/width equal to or less than view length/width
Generated by Foxit PDF creator©foxit Software
Http://www.foxitsoftware.com for evaluation only.
Fit_center/fitcenter enlarge/Shrink the picture to the width of the view, centered on the display
Fit_end/fitend Zoom in/out to the width of the view, displayed in the lower part of the view
Fit_start/fitstart enlarge/Shrink the image to the width of the view, displayed in the upper part of the view
Fit_xy/fitxy The picture is not scaled up/down to the view size display
The Matrix/matrix is drawn with a matrix and is displayed by zooming in and out of the image.
Zoom in and out of pictures
public static Bitmap Zoombitmap (Bitmap bitmap,int w,int h) {
int width = bitmap.getwidth ();
int height = bitmap.getheight ();
Matrix matrix = new Matrix ();
float SCALEWIDHT = ((float) w/width);
float ScaleHeight = ((float) h/height);
Matrix.postscale (SCALEWIDHT, ScaleHeight);
Bitmap newbmp = Bitmap.createbitmap (Bitmap, 0, 0, width, height, matrix,
true);
return newbmp;
}
Convert drawable to Bitmap
public static Bitmap Drawabletobitmap (drawable drawable) {
int width = drawable.getintrinsicwidth ();
int height = drawable.getintrinsicheight ();
Bitmap Bitmap = bitmap.createbitmap (width, height,
Drawable.getopacity ()! = Pixelformat.opaque? Bitmap.Config.ARGB_8888
: Bitmap.Config.RGB_565);
Canvas canvas = new canvas (bitmap);
Drawable.setbounds (0,0,width,height);
Drawable.draw (canvas);
return bitmap;
Generated by Foxit PDF creator©foxit Software
Http://www.foxitsoftware.com for evaluation only.
}
Ways to get rounded pictures
public static Bitmap Getroundedcornerbitmap (Bitmap bitmap,float roundpx) {
Bitmap output = Bitmap.createbitmap (Bitmap.getwidth (), Bitmap
. GetHeight (), config.argb_8888);
Canvas canvas = new canvas (output);
final int color = 0xff424242;
Final Paint paint = new paint ();
Final rect rect = new Rect (0, 0, bitmap.getwidth (), Bitmap.getheight ());
Final RECTF RECTF = new RECTF (rect);
Paint.setantialias (TRUE);
Canvas.drawargb (0, 0, 0, 0);
Paint.setcolor (color);
Canvas.drawroundrect (RECTF, ROUNDPX, ROUNDPX, paint);
Paint.setxfermode (New Porterduffxfermode (mode.src_in));
Canvas.drawbitmap (Bitmap, rect, rect, paint);
return output;
}
Get the image method with reflection
public static Bitmap Createreflectionimagewithorigin (Bitmap Bitmap) {
Final int reflectiongap = 4;
int width = bitmap.getwidth ();
int height = bitmap.getheight ();
Matrix matrix = new Matrix ();
Matrix.prescale (1,-1);
Bitmap reflectionimage = Bitmap.createbitmap (Bitmap,
0, HEIGHT/2, width, HEIGHT/2, matrix, false);
Bitmap bitmapwithreflection = bitmap.createbitmap (width, height + height/2),
config.argb_8888);
Canvas canvas = new canvas (bitmapwithreflection);
Canvas.drawbitmap (bitmap, 0, 0, NULL);
Paint Deafalutpaint = new paint ();
Generated by Foxit PDF creator©foxit Software
Http://www.foxitsoftware.com for evaluation only.
Canvas.drawrect (0, height,width,height + reflectiongap,
Deafalutpaint);
Canvas.drawbitmap (reflectionimage, 0, height + reflectiongap, null);
Paint paint = new paint ();
LinearGradient shader = new LinearGradient (0,
Bitmap.getheight (), 0, Bitmapwithreflection.getheight ()
+ Reflectiongap, 0X70FFFFFF, 0X00FFFFFF, Tilemode.clamp);
Paint.setshader (shader);
Set the Transfer mode to being Porter Duff and destination in
Paint.setxfermode (New Porterduffxfermode (mode.dst_in));
Draw a rectangle using the paint with our linear gradient
Canvas.drawrect (0, height, width, bitmapwithreflection.getheight ()
+ reflectiongap, paint);
return bitmapwithreflection;
}
Android Bitmap Usage Summary