Android绘制验证码的实例代码
程序员文章站
2023-08-25 12:38:32
在前面仿华为加载动画、仿网易音乐听歌识曲-麦克风动画中,我们通过绘图的基础知识完成了简单的绘制。在本例中,我们将绘制常见的验证码。
一、效果图
二、知识点与思路分析...
在前面仿华为加载动画、仿网易音乐听歌识曲-麦克风动画中,我们通过绘图的基础知识完成了简单的绘制。在本例中,我们将绘制常见的验证码。
一、效果图
二、知识点与思路分析
通过上面的效果图观察,我们可以看到里面有绘制的随机线条,随机绘制的验证码。
绘制线条,直线或曲线
绘制文本,生成的验证码文本的绘制
绘制圆点。
三、代码编写
/** * created by iflytek_dsw on 2017/7/3. */ public class identifycodeutil { private static final int code_number = 4; private static final int line_number = 5; private static final int point_number = 10; private stringbuffer stringbuffer = null; private random random = new random(); //随机数数组 private static final char[] chars = { '2', '3', '4', '5', '6', '7', '8', '9', 'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'j', 'k', 'm', 'n', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z', 'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z' }; private static identifycodeutil instance; public static identifycodeutil getinstance(){ if(instance == null){ instance = new identifycodeutil(); } return instance; } public bitmap createbitmapcode(int width, int height){ bitmap bitmap = bitmap.createbitmap(width, height, bitmap.config.argb_8888); canvas canvas = new canvas(bitmap); canvas.drawcolor(color.ltgray); drawcodetext(canvas, width, height); drawlines(canvas, width, height); drawpoint(canvas, width, height); return bitmap; } /** * 返回验证码 * @return 验证码生成的字符串 */ public string getidentifycode(){ if(stringbuffer == null){ return ""; } return stringbuffer.tostring(); } /** * 生成验证码 * @return */ private string buildidentifycode(){ stringbuffer stringbuffer = new stringbuffer(); for(int i=0; i < code_number;i++){ stringbuffer.append(chars[random.nextint(chars.length)]); } log.d("code",stringbuffer.tostring()); return stringbuffer.tostring(); } /** * 绘制文本 * @param canvas 画布 * @param width 宽度 * @param height 高度 */ private void drawcodetext(canvas canvas,int width, int height){ paint paint = new paint(); paint.settextsize(50); /**构建验证码code*/ string text = buildidentifycode(); float textlength = paint.measuretext(text); int startmaxlength = (int) ((width - textlength) / 2); /**随机计算验证码绘制每次开头的位置*/ int startposition = random.nextint(startmaxlength); //绘制文字 for(int index = 0; index < text.length(); index++){ /**生成旋转的角度*/ int offsetdegree = random.nextint(15); /**这里只会产生0和1,如果是1那么正旋转正角度,否则旋转负角度*/ offsetdegree = random.nextint(2) == 1 ? offsetdegree : -offsetdegree; canvas.save(); //设置旋转 canvas.rotate(offsetdegree, width / 2, height / 2); /**生成随机的颜色*/ paint.setargb(255, random.nextint(200) + 20, random.nextint(200) + 20, random.nextint(200) + 20); char tempchar = text.charat(index); //给画笔设置随机颜色 canvas.drawtext(string.valueof(tempchar), startposition +index * textlength / text.length() +15, height * 3 / 5f,paint); canvas.restore(); } } /** * 生成干扰线 * @param canvas * @param width * @param height */ private void drawlines(canvas canvas,int width, int height){ paint paint = new paint(); paint.setstrokewidth(3); for(int i = 0;i < line_number;i++){ paint.setargb(255, random.nextint(200) + 30, random.nextint(200) + 30, random.nextint(200) + 30); int startx = random.nextint(width); int starty = random.nextint(height); int endx = random.nextint(width); int endy = random.nextint(height); canvas.drawline(startx, starty, endx, endy, paint); } } /** * 生成干扰点 */ private void drawpoint(canvas canvas, int width, int height) { paint paint = new paint(); paint.setstrokewidth(3); paint.setcolor(color.gray); for(int i=0; i< point_number; i++){ pointf pointf = new pointf(random.nextint(width) + 10, random.nextint(height) + 10); canvas.drawpoint(pointf.x, pointf.y, paint); } } }
以上所述是小编给大家介绍的android绘制验证码的实例代码,希望对大家有所帮助