) Bitmap usage Summary

Source: Internet
Author: User
Tags foxit software

(Conversion) bitmap usage Summary

00:12:56 | category:
Android UI | Tag: | large font size, small/medium subscription

Bitmap usage Summary

1. drawable → bitmap

Public static bitmap drawabletobitmap (drawable ){

Bitmap bitmap = bitmap

. Createbitmap (

Drawable. getintrinsicwidth (),

Drawable. getintrinsicheight (),

Drawable. getopacity ()! = Pixelformat. opaque? Bitmap. config. argb_8888

: Bitmap. config. rgb_565 );

Canvas canvas = newcanvas (Bitmap );

// Canvas. setbitmap (Bitmap );

Drawable. setbounds (0, 0, drawable. getintrinsicwidth (),

Drawable. getintrinsicheight ());

Drawable. Draw (canvas );

Return bitmap;

}


2. Obtain bitmap from a resource

Resources res = getresources ();

Bitmap BMP = bitmapfactory. decoderesource (Res, R. drawable. PIC );


3. Bitmap → byte []

Private byte [] bitmap2bytes (Bitmap BM ){

Bytearrayoutputstream baos = new bytearrayoutputstream ();

BM. Compress (bitmap. compressformat. PNG, 100, baos );

Return baos. tobytearray ();

}


4. byte [] → bitmap

Private bitmap bytes2bimap (byte [] B ){

If (B. length! = 0 ){

Return bitmapfactory. decodebytearray (B, 0, B. Length );

}

Else {

Return NULL;

}

}


5. Save bitmap

Static Boolean savebitmap2file (bitmap BMP, string filename ){

Compressformat format = bitmap. compressformat. JPEG;

Int Quality = 100;

Outputstream stream = NULL;

Try {

Stream = new fileoutputstream ("/sdcard/" + filename );

} Catch (filenotfoundexception e ){

// Todo auto-generated Catch Block

E. printstacktrace ();

}

Return BMP. Compress (format, quality, stream );

}


6. Scale the image as needed

// Image Source

Bitmap Bm = bitmapfactory. decodestream (getresources ()

. Openrawresource (R. drawable. Dog ));

// Obtain the image width and height

Int width = BM. getwidth ();

Int Height = BM. getheight ();

// Set the desired size

Int newwidth = 320;

Int newheight = 480;

// Calculate the scaling ratio

Float scalewidth = (float) newwidth)/width;

Float scaleheight = (float) newheight)/height;

// Obtain the matrix parameter to be scaled.

Matrix matrix = new matrix ();

Matrix. postscale (scalewidth, scaleheight );

// Get a new image

Bitmap newbm = bitmap. createbitmap (BM, 0, 0, width, height, matrix,

True );

// Place it on the canvas

Canvas. drawbitmap (newbm, 0, 0, paint );


7. Usage of Bitmap

Bitmapfactory. Options option = new bitmapfactory. Options ();

Option. insamplesize = 2; // set the image to 1/2 in width to prevent memory overflow.

Bitmap Bm = bitmapfactory. decodefile ("", option); // file stream

URL url = new URL ("");

Inputstream is = URL. openstream ();

Bitmap Bm = bitmapfactory. decodestream (is );


 

// Zoom in and out the image

Public static bitmap zoombitmap (Bitmap bitmap, int W, int h ){

Int width = bitmap. getwidth ();

Int Height = bitmap. getheight ();

Matrix matrix = new matrix ();

Float scalewidht = (float) W/width );

Float scaleheight = (float) h/height );

Matrix. postscale (scalewidht, scaleheight );

Bitmap newbmp = bitmap. createbitmap (bitmap, 0, 0, width, height, matrix,

True );

Return newbmp;

}


// Converts drawable to bitmap

Public static bitmap drawabletobitmap (drawable ){

Int width = drawable. getintrinsicwidth ();

Int Height = drawable. getintrinsicheight ();

Bitmap bitmap = bitmap. createbitmap (width, height,

Drawable. getopacity ()! = Pixelformat. opaque? Bitmap. config. argb_8888

: Bitmap. config. rgb_565 );

Canvas canvas = new canvas (Bitmap );

Drawable. setbounds (0, 0, width, height );

Drawable. Draw (canvas );

Return bitmap;

}



// Obtain the rounded corner Image

Public static bitmap getroundedcornerbitmap (Bitmap bitmap, float roundpx ){

Bitmap output = bitmap. createbitmap (bitmap. getwidth (), bitmap

. Getheight (), config. argb_8888 );

Canvas canvas = new canvas (output );

Final int color = 0xff0000242;

Final paint = new paint ();

Final rect = new rect (0, 0, bitmap. getwidth (), bitmap. getheight ());

Final rectf = new rectf (rect );

Paint. setantialias (true );

Canvas. drawargb (0, 0, 0, 0 );

Paint. setcolor (color );

Canvas. drawroundrect (rectf, roundpx, roundpx, paint );

Paint. setxfermode (New porterduxfermode (mode. src_in ));

Canvas. drawbitmap (bitmap, rect, rect, paint );

Return output;

}



// Method for obtaining images with reflections

Public static bitmap createreflectionimagewithorigin (Bitmap bitmap ){

Final int reflectiongap = 4;

Int width = bitmap. getwidth ();

Int Height = bitmap. getheight ();

Matrix matrix = new matrix ();

Matrix. prescale (1,-1 );

Bitmap reflectionimage = bitmap. createbitmap (bitmap,

0, height/2, width, height/2, matrix, false );

Bitmap bitmapwithreflection = bitmap. createbitmap (width, (height + height/2 ),

Config. argb_8888 );

Canvas canvas = new canvas (bitmapwithreflection );

Canvas. drawbitmap (bitmap, 0, 0, null );

Paint deafalutpaint = new paint ();

Generated by Foxit PDF Creator? Foxit Software

A http://www.foxitsoftware.com for evaluation only.

Canvas. drawrect (0, height, width, height + reflectiongap,

Deafalutpaint );

Canvas. drawbitmap (reflectionimage, 0, height + reflectiongap, null );

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 porterduxfermode (mode. dst_in ));

// Draw a rectangle using the paint with our linear gradient

Canvas. drawrect (0, height, width, bitmapwithreflection. getheight ()

+ Reflectiongap, paint );

Return bitmapwithreflection;

}

}

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.