Android in bitmap and drawable

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

Second, Bitmap

1. 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. Bytearrayoutputstream BAOs = 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 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

Related Article

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.