在前面仿華為加載動畫、仿網(wǎng)易音樂聽歌識曲-麥克風(fēng)動畫中,我們通過繪圖的基礎(chǔ)知識完成了簡單的繪制。在本例中,我們將繪制常見的驗證碼。
成都創(chuàng)新互聯(lián)公司從2013年開始,是專業(yè)互聯(lián)網(wǎng)技術(shù)服務(wù)公司,擁有項目做網(wǎng)站、網(wǎng)站設(shè)計網(wǎng)站策劃,項目實施與項目整合能力。我們以讓每一個夢想脫穎而出為使命,1280元西華做網(wǎng)站,已為上家服務(wù),為西華各地企業(yè)和個人服務(wù),聯(lián)系電話:18982081108
一、效果圖
二、知識點與思路分析
通過上面的效果圖觀察,我們可以看到里面有繪制的隨機線條,隨機繪制的驗證碼。
繪制線條,直線或曲線
繪制文本,生成的驗證碼文本的繪制
繪制圓點。
三、代碼編寫
/** * 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(); //隨機數(shù)數(shù)組 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); /**構(gòu)建驗證碼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++){ /**生成旋轉(zhuǎn)的角度*/ int offsetDegree = random.nextInt(15); /**這里只會產(chǎn)生0和1,如果是1那么正旋轉(zhuǎn)正角度,否則旋轉(zhuǎn)負(fù)角度*/ offsetDegree = random.nextInt(2) == 1 ? offsetDegree : -offsetDegree; canvas.save(); //設(shè)置旋轉(zhuǎn) 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); //給畫筆設(shè)置隨機顏色 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繪制驗證碼的實例代碼,希望對大家有所幫助,如果大家有任何疑問請給我留言,小編會及時回復(fù)大家的。在此也非常感謝大家對創(chuàng)新互聯(lián)網(wǎng)站的支持!