国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 系統(tǒng) > Android > 正文

Android繪制驗證碼的實例代碼

2019-10-22 18:33:01
字體:
供稿:網(wǎng)友

在前面仿華為加載動畫、仿網(wǎng)易音樂聽歌識曲-麥克風動畫中,我們通過繪圖的基礎知識完成了簡單的繪制。在本例中,我們將繪制常見的驗證碼。

一、效果圖

android,繪制,驗證碼

二、知識點與思路分析

通過上面的效果圖觀察,我們可以看到里面有繪制的隨機線條,隨機繪制的驗證碼。

繪制線條,直線或曲線

繪制文本,生成的驗證碼文本的繪制

繪制圓點。

三、代碼編寫

/** * 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);    /**構建驗證碼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)負角度*/      offsetDegree = random.nextInt(2) == 1 ? offsetDegree : -offsetDegree;      canvas.save();      //設置旋轉(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);      //給畫筆設置隨機顏色      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,繪制,驗證碼

以上所述是小編給大家介紹的Android繪制驗證碼的實例代碼,希望對大家有所幫助,如果大家有任何疑問請給我留言,小編會及時回復大家的。在此也非常感謝大家對VEVB武林網(wǎng)網(wǎng)站的支持!


注:相關教程知識閱讀請移步到Android開發(fā)頻道。
發(fā)表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發(fā)表
主站蜘蛛池模板: 饶河县| 辽源市| 二连浩特市| 铁力市| 当阳市| 汕头市| 竹山县| 宾川县| 琼海市| 台前县| 翼城县| 监利县| 淮安市| 曲周县| 深泽县| 青岛市| 库伦旗| 华坪县| 石屏县| 南阳市| 深水埗区| 赤水市| 资兴市| 积石山| 民和| 怀集县| 福安市| 合山市| 神池县| 台州市| 通山县| 赫章县| 贡山| 孟村| 渭源县| 凤庆县| 东台市| 阿瓦提县| 北海市| 安平县| 青川县|