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
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     static Bitmap Zoombitmap (Bitmap Bitmap, int width, int height) {
2 int w = bitmap.getwidth ();
3 int h = bitmap.getheight ();
4 Matrix matrix = new Matrix ();
5 Float ScaleWidth = ((float) width/w);
6 Float ScaleHeight = ((float) height/h);
7 Matrix.postscale (ScaleWidth, ScaleHeight);
8 Bitmap newbmp = Bitmap.createbitmap (Bitmap, 0, 0, W, h, Matrix, true);
9 return newbmp;
Ten }

5. Convert drawable to bitmap
1 public static Bitmap Drawabletobitmap (drawable drawable) {
2 //Take Drawable's length and width
3 int w = drawable.getintrinsicwidth ();
4 int h = drawable.getintrinsicheight ();

6 //Take drawable color format
7 Bitmap.config Config = drawable.getopacity ()! = Pixelformat.opaque? Bitmap.Config.ARGB_8888
8 : Bitmap.Config.RGB_565;
9 //establish corresponding bitmap
Ten Bitmap Bitmap = Bitmap.createbitmap (w, H, config);
//Build a canvas for bitmap
Canvas Canvas = new canvas (bitmap);
drawable.setbounds (0, 0, W, h);
//Draw drawable content to the canvas
Drawable.draw (canvas);
bitmap return;
+ }

6. Get Fillet picture
1 public     static Bitmap Getroundedcornerbitmap (Bitmap Bitmap, float roundpx) {
2 int w = bitmap.getwidth ();
3 int h = bitmap.getheight ();
4 Bitmap output = Bitmap.createbitmap (W, H, config.argb_8888);
5 Canvas Canvas = new canvas (output);
6 Final int color = 0xff424242;
7 Final Paint paint = new paint ();
8 Final Rect rect = new Rect (0, 0, W, h);
9 Final RECTF RECTF = new RECTF (rect);
Ten 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;
(+ }

7. Get a picture with reflection

1 public static Bitmap Createreflectionimagewithorigin (Bitmap Bitmap) {
2 Final int reflectiongap = 4;
3 int w = bitmap.getwidth ();
4 int h = bitmap.getheight ();
5
6 Matrix matrix = new Matrix ();
7 Matrix.prescale (1,-1);
8
9 Bitmap reflectionimage = Bitmap.createbitmap (Bitmap, 0, H/2, W,
Ten H/2, Matrix, false);
11
Bitmap bitmapwithreflection = Bitmap.createbitmap (W, (H + h/2),
config.argb_8888);
14
Canvases canvas = new canvas (bitmapwithreflection);
Canvas.drawbitmap (bitmap, 0, 0, NULL);
Deafalutpaint paint = new paint ();
Canvas.drawrect (0, H, W, H + reflectiongap, deafalutpaint);
19
Canvas.drawbitmap (reflectionimage, 0, H + reflectiongap, null);
21st
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 be 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, H, W, Bitmapwithreflection.getheight ()
+ reflectiongap, paint);
32
Bitmapwithreflection return;
34}

Three, Drawable1, bitmap converted into drawable
1 Bitmap bm=xxx; XXX based on your situation to obtain

3 because Btimapdrawable is a subclass of drawable, it is finally possible to use the BD object directly.
2, Drawable Zoom

1 public     static drawable zoomdrawable (drawable drawable, int w, int h) {
2 int width = drawable.getintrinsicwidth ();
3 int height = drawable.getintrinsicheight ();
4 //drawable convert to bitmap
5 Bitmap oldbmp = Drawabletobitmap (drawable);
6 //Create a Matrix object for manipulating pictures
7 Matrix matrix = new Matrix ();
8 //Calculate the zoom ratio
9 Float SX = ((float) w/width);
Ten Float sy = ((float) h/height);
One //Set zoom ratio
Matrix.postscale (SX, SY);
Create a new bitmap, the content is the original bitmap after the scaling of the figure
Bitmap newbmp = Bitmap.createbitmap (oldbmp, 0, 0, width, height,
matrix, True);
new Bitmapdrawable (newbmp);
+ }

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.