android+canvas+图片,android 开发 View _12_ 用Canvas 绘制一张图片(博客中演示用Canvas画验证码图片)...
packagenet.yt.yuncare.widgets;importandroid.graphics.Bitmap;importandroid.graphics.Canvas;importandroid.graphics.Color;importandroid.graphics.Paint;import java.lang.*;importjava.util.Random;/*** Created by lenovo on 2018/5/14.*/
public classCode {//隨機數(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 staticCode bmpCode;public static Code getInstance() { //單例模式
if(bmpCode == null)
bmpCode= newCode();returnbmpCode;
}//default settings//驗證碼默認(rèn)隨機數(shù)的個數(shù)
private static final int DEFAULT_CODE_LENGTH = 4;//默認(rèn)字體大小
private static final int DEFAULT_FONT_SIZE = 35;//默認(rèn)線條的條數(shù)
private static final int DEFAULT_LINE_NUMBER = 2;//padding值
private static final int BASE_PADDING_LEFT = 10, RANGE_PADDING_LEFT = 15, BASE_PADDING_TOP = 15, RANGE_PADDING_TOP = 20;//驗證碼的默認(rèn)寬高
private static final int DEFAULT_WIDTH = 100, DEFAULT_HEIGHT = 40;//settings decided by the layout xml//canvas width and height
private int width = DEFAULT_WIDTH, height =DEFAULT_HEIGHT;//random word space and pading_top
private int base_padding_left = BASE_PADDING_LEFT, range_padding_left =RANGE_PADDING_LEFT,
base_padding_top= BASE_PADDING_TOP, range_padding_top =RANGE_PADDING_TOP;//number of chars, lines; font size
private int codeLength = DEFAULT_CODE_LENGTH, line_number = DEFAULT_LINE_NUMBER, font_size =DEFAULT_FONT_SIZE;//variables
privateString code;private intpadding_left, padding_top;private Random random = newRandom();//生成驗證碼圖片 在外部Image調(diào)用,用setImageBitmap添加圖片到ImageView控件中
publicBitmap createBitmap() {
padding_left= 0;//重點:創(chuàng)建Bt圖片
Bitmap bp = Bitmap.createBitmap(width, height, Bitmap.Config.ARGB_8888); // Canvas c = new Canvas(bp);//創(chuàng)建畫板導(dǎo)入bt圖片
code=createCode();
c.drawColor(Color.WHITE);//添加畫板背景色為白色
Paint paint = new Paint();//創(chuàng)建畫筆
paint.setAntiAlias(true);
paint.setTextSize(font_size);//設(shè)置畫筆大小//畫驗證碼
for (int i = 0; i < code.length(); i++) {
randomTextStyle(paint);//畫筆傳入生成隨機樣式
randomPadding();
c.drawText(code.charAt(i)+ "", padding_left, padding_top, paint);
}//畫線條
for (int i = 0; i < line_number; i++) {
drawLine(c, paint);
}
c.save( Canvas.ALL_SAVE_FLAG );//保存
c.restore();// returnbp;
}publicString getCode() {returncode;
}//生成驗證碼
privateString createCode() {
StringBuilder buffer= newStringBuilder();for (int i = 0; i < codeLength; i++) {
buffer.append(CHARS[random.nextInt(CHARS.length)]);
}returnbuffer.toString();
}//畫干擾線
private voiddrawLine(Canvas canvas, Paint paint) {int color =randomColor();int startX =random.nextInt(width);int startY =random.nextInt(height);int stopX =random.nextInt(width);int stopY =random.nextInt(height);
paint.setStrokeWidth(1);
paint.setColor(color);
canvas.drawLine(startX, startY, stopX, stopY, paint);
}//生成隨機顏色
private intrandomColor() {return randomColor(1);
}private int randomColor(intrate) {int red = random.nextInt(256) /rate;int green = random.nextInt(256) /rate;int blue = random.nextInt(256) /rate;returnColor.rgb(red, green, blue);
}//隨機生成文字樣式,顏色,粗細(xì),傾斜度
private voidrandomTextStyle(Paint paint) {int color =randomColor();
paint.setColor(color);
paint.setFakeBoldText(random.nextBoolean());//true為粗體,false為非粗體//float skewX = random.nextInt(11) / 10;//skewX = random.nextBoolean() ? skewX : -skewX;//paint.setTextSkewX(skewX);//float類型參數(shù),負(fù)數(shù)表示右斜,整數(shù)左斜//paint.setUnderlineText(true);//true為下劃線,false為非下劃線//paint.setStrikeThruText(true);//true為刪除線,false為非刪除線
}//隨機生成padding值
private voidrandomPadding() {
padding_left+= base_padding_left +random.nextInt(range_padding_left);
padding_top= base_padding_top +random.nextInt(range_padding_top);
}
}
總結(jié)
以上是生活随笔為你收集整理的android+canvas+图片,android 开发 View _12_ 用Canvas 绘制一张图片(博客中演示用Canvas画验证码图片)...的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: linux下crontab allow,
- 下一篇: html5支付宝主页面代码,JavaSc