Bitmap processing methods commonly used in Android

Source: Internet
Author: User

Collected a lot of bitmap related processing methods, almost all applied in the project, so special record!

Package Com.tmacsky.utils;import Java.io.bytearrayoutputstream;import Java.io.ioexception;import Android.content.context;import Android.content.res.resources;import Android.graphics.bitmap;import Android.graphics.bitmapfactory;import Android.graphics.canvas;import Android.graphics.matrix;import Android.graphics.paint;import Android.graphics.pixelformat;import Android.graphics.porterduffxfermode;import Android.graphics.rect;import Android.graphics.rectf;import Android.graphics.bitmap.config;import Android.graphics.porterduff.mode;import Android.graphics.drawable.bitmapdrawable;import Android.graphics.drawable.drawable;import Android.view.view;import Android.view.view.measurespec;public Class imageutils {//--->BITMAP related//reference website http://www.cnblogs.com/fighter/archive/2012/02/20/ android-bitmap-drawable.html//See blog: http://blog.sina.com.cn/s/blog_afb547c60101j7qn.html/** * View turn into bitmap * @param View * @return */public static Bitmap convertviewtobitmap (view view) {view.setdrawingcacheenabled (trUE); View.measure (Measurespec.makemeasurespec (0, measurespec.unspecified), Measurespec.makemeasurespec (0, measurespec.unspecified)); view.layout (0, 0, view.getmeasuredwidth (), View.getmeasuredheight ()); View.builddrawingcache (); return View.getdrawingcache ();} /** * Zoom drawable * @param drawable * @param w scale required width after scaling * @param h scaling Required Height * @return */public static drawable zoomdrawab        Le (drawable drawable, int w, int h) {int width = drawable.getintrinsicwidth ();        int height = drawable.getintrinsicheight ();        Drawable converted to Bitmap Bitmap oldbmp = Drawabletobitmap (drawable);        Create an action picture with the Matrix object matrix matrix = new Matrix ();        Calculate scale Float SX = ((float) w/width);        Float sy = ((float) h/height);        Set the scale Matrix.postscale (SX, SY); Create a new Bitmap, whose content is the scaled figure of the original Bitmap Bitmap newbmp = Bitmap.createbitmap (oldbmp, 0, 0, width, height, MA        Trix, True);    return new bitmapdrawable (newbmp); }/** * ZoomBitmap * @param oldbitmap input bitmap * @param newwidth * @param newheight * @return */public static bitmap Zoombitmap (Bitma P oldbitmap, int newwidth, int newheight) {//Get the picture's width of height int width = oldbitmap.getwidth (); int height = oldbitmap.getheight (); /calculate scale Float ScaleWidth = ((float) newwidth)/Width;float ScaleHeight = ((float) newheight)/height;//get the Matrix parameter m that you want to scale Atrix matrix = new Matrix (); Matrix.postscale (ScaleWidth, ScaleHeight);//Get new picture Bitmap NEWBM = Bitmap.createbitmap ( Oldbitmap, 0, 0, width, height, matrix,true); return NEWBM;} /** * Zoom network picture dependent on ZOOMBITMAP * @param img * @param newwidth * @param newheight * @return */public static Bitmap zoomimg (stri ng img, int newwidth, int newheight) {//Picture source Bitmap BM = Bitmapfactory.decodefile (IMG), if (null! = BM) {return Zoombitmap (b M, Newwidth, newheight);} return null;} /** * Zoom network picture dependent on Zoombitmap * @param context * @param img * @param newwidth * @param newheight * @return */public static Bi TMap zoomimg (Context context, String img, int newwidth,inT newheight) {//Picture source try {Bitmap BM = Bitmapfactory.decodestream (Context.getassets (). Open (IMG)); if (null! = BM) {return zoo Mbitmap (BM, newwidth, newheight);}} catch (IOException e) {//TODO auto-generated catch Blocke.printstacktrace ();} return null;}  /** * Determine if bitmap exists * @param bitmap * @return */public static Boolean bitmapavailable (bitmap bitmap) {return bitmap! = NULL && bitmap.getwidth () > 0 && bitmap.getheight () > 0;}  /** * drawable turn into Bitmap * @param drawable * @return */public static Bitmap drawabletobitmap (drawable drawable) {//        Take drawable of the long width int w = drawable.getintrinsicwidth ();        int h = drawable.getintrinsicheight (); Take drawable color format bitmap.config Config = drawable.getopacity ()! = Pixelformat.opaque?        Bitmap.Config.ARGB_8888:Bitmap.Config.RGB_565;        Establish corresponding bitmap bitmap bitmap = Bitmap.createbitmap (w, H, config);        Create a canvas for the corresponding bitmap canvases canvas = new canvas (bitmap); DrAwable.setbounds (0, 0, W, h);        Draw the drawable content to the canvas drawable.draw (canvas);    return bitmap; }/** * Bitmap converted to drawable * @param context * @param bitmap * @return */public static drawable bitmaptodrawable (Context cont Ext,bitmap Bitmap) {//Because btimapdrawable is a subclass of drawable and ultimately directly uses the BD object. Bitmapdrawable bd= New Bitmapdrawable (Context.getresources (), bitmap); return BD;} /** * Get Bitmap * FROM resources * @param context * @param req r.drawable.icon (eg.) * @return */public Bitmap getbitmapfromresources (Co  ntext Context,int req) {Resources res = context.getresources ();  Bitmap BMP = Bitmapfactory.decoderesource (res, req); return bmp;} /** * byte[]-Bitmap conversion */public Bitmap Bytes2bimap (byte[] b) {if (b.length! = 0) {return BITMAPF        Actory.decodebytearray (b, 0, b.length);        } else {return null; }}/** * bitmap->byte[] Conversion * @param BM * @return */public byte[] Bitmap2bytes (Bitmap BM) {Bytearrayoutputstream ba OS = new Bytearrayoutputstream (); BM.compress (Bitmap.CompressFormat.PNG, BAOs);    return Baos.tobytearray (); }/** * Get rounded picture * @param bitmap * @param roundpx radians * @return */public static bitmap Getroundedcornerbitmap (bitmap bitmap        , float roundpx) {int w = bitmap.getwidth ();        int h = bitmap.getheight ();        Bitmap output = Bitmap.createbitmap (W, H, config.argb_8888);        Canvas canvas = new canvas (output);        final int color = 0xff424242;        Final Paint paint = new paint ();        Final rect rect = new Rect (0, 0, W, h);        Final RECTF 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 Porterduffxfermode (mode.src_in));        Canvas.drawbitmap (Bitmap, rect, rect, paint);    return output; }}


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.