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, Bitmap
1. Get bitmap from the resources
- Resources res = getresources ();
- Bitmap BMP = Bitmapfactory.decoderesource (res, r.drawable.icon);
2, bitmap→byte[]
- Public byte[] Bitmap2bytes (Bitmap BM) {
- Bytearrayoutputstream BAOs = new Bytearrayoutputstream ();
- Bm.compress (Bitmap.CompressFormat.PNG, BAOs);
- return Baos.tobytearray ();
- }
3, Byte[]→bitmap
- Public Bitmap Bytes2bimap (byte[] b) {
- if (b.length! = 0) {
- return Bitmapfactory.decodebytearray (b, 0, b.length);
- } Else {
- return null;
- }
- }
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 Getroundedcornerbitmap (Bitmap Bitmap,floatRoun dPx) {2 intW =bitmap.getwidth ();3 inth =bitmap.getheight ();4Bitmap output =Bitmap.createbitmap (W, H, config.argb_8888);5Canvas Canvas =NewCanvas (output);6 Final intcolor = 0xff424242;7 FinalPaint paint =NewPaint ();8 FinalRect rect =NewRect (0, 0, W, h);9 FinalRECTF RECTF =NewRECTF (rect);TenPaint.setantialias (true); OneCanvas.drawargb (0, 0, 0, 0); A paint.setcolor (color); - Canvas.drawroundrect (RECTF, ROUNDPX, ROUNDPX, paint); -Paint.setxfermode (NewPorterduffxfermode (mode.src_in)); the canvas.drawbitmap (Bitmap, rect, rect, paint); - - returnoutput; -}
6. Get fillet picture
1 Public StaticBitmap Getroundedcornerbitmap (Bitmap Bitmap,
floatroundpx) {2 intW =bitmap.getwidth ();3 inth =bitmap.getheight ();4Bitmap output =Bitmap.createbitmap (W, H, config.argb_8888);5Canvas Canvas =NewCanvas (output);6 Final intcolor = 0xff424242;7 FinalPaint paint =NewPaint ();8 FinalRect rect =NewRect (0, 0, W, h);9 FinalRECTF RECTF =NewRECTF (rect);TenPaint.setantialias (true); OneCanvas.drawargb (0, 0, 0, 0); A paint.setcolor (color); - Canvas.drawroundrect (RECTF, ROUNDPX, ROUNDPX, paint); -Paint.setxfermode (NewPorterduffxfermode (mode.src_in)); the 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, drawable
1, bitmap converted into drawable
1 // XXX Based on your situation to obtain 2 New 3 because Btimapdrawable is a subclass of drawable, it is finally 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