Example of generating special images in Android
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. porterduxfermode; 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 {/*** draws a semi-circle with the title text in the middle, you can set the shadow and top rectangular projection ** @ param text * Title text (this is set to the center effect of the two words) * @ param radio * Half circle radius * @ param shadowWidth * shadow width * @ param isWithRectf * whether there is a rectangular projection * @ param rectfColor * the color of the rectangular projection * @ param paint * 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 circle center, which is a semi-circle canvas. drawArc (new RectF (0, 0, radio * 2, radio * 2), 360,180, true, paint); // do you need to draw a rectangular projection on the top of the semi-circle if (isWithRectf) {paint = reset (paint); paint. setColor (rectfColor); Bitmap bmp = drawRectf (radio * 2, radio/8, paint); paint = reset (paint); paint. setXfermode (new porterduxfermode (PorterDuff. mode. SRC_ATOP); // remove the canvas that does not intersection the rectangular projection and the semi-circle. drawBitmap (bmp, 0, radio, paint);} else {LinearGradient lg = new LinearGradient (00000000, 0, radio/x 44000000,0 x, Shader. tileMode. MIRROR); paint = reset (paint); paint. setShader (lg); Bitmap bmp = drawRectf (radio * 2, radio/8, paint); paint = reset (paint); paint. setXfermode (new porterduxfermode (PorterDuff. mode. SRC_ATOP); // remove the canvas that does not intersection the rectangular projection and the semi-circle. drawBitmap (bmp, 0, radio, paint);} // draw the text paint = reset (paint); paint on the semi-circle. setColor (Color. WHITE); paint. setTextSize (radio * 5/12); paint. setTextAlign (Align. CENTER); paint. setXfermode (new porterduxfermode (PorterDuff. mode. SRC_OVER); canvas. drawText (text, radio, radio * 5/3, paint); bitmap = Bitmap. createBitmap (bitmap, 0, radio, radio * 2, radio + shadowWidth); return bitmap ;} /*** draw a specified number of triangles based on the width ** @ param screenWidth * width * @ param triangleHeight * sawtooth height * @ param triangleNum * Number of triangles * @ param isDown * orientation of triangle vertices, true indicates downward, false indicates upward * @ param paint * paint Brush object * @ return returns bitmap object */public static Bitmap drawtriangle (float screenWidth, float triangleHeight, int triangleNum, boolean isDown, 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. lineTo (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 half circles * @ param width the length of the lace * @ param num the number of half circles * @ param paint brush object * @ return */public static Bitmap drawMultHalfCircle (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 semi-circle ** @ param radio * radius * @ param paint * @ return */public static Bitmap drawHalfCircle (int radio, Paint paint) {Bitmap bitmap = Bitmap. 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), 360,180, true, paint); bitmap = Bitmap. createBitmap (bitmap, 0, radio, radio * 2, radio); return bitmap ;} /*** cut the image into a circle based on its short side and draw a certain width of white side around it * @ 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), paint); paint. setXfermode (new porterduxfermode (PorterDuff. mode. SRC_OVER); canvas. drawBitmap (roundBitmap, ringWidth, ringWidth, paint); return _ bitmap ;} /*** crop the image into 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 = 0xff0000242; 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 porterduduxfermode (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 (); paint. setAntiAlias (true); paint. setStyle (Paint. style. FILL); return paint ;}}