Bitmap,byte[],drawable Mutual Transformation in Android

Source: Internet
Author: User

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
Ii. Bitmap1, obtaining bitmap from resources
Resources res == Bitmapfactory.decoderesource (res, r.drawable.icon);
2, bitmap→byte[]
 Public byte   new, BAOs);  return  Baos.tobytearray ();}
3, Byte[]→bitmap
 Public Bitmap bytes2bimap (byte[] b) {        if0) {            return0  , b.length);         Else {            returnnull;        }    }
4, Bitmap zoom
 Public StaticBitmap Zoombitmap (Bitmap Bitmap,intWidthintheight) {        intW =bitmap.getwidth (); inth =bitmap.getheight (); Matrix Matrix=NewMatrix (); floatScaleWidth = ((float) Width/W); floatScaleHeight = ((float) Height/h);        Matrix.postscale (ScaleWidth, ScaleHeight); Bitmap newbmp= Bitmap.createbitmap (Bitmap,0,0, W, h, Matrix,true); returnnewbmp; }
5. Convert drawable to bitmap
 Public StaticBitmap Drawabletobitmap (drawable drawable) {//take the length of the drawable width        intW =drawable.getintrinsicwidth (); inth =drawable.getintrinsicheight (); //take drawable color formatBitmap.config Config = drawable.getopacity ()! = Pixelformat.opaque?Bitmap.Config.ARGB_8888:Bitmap.Config.RGB_565; //establish corresponding bitmapBitmap Bitmap =Bitmap.createbitmap (w, H, config); //Create a canvas for the corresponding bitmapCanvas Canvas =NewCanvas (bitmap); Drawable.setbounds (0,0, W, h); //draw the drawable content to the canvasDrawable.draw (canvas); returnbitmap; }
6. Get the 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); Finalintcolor =0xff424242; Final Paint Paint=NewPaint (); Final rect rect=NewRect (0,0, W, h); Final RECTF 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
 Public StaticBitmap createreflectionimagewithorigin (Bitmap Bitmap) {finalintReflectiongap =4; intW =bitmap.getwidth (); inth =bitmap.getheight (); Matrix Matrix=NewMatrix (); Matrix.prescale (1, -1); Bitmap Reflectionimage= Bitmap.createbitmap (Bitmap,0, H/2, W, H/2, Matrix,false); Bitmap bitmapwithreflection= Bitmap.createbitmap (W, (H + H/2), config.argb_8888); Canvas 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); Paint Paint=NewPaint (); LinearGradient 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 inPaint.setxfermode (NewPorterduffxfermode (mode.dst_in)); //Draw a rectangle using the paint with our linear gradientCanvas.drawrect (0, H, W, Bitmapwithreflection.getheight ()+reflectiongap, paint); returnbitmapwithreflection; }
Three, Drawable1, bitmap converted into drawable
Bitmap bm=xxx; XXX based on your situation to obtain

Because Btimapdrawable is a subclass of drawable, it is finally possible to use the BD object directly.
2, Drawable Zoom
 Public StaticDrawable zoomdrawable (drawable drawable,intWinth) {intwidth =drawable.getintrinsicwidth (); intHeight =drawable.getintrinsicheight (); //drawable conversion into bitmapBitmap oldbmp =Drawabletobitmap (drawable); //Create a Matrix object for manipulating picturesMatrix Matrix =NewMatrix (); //Calculating the zoom ratio        floatSX = ((float) w/width); floatSy = ((float) H/height); //set the zoom ratioMatrix.postscale (SX, SY); //Create a new bitmap, whose content is a scaled graph of the original bitmapBitmap newbmp = Bitmap.createbitmap (Oldbmp,0,0, width, height, matrix,true); return Newbitmapdrawable (newbmp); }

This article was transferred from dyh7077063 's blog http://dyh7077063.iteye.com/blog/970672

Bitmap,byte[],drawable Mutual Transformation in Android

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.