Some simple operations for Android bitmap

Source: Internet
Author: User

Original link: http://www.2cto.com/kf/201312/265180.html


All of them are snippets of code that need to be pasted directly past

/** get drawable picture   can loop   1. Picture name    2.drawable 3. Package name      **/    int imgid = Getresources (). Getidentifier ("Ic_ Launcher "," drawable "," com.example.anywight ");    Text.setbackgroundresource (Imgid);

/** by Image ID Bitmap  **/    Bitmap bitmap=bitmapfactory.decoderesource (getresources (), R.drawable.ic_launcher) ;

/** View turn Bitmap **/public static Bitmap convertviewtobitmap (view view, int bitmapwidth, int bitmapheight) {         Bitmap bit Map = Bitmap.createbitmap (Bitmapwidth, Bitmapheight, Bitmap.Config.ARGB_8888);    View.draw (New Canvas (bitmap));    return bitmap;}
/** convert control to Bitmap **/public static Bitmap convertviewtobitmap (view view) {// Open the image cache view.setdrawingcacheenabled (true);//The measure and layout methods must be called to successfully save the visual component's to PNG image File//measure the View size view.measure ( Measurespec.makemeasurespec (0, measurespec.unspecified), Measurespec.makemeasurespec (0, MeasureSpec.UNSPECIFIED)) ;//Send location and size to view and all of its sub-viewview.layout (0, 0, view.getmeasuredwidth (), View.getmeasuredheight ());//Get Visual Component bitmap Bitmap = View.getdrawingcache (); return bitmap;} 
<span style= "font-family:arial, Helvetica, Sans-serif;" ></span><pre name= "code" class= "java" >public static Bitmap Getbitmapfromview (view view) {    Bitmap Returnedbitmap = Bitmap.createbitmap (View.getwidth (), View.getheight (), Bitmap.Config.ARGB_8888);    Canvas canvas = new canvas (returnedbitmap);    drawable bgdrawable = View.getbackground ();    if (bgdrawable! = null)        Bgdrawable.draw (canvas);    else        Canvas.drawcolor (color.white);    View.draw (canvas);    return returnedbitmap;}

/** gets the code for the Bitmap object of the screen as follows **/public Bitmap getscreenpic (view view) {View Rootview = View.getrootview ();    Rootview.setdrawingcacheenabled (TRUE);    Rootview.builddrawingcache (); Do not understand why here return an empty, there is a post that cannot be called in the Oncreat method//Measurement view size rootview.measure (Measurespec.makemeasurespec (0, Measurespec.unspec    ified), Measurespec.makemeasurespec (0, measurespec.unspecified));    Send location and dimensions to view and all of its sub-view rootview.layout (0, 0, rootview.getmeasuredwidth (), Rootview.getmeasuredheight ());    Workaround, after invoking the above measure and layout methods, the return value is no longer empty//if you want to create a fixed length and width?    Bitmap Bitmap = Rootview.getdrawingcache ();    Rootview.destroydrawingcache ();  return bitmap;} /** drawable→bitmap **/public static Bitmap drawabletobitmap (drawable drawable) {Bitmap Bitmap = Bitmap.createbi TMap (Drawable.getintrinsicwidth (), Drawable.getintrinsicheight (), drawable.getopacity ()! = Pixelformat.opaque?    Bitmap.Config.ARGB_8888:Bitmap.Config.RGB_565);    Canvas canvas = new canvas (bitmap); Canvas.setBitmap (BITMAP);    Drawable.setbounds (0, 0, drawable.getintrinsicwidth (), Drawable.getintrinsicheight ());    Drawable.draw (canvas); return bitmap; }/** bitmap→drawable **/public static drawable bitmaptodrawable (Context context,string filename) {bitmap imag       e = null;          Bitmapdrawable DDD = null;              try {Assetmanager am = context.getassets ();              InputStream is = am.open (filename);              Image = Bitmapfactory.decodestream (IS);              DDD = new Bitmapdrawable (context.getresources (), image);          Is.close ();   } catch (Exception e) {} return DDD; }/** byte[]→bitmap **/public static Bitmap bytetodrawable (Context context,byte[] bb) {Bitmap pp = Bitmapfactory.d     Ecodebytearray (BB, 0, Bb.length);  Return pp;} /** bitmap→byte[]**/public Static byte[] Bitmaptobyte (Bitmap Bitmap) {Bytearrayoutputstream BAOs = new Bytearra      Youtputstream (); Bitmap.compress (Bitmap.CompressFormat.PNG, +, BAOs);    byte[] yy = Baos.tobytearray ();  return yy;} /** convert text to bitmap **/public static bitmap createtxtimage (String txt, int txtsize) {Bitmap mbmptest = Bitmap.cre    Atebitmap (Txt.length () * txtsize + 4, Txtsize + 4, config.argb_8888);    Canvas canvastemp = new canvas (mbmptest);    Paint p = new paint ();    P.setantialias (TRUE);    P.setcolor (Color.White);    P.settextsize (txtsize);    Canvastemp.drawtext (TXT, 2, txtSize-2, p); return mbmptest; The/** display Bitmap is scaled **/public Bitmap Bitmapscanel (Context context) {//Openrawresource gets a InputStream object I      Nputstream InputStream = Context.getresources (). Openrawresource (R.id.backageground);      Create a Bitmapdrawable object from a inputstream bitmapdrawable drawable = new bitmapdrawable (InputStream);      Get Bitmap object Bitmap Bitmap = Drawable.getbitmap () by Bitmapdrawable object;      Create thumbnails with bitmap objects bitmap = Thumbnailutils.extractthumbnail (Bitmap, 40, 40);  return bitmap; }/** putLarge shrink picture **/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; }/** method for getting rounded pictures **/public static Bitmap Getroundedcornerbitmap (Bitmap bitmap,float roundpx) {Bitmap OUTP      UT = Bitmap.createbitmap (Bitmap.getwidth (), Bitmap. GetHeight (), config.argb_8888);       Canvas canvas = new canvas (output);      final int color = 0xff424242;      Final Paint paint = new paint ();      Final rect rect = new Rect (0, 0, bitmap.getwidth (), Bitmap.getheight ());       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; /** bitmap is clipped **/public bitmap bitmapclip (context context, int ID, int x, int y) {Bitmap map = Bitma     Pfactory.decoderesource (Context.getresources (), id);     Map = Bitmap.createbitmap (map, x, y, 120, 120);   return map;}     /** * Image Reflection effect */public static Bitmap createreflectedimage (Bitmap originalimage) {final int reflectiongap = 4;    int width = originalimage.getwidth ();     int height = originalimage.getheight ();    Matrix matrix = new Matrix ();     Matrix.prescale (1,-1);    Create a Bitmap with the flip matrix applied to it.            We only want the bottom half of the image Bitmap Reflectionimage = Bitmap.createbitmap (originalimage, 0,     HEIGHT/2, Width, HEIGHT/2, matrix, false); Create a new bitmap with same width but taller to fit reflection bitmap Bitmapwithreflection = Bitmap.createbitmap (width, (height + height/2), config.argb_8888); Create a new canvas with the bitmap this ' s big enough for//the image plus gap plus reflection canvas canvas = N    EW Canvas (bitmapwithreflection);    Draw in the original image Canvas.drawbitmap (originalimage, 0, 0, NULL);    Draw in the gap paint Defaultpaint = new paint ();    Canvas.drawrect (0, height, width, height + reflectiongap, defaultpaint);     Draw in the Reflection Canvas.drawbitmap (reflectionimage, 0, height + reflectiongap, null);    Create a shader that's a linear gradient that covers the reflection paint paint = new paint ();                    LinearGradient shader = new LinearGradient (0, Originalimage.getheight (), 0, Bitmapwithreflection.getheight ()    + Reflectiongap, 0X70FFFFFF, 0X00FFFFFF, Tilemode.clamp);    Set the paint to use this shader (linear gradient) 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, height, width, bitmapwithreflection.get     Height () + reflectiongap, paint); return bitmapwithreflection; }



Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.

Some simple operations for Android bitmap

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.