First, related concepts
1, drawable is a can be drawn object, it may be a bitmap (bitmapdrawable), it may be a graph (shapedrawable), there may be a layer (layerdrawable), we based on the demand for paint, Create the appropriate object to draw
2, canvas canvas, the destination area of the drawing, for drawing
3, Bitmap bitmap, for the processing of graphs
4. Matrix matrices
Second, Bitmap1. Get bitmap from the resources
1 Resources res = getresources (); 2 Bitmap BMP = Bitmapfactory.decoderesource (res, r.drawable.icon);
2, bitmap→byte[]
1 Public byte [] bitmap2bytes (Bitmap BM) {2 New Bytearrayoutputstream (); 3 Bm.compress (Bitmap.CompressFormat.PNG,BAOs); 4 return Baos.tobytearray (); 5 }
3, Byte[]→bitmap
1 Public Bitmap bytes2bimap (byte[] b) {2 if (b.length! = 0) {3 return Bitmapfactory.decodebytearray (b, 0, b.length); 4 Else {5 return NULL ; 6 }7 }
4, Bitmap zoom
1 Public StaticBitmap Zoombitmap (Bitmap Bitmap,intWidthintheight) {2 intW =bitmap.getwidth ();3 inth =bitmap.getheight ();4Matrix Matrix =NewMatrix ();5 floatScaleWidth = ((float) Width/W);6 floatScaleHeight = ((float) Height/h);7 Matrix.postscale (ScaleWidth, scaleheight);8Bitmap newbmp = Bitmap.createbitmap (Bitmap, 0, 0, W, h, Matrix,true);9 returnnewbmp;Ten}
5. Convert drawable to bitmap
1 Public StaticBitmap Drawabletobitmap (drawable drawable) {2 //take the length of the drawable width3 intW =drawable.getintrinsicwidth ();4 inth =drawable.getintrinsicheight ();5 6 //take drawable color format7Bitmap.config Config = drawable.getopacity ()! = Pixelformat.opaque?Bitmap.Config.ARGB_88888 : Bitmap.Config.RGB_565;9 //establish corresponding bitmapTenBitmap Bitmap =Bitmap.createbitmap (w, H, config); One //Create a canvas for the corresponding bitmap ACanvas Canvas =NewCanvas (bitmap); -Drawable.setbounds (0, 0, W, h); - //draw the drawable content to the canvas the Drawable.draw (canvas); - returnbitmap; -}
6. Get fillet picture
Public StaticBitmap Getroundedcornerbitmap (Bitmap Bitmap,floatroundpx) { intW =bitmap.getwidth (); inth =bitmap.getheight (); Bitmap Output=Bitmap.createbitmap (W, H, config.argb_8888); Canvas Canvas=NewCanvas (output); Final intcolor = 0xff424242; FinalPaint paint =NewPaint (); FinalRect rect =NewRect (0, 0, W, h); FinalRECTF RECTF =NewRECTF (rect); Paint.setantialias (true); Canvas.drawargb (0, 0, 0, 0); Paint.setcolor (color); Canvas.drawroundrect (RECTF, ROUNDPX, ROUNDPX, paint); Paint.setxfermode (NewPorterduffxfermode (mode.src_in)); Canvas.drawbitmap (Bitmap, rect, rect, paint); returnoutput; }
7. Get a picture with reflection
1 Public StaticBitmap createreflectionimagewithorigin (Bitmap Bitmap) {2 Final intReflectiongap = 4;3 intW =bitmap.getwidth ();4 inth =bitmap.getheight ();5 6Matrix Matrix =NewMatrix ();7Matrix.prescale (1,-1);8 9Bitmap reflectionimage = Bitmap.createbitmap (Bitmap, 0, H/2, W,TenH/2, Matrix,false); One ABitmap bitmapwithreflection = Bitmap.createbitmap (W, (H + H/2), - config.argb_8888); - theCanvas Canvas =NewCanvas (bitmapwithreflection); -Canvas.drawbitmap (bitmap, 0, 0,NULL); -Paint Deafalutpaint =NewPaint (); -Canvas.drawrect (0, H, W, H +Reflectiongap, deafalutpaint); + -Canvas.drawbitmap (reflectionimage, 0, H + reflectiongap,NULL); + APaint paint =NewPaint (); atLinearGradient shader =NewLinearGradient (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 (NewPorterduffxfermode (mode.dst_in)); in //Draw a rectangle using the paint with our linear gradient -Canvas.drawrect (0, H, W, Bitmapwithreflection.getheight () to+reflectiongap, paint); + - returnbitmapwithreflection; the}
Third, drawable1, bitmap converted into drawable
// XXX Based on your situation to obtain New bitmapdrawable (GetResource (), BM); Because Btimapdrawable is a subclass of drawable, it is ultimately possible to use the BD object directly.
2,, drawable zoom
1 Public StaticDrawable zoomdrawable (drawable drawable,intWinth) {2 intwidth =drawable.getintrinsicwidth ();3 intHeight =drawable.getintrinsicheight ();4 //drawable conversion into bitmap5Bitmap oldbmp =Drawabletobitmap (drawable);6 //Create a Matrix object for manipulating pictures7Matrix Matrix =NewMatrix ();8 //Calculating the zoom ratio9 floatSX = ((float) w/width);Ten floatSy = ((float) H/height); One //set the zoom ratio A Matrix.postscale (SX, SY); - //Create a new bitmap, whose content is a scaled graph of the original bitmap -Bitmap newbmp = Bitmap.createbitmap (oldbmp, 0, 0, width, height, theMatrixtrue); - return Newbitmapdrawable (newbmp); -}
Android in bitmap and drawable