首页 > 代码库 > Android中特殊图形的生成例子
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.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 { /** * 画出一个这样一个图形:中间带标题文字的半圆,半圆可以设置阴影和顶部的矩形投影 * * @param text * 标题文字 (此处设置为两个字的居中效果) * @param radio * 半圆的半径 * @param shadowWidth * 阴影的宽度 * @param isWithRectf * 是否有矩形投影 * @param rectfColor * 矩形投影的颜色 * @param paint * 画笔对象 * @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); // 画一个带圆心的扇形,此处为半圆 canvas.drawArc(new RectF(0, 0, radio * 2, radio * 2), 360, 180, true, paint); // 是否需要在半圆顶部画出一个矩形投影 if (isWithRectf) { paint = reset(paint); paint.setColor(rectfColor); Bitmap bmp = drawRectf(radio * 2, radio/8, paint); paint = reset(paint); paint.setXfermode(new PorterDuffXfermode(PorterDuff.Mode.SRC_ATOP));//去掉矩形投影和半圆不相交的部分 canvas.drawBitmap(bmp, 0, radio, paint); }else { LinearGradient lg=new LinearGradient(0,0,0,radio/8,0x44000000,0x00000000,Shader.TileMode.MIRROR); paint = reset(paint); paint.setShader(lg); Bitmap bmp = drawRectf(radio * 2, radio/8, paint); paint = reset(paint); paint.setXfermode(new PorterDuffXfermode(PorterDuff.Mode.SRC_ATOP));//去掉矩形投影和半圆不相交的部分 canvas.drawBitmap(bmp, 0, radio, paint); } //在半圆上面画上文字 paint = reset(paint); paint.setColor(Color.WHITE); paint.setTextSize(radio * 5 / 12); paint.setTextAlign(Align.CENTER); paint.setXfermode(new PorterDuffXfermode(PorterDuff.Mode.SRC_OVER)); canvas.drawText(text, radio, radio * 5 / 3, paint); bitmap = Bitmap.createBitmap(bitmap, 0, radio, radio * 2, radio + shadowWidth); return bitmap; } /** * 根据宽度画出一条指定三角形数量的锯齿 * * @param screenWidth * 宽度 * @param triangleHeight * 锯齿的高度 * @param triangleNum * 三角形的数量 * @param isDown * 三角形顶点的朝向,true表示向下,false表示向上 * @param paint * 画笔对象 * @return 返回bitmap对象 */ 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; } /** * 画出一条带有指定半圆数量的花边 * @param width 花边的长度 * @param num 半圆的数量 * @param paint 画笔对象 * @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; } /** * 画一个矩形 * @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; } /** * 画出一个半圆 * * @param radio * 半径 * @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; } /** * 根据图片的短边裁剪成圆形并在周围画上一定宽度的白边 * @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); paint.setXfermode(new PorterDuffXfermode(PorterDuff.Mode.SRC_OVER)); canvas.drawBitmap(roundBitmap, ringWidth, ringWidth, paint); return _bitmap; } /** * 将图像裁剪成圆形 * * @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 = http://www.mamicode.com/new Rect((int) left, (int) top, (int) right,>Android中特殊图形的生成例子
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。