Examples of special graphics generation in Android

Source: Internet
Author: User

Import Android.graphics.bitmap;import Android.graphics.canvas;import Android.graphics.color;import Android.graphics.lineargradient;import Android.graphics.paint;import Android.graphics.path;import Android.graphics.porterduff;import Android.graphics.porterduffxfermode;import Android.graphics.Rect;import Android.graphics.rectf;import Android.graphics.shader;import Android.graphics.bitmap.config;import Android.graphics.paint.align;import Android.graphics.porterduff.mode;import android.graphics.drawable.Drawable; public class drawutil{/** * Draw a graph with a semi-circle with title text in the middle, a semicircle that can set the shadow and the top of the rectangle to be projected * * @param text * title text (set to center effect of two characters here) * @p Aram Radio * Circle RADIUS * @param shadowwidth * Shadow Width * @param ISWITHRECTF * Whether there is a rectangular projection * @param r Ectfcolor * Rectangle Projection Color * @param paint * Brush Object * @return */public static Bitmap Drawhalfcirclewithtext (String text, int radio,int shadowwidth, boolean iswithrectf, int rectfcolor, paint paint) {Bitmap Bitmap = Bitmap.createbitmap (rAdio * 2, Radio * 2 + shadowwidth, config.argb_8888); Canvas canvas = new canvas (bitmap);//Draw a sector with a center circle, here is a semicircle canvas.drawarc (new RECTF (0, 0, Radio * 2, Radio * 2), A. paint);//Whether you need to draw a rectangular projection on the top of the semicircle if (ISWITHRECTF) {paint = Reset (Paint);p aint.setcolor (Rectfcolor); Bitmap BMP = DRAWRECTF (Radio * 2, RADIO/8, paint);p aint = reset (Paint);p Aint.setxfermode (New Porterduffxfermode ( PorterDuff.Mode.SRC_ATOP));//Remove rectangular projection and semicircular disjoint portions of Canvas.drawbitmap (BMP, 0, radio, paint);} Else{lineargradient lg=new lineargradient (0,0,0,radio/8,0x44000000,0x00000000,shader.tilemode.mirror);p aint = Reset (Paint);p Aint.setshader (LG); Bitmap BMP = DRAWRECTF (Radio * 2, RADIO/8, paint);p aint = reset (Paint);p Aint.setxfermode (New Porterduffxfermode ( PorterDuff.Mode.SRC_ATOP));//Remove rectangular projection and semicircular disjoint portions of Canvas.drawbitmap (BMP, 0, radio, paint);} Draw text on the semicircular paint = Reset (Paint);p Aint.setcolor (color.white);p aint.settextsize (Radio * 5/12);p aint.settextalign ( Align.center);p Aint.setxfermode (New Porterduffxfermode (PorterDuff.Mode.SRC_OVER)); cAnvas.drawtext (text, Radio, radio * 5/3, paint); bitmap = Bitmap.createbitmap (bitmap, 0, Radio, Radio * 2, radio+ SHADOWW idth); return bitmap;} /** * To draw a specified number of triangles based on the width of the triangle * * @param screenwidth * Width * @param triangleheight * sawtooth Height * @param trian Glenum * Number of triangles * @param isdown * triangle vertex orientation, true indicates down, false indicates up * @param paint * Brush Object * @ Return returns Bitmap object */public static Bitmap drawtriangle (float screenwidth, float Triangleheight,int trianglenum, Boolean is Down,paint Paint) {float trianglewidth = screenwidth/trianglenum; Bitmap Bitmap = bitmap.createbitmap ((int) screenwidth, (int) triangleheight, config.argb_8888); Canvas canvas = new canvas (bitmap); Path PATH = new Path (), if (Isdown) {path.moveto (0, 0), for (int i = 0; i < Trianglenum * 2; i++) {if (i 2 = = 0) {Path.line to ((TRIANGLEWIDTH/2) * (i + 1), triangleheight);} else if (i% 2 = = 1) {Path.lineto ((TRIANGLEWIDTH/2) * (i + 1), 0);}}} Else{path.moveto (0, Triangleheight); for (int i = 0; i< Trianglenum * 2; i++) {if (i% 2 = = 0) {Path.lineto ((TRIANGLEWIDTH/2) * (i + 1), 0);} else if (i% 2 = = 1) {Path.lineto ((TRIANGLEWIDTH/2) * (i + 1), triangleheight);}}} Path.close (); Canvas.drawpath (path, paint); return bitmap;} /** * Draw a lace with a specified number of semi-circles * @param width lace length * @param num semicircle number * @param paint Brush Object * @return */public static Bitmap Drawmul thalfcircle (int width, int num,paint Paint) {int radio = (width/num)/2; Bitmap Bitmap = Bitmap.createbitmap (width, radio, config.argb_8888); Canvas canvas = new canvas (bitmap), for (int i = 0; i < num; i++) {Canvas.drawbitmap (Drawhalfcircle (radio, paint), radio*2 *i, 0, paint);} return bitmap;} /** * Draw a rectangle * @param width * @param height * @param color * @param paint * @return */public static Bitmap DRAWRECTF (float width, float height, paint paint) {Bitmap Bitmap = bitmap.createbitmap ((int) width, (int) height, config.argb_8888); Canvas canvas = new canvas (bitmap), canvas.drawrect (0, 0, width, height, paint); return bitmap;} /** * Draw a semicircle * * @param radio * RADIUS * @param paint * @return */public static Bitmap drawhalfcircle (int radio, paint paint) {Bitmap Bitmap = Bi Tmap.createbitmap ((int) (radio * 2), (int) (radio * 2), config.argb_8888); Canvas canvas = new canvas (bitmap), Canvas.drawarc (new RECTF (0, 0, Radio * 2, Radio * 2), True,paint = Bi, bitmap); Tmap.createbitmap (bitmap, 0, Radio, Radio * 2, radio); return bitmap;}  /** * Cut to a circle according to the short edge of the picture and draw a certain width around the white edge * @param bitmap * @param ringwidth * @param radio * @param paint * @return */public static Bitmap drawbitmapwithring (Bitmap bitmap,int ringwidth,paint Paint) {Bitmap Roundbitmap = Toroundbitmap (Bitmap); int Radio = Roundbitmap.getwidth ()/2; Bitmap _bitmap = Bitmap.createbitmap ((radio+ringwidth) * 2, (radio+ringwidth) * 2, config.argb_8888); Canvas canvas = new Canvas (_bitmap), Canvas.drawcircle ((Radio+ringwidth), (Radio+ringwidth), (radio+ringwidth), paint) ;p Aint.setxfermode (New Porterduffxfermode (PorterDuff.Mode.SRC_OVER)); Canvas.drawbitmap (Roundbitmap, Ringwidth, Ringwidth, PaiNT); return _bitmap;}    /** * Crop the image to a circle * * @param bitmap * @return * * public static bitmap Toroundbitmap (bitmap bitmap)    {if (bitmap = = null) {return null;        } 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;            top = 0;            bottom = width;            left = 0;            right = 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 canvas = new canvas (output);        final int color = 0xff424242;        Final Paint 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 RECTF = new RECTF (DST);        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, SRC, DST, paint);    return output; } public static Bitmap Darwshadow (Bitmap bitmap,int color,int padding) {int width = bitmap.getwidth () +        Padding        int height = bitmap.getheight () + padding; Bitmap output = BitmAp.createbitmap (width, height, config.argb_8888);        Canvas canvas = new canvas (output);        Paint paint = new paint ();        Paint = reset (paint);        Paint.setcolor (color);        Canvas.drawbitmap (Bitmap, PADDING/2, PADDING/2, NULL);        Canvas.drawroundrect (New RECTF (0, 0, width, height), padding/2,padding/2,paint);    return output; } private static Paint reset (Paint paint) {paint.reset ();p Aint.setantialias (True);p Aint.setstyle (Paint.Style.FILL); return paint;}}

Examples of special graphics generation 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.