Circleimagemanager-class for converting an image into a circular image

Source: Internet
Author: User
Package COM. kale. utils; import android. content. context; import android. graphics. bitmap; import android. graphics. bitmapfactory; import android. graphics. canvas; import android. graphics. paint; import android. graphics. porterduxfermode; import android. graphics. rect; import android. graphics. rectf; import android. graphics. bitmap. config; import android. graphics. porterduff. mode; import android. graphics. drawable. bitm Apdrawable; import android. graphics. drawable. drawable; public class circleimagemanager {/*** @ Param context object * @ Param resid image resource ID * @ return a circular drawable object */public static drawable getcircledrawable (context, int resid) {Bitmap bitmap = getcirclebitmap (context, resid); drawable = new bitmapdrawable (context. getresources (), bitmap); Return drawable;}/*** @ Param context * @ par Am resid * @ return: converted bitmap object */public static bitmap getcirclebitmap (context, int resid) {Bitmap bitmap = bitmapfactory. decoderesource (context. getresources (), resid); int width = bitmap. getwidth (); int Height = bitmap. getheight (); float roundpx; float left, top, right, bottom, dst_left, dst_top, dst_right, dst_bottom; If (width <= height) {roundpx = width/2; left = 0; Top = 0; Right = Width; bottom = width; Height = width; dst_left = 0; dst_top = 0; dst_right = width; dst_bottom = width;} else {roundpx = height/2; float clip = (width-height)/2; left = clip; Right = width-clip; Top = 0; bottom = height; width = height; dst_left = 0; dst_top = 0; dst_right = height; dst_bottom = height;} bitmap output = bitmap. createbitmap (width, height, config. argb_8888); canvas can Vas = new canvas (output); Final paint = new paint (); Final rect src = new rect (INT) left, (INT) Top, (INT) Right, (INT) Bottom); Final rect DST = new rect (INT) dst_left, (INT) dst_top, (INT) dst_right, (INT) dst_bottom ); final rectf = new rectf (DST); paint. setantialias (true); // you can specify a paint brush that does not have a sawtooth canvas. drawargb (0, 0, 0, 0); // fill the entire canvas. // you can draw circles, drawrounrect, and drawcircle canvas in either of the following ways. drawroundrec T (rectf, roundpx, roundpx, paint); // draw a rounded rectangle. The first parameter is the graphic display area, the second and third parameters are the radius of the horizontal and vertical rounded corners. // Canvas. drawcircle (roundpx, paint); paint. setxfermode (New porterduduxfermode (mode. src_in); // set the pattern when two images are intersecting, refer to the http://trylovecatch.iteye.com/blog/1189452 canvas. drawbitmap (bitmap, SRC, DST, paint); // In mode. in src_in mode, merge bitmap with the draw circle 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.