public class RandomChars {
char[] chars;
public RandomChars() {
chars = new char[26];
for (int i = 0; i < 26; i++) {
chars[i] = (char) (i + 65);
}
}
public char[] get4Chars() {
char[] rlt = new char[4];
for (int i = 0; i < rlt.length; i++) {
int randomIndex = (int) (Math.random() * 26);
rlt[i] = chars[randomIndex];
}
return rlt;
}
}
float unitWidth = (float) getWidth() / (float) chars.length;
for (int i = 0; i < chars.length; i++) {
String str = chars[i] + "";
textPaint.getTextBounds(str, 0, str.length(), mRect);
resetColor();
int angel = (int) (Math.random()*(8-(-8)+1)+(-8));
canvas.rotate(angel);//旋转字母,随机角度
canvas.drawText(str, i * unitWidth + 5, getHeight() / 2 - mRect.centerY(), textPaint);
/**
* 很关键,旋转
*/
canvas.save();//保存状态
canvas.restore();//恢复
}
/**
* 重新设置随机颜色
*/
private void resetColor() {
int r = (int) (Math.random() * 230 - 30);
int g = (int) (Math.random() * 230 - 30);
int b = (int) (Math.random() * 230 - 30);
textPaint.setColor(Color.rgb(r, g, b));
}
submit.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
String inputStr = input.getText().toString();
inputStr = inputStr.toUpperCase();
str = str.toUpperCase();
if (str.equals(inputStr)) {
Toast.makeText(MainActivity.this, "输入正确", Toast.LENGTH_SHORT).show();
}else{
Toast.makeText(MainActivity.this, "验证码输入错误", Toast.LENGTH_SHORT).show();
char[] getchar = randomChars.get4Chars();
str = new String(getchar);
codeView.setChars(getchar);
}
}
});
机械节能产品生产企业官网模板...
大气智能家居家具装修装饰类企业通用网站模板...
礼品公司网站模板
宽屏简约大气婚纱摄影影楼模板...
蓝白WAP手机综合医院类整站源码(独立后台)...苏ICP备2024110244号-2 苏公网安备32050702011978号 增值电信业务经营许可证编号:苏B2-20251499 | Copyright 2018 - 2025 源码网商城 (www.ymwmall.com) 版权所有