android 验证码的实现
突然发现自己开通博客已经两周年了。时间真是把杀猪刀,感觉时间过得太快了~
自己断断续续地也写了一些博客,但是总感觉到自己写博客只是停留在表面上,缺乏深度。 在android开发这条路上,自己还只是一个小菜鸟,需要做的事情还有很多,一定要继续加油努力~
好了,言归正传,说一下我们经常使用的android APP在登陆时的需要验证码,下面我们说一下验证码的实现过程。
package com.jinfenglee.vertifycode; import java.util.Random; import android.graphics.Bitmap; import android.graphics.Canvas; import android.graphics.Color; import android.graphics.Paint; import android.graphics.Bitmap.Config; public class VertifyCode { private static VertifyCode bmpCode; private String code; private int paddingLeft, paddingTop; private Random random = new Random(); public static VertifyCode getInstance() { if (bmpCode == null) bmpCode = new VertifyCode(); return bmpCode; } /** * 生成验证码图片 * @return */ public Bitmap createBitmap() { paddingLeft = 0; Bitmap bitmap = Bitmap.createBitmap(Constants.DEFAULT_WIDTH, Constants.DEFAULT_HEIGHT, Config.ARGB_8888); Canvas c = new Canvas(bitmap); code = createCode(); c.drawColor(Color.WHITE); Paint paint = new Paint(); paint.setTextSize(Constants.DEFAULT_FONT_SIZE); for (int i = 0; i < code.length(); i++) { randomTextStyle(paint); randomPadding(); c.drawText(code.charAt(i) + "", paddingLeft, paddingTop, paint); } for (int i = 0; i < Constants.DEFAULT_LINE_NUMBER; i++) { drawLine(c, paint); } c.save(Canvas.ALL_SAVE_FLAG);// 保存 c.restore(); return bitmap; } public String getCode() { return code; } /** * 生成 验证码 * * @return */ private String createCode() { StringBuilder buffer = new StringBuilder(); for (int i = 0; i < Constants.DEFAULT_CODE_LENGTH; i++) { buffer.append(Constants.CHARS[random.nextInt(Constants.CHARS.length)]); } return buffer.toString(); } /** * 在画布上用画笔画线 * @param canvas * @param paint */ private void drawLine(Canvas canvas, Paint paint) { int color = randomColor(); int startX = random.nextInt(Constants.DEFAULT_WIDTH); int startY = random.nextInt(Constants.DEFAULT_HEIGHT); int stopX = random.nextInt(Constants.DEFAULT_WIDTH); int stopY = random.nextInt(Constants.DEFAULT_HEIGHT); paint.setStrokeWidth(1); paint.setColor(color); canvas.drawLine(startX, startY, stopX, stopY, paint); } /** * 随机生成颜色值 * @return */ private int randomColor() { return randomColor(1); } private int randomColor(int rate) { int red = random.nextInt(256) / rate; int green = random.nextInt(256) / rate; int blue = random.nextInt(256) / rate; return Color.rgb(red, green, blue); } /** * 验证码字体样式 * @param paint */ private void randomTextStyle(Paint paint) { int color = randomColor(); paint.setColor(color); paint.setFakeBoldText(random.nextBoolean()); } private void randomPadding() { paddingLeft += Constants.BASE_PADDING_LEFT + random.nextInt(Constants.RANGE_PADDING_LEFT); paddingTop = Constants.BASE_PADDING_TOP + random.nextInt(Constants.RANGE_PADDING_TOP); } }
MainActivity:
public class MainActivity extends Activity { private ImageView ivVertifyCode; private Button btnSwitch; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); ivVertifyCode = (ImageView) findViewById(R.id.iv_vertify_code); btnSwitch = (Button) findViewById(R.id.btn_switch_another); ivVertifyCode.setImageBitmap(VertifyCode.getInstance().createBitmap()); btnSwitch.setOnClickListener(new OnClickListener() { @Override public void onClick(View v) { ivVertifyCode.setImageBitmap(VertifyCode.getInstance().createBitmap()); Toast.makeText(getApplicationContext(), VertifyCode.getInstance().getCode(), Toast.LENGTH_LONG).show(); } }); } }
posted on 2015-04-10 23:58 jinfenglee 阅读(525) 评论(0) 编辑 收藏 举报