android怎么设置随机验证码

android怎么设置随机验证码,第1张

使用自定义的view可以使用,随机生成代码:
package comnobegutil;
import javautilRandom;
import androidgraphicsBitmap;
import androidgraphicsCanvas;
import androidgraphicsColor;
import androidgraphicsPaint;
import androidgraphicsBitmapConfig;
public class Code {

private static final char[] CHARS = {
'2', '3', '4', '5', '6', '7', '8', '9',
'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'j', 'k', 'l', '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 Code bmpCode;

public static Code getInstance() {
if(bmpCode == null)
bmpCode = new Code();
return bmpCode;
}

//default settings
private static final int DEFAULT_CODE_LENGTH = 3;
private static final int DEFAULT_FONT_SIZE = 25;
private static final int DEFAULT_LINE_NUMBER = 2;
private static final int BASE_PADDING_LEFT = 5, RANGE_PADDING_LEFT = 15, BASE_PADDING_TOP = 15, RANGE_PADDING_TOP = 20;
private static final int DEFAULT_WIDTH = 60, 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
private String code;
private int padding_left, padding_top;
private Random random = new Random();
//验证码
public Bitmap createBitmap() {
padding_left = 0;

Bitmap bp = BitmapcreateBitmap(width, height, ConfigARGB_8888);
Canvas c = new Canvas(bp);
code = createCode();

cdrawColor(ColorWHITE);
Paint paint = new Paint();
paintsetTextSize(font_size);

for (int i = 0; i < codelength(); i++) {
randomTextStyle(paint);
randomPadding();
cdrawText(codecharAt(i) + "", padding_left, padding_top, paint);
}
for (int i = 0; i < line_number; i++) {
drawLine(c, paint);
}

csave( CanvasALL_SAVE_FLAG );//保存
crestore();//
return bp;
}

public String getCode() {
return code;
}

//验证码
private String createCode() {
StringBuilder buffer = new StringBuilder();
for (int i = 0; i < codeLength; i++) {
bufferappend(CHARS[randomnextInt(CHARSlength)]);
}
return buffertoString();
}

private void drawLine(Canvas canvas, Paint paint) {
int color = randomColor();
int startX = randomnextInt(width);
int startY = randomnextInt(height);
int stopX = randomnextInt(width);
int stopY = randomnextInt(height);
paintsetStrokeWidth(1);
paintsetColor(color);
canvasdrawLine(startX, startY, stopX, stopY, paint);
}

private int randomColor() {
return randomColor(1);
}
private int randomColor(int rate) {
int red = randomnextInt(256) / rate;
int green = randomnextInt(256) / rate;
int blue = randomnextInt(256) / rate;
return Colorrgb(red, green, blue);
}

private void randomTextStyle(Paint paint) {
int color = randomColor();
paintsetColor(color);
paintsetFakeBoldText(randomnextBoolean()); //true为粗体,false为非粗体
float skewX = randomnextInt(11) / 10;
skewX = randomnextBoolean() skewX : -skewX;
paintsetTextSkewX(skewX); //float类型参数,负数表示右斜,整数左斜
// paintsetUnderlineText(true); //true为下划线,false为非下划线
// paintsetStrikeThruText(true); //true为删除线,false为非删除线
}

private void randomPadding() {
padding_left += base_padding_left + randomnextInt(range_padding_left);
padding_top = base_padding_top + randomnextInt(range_padding_top);
}
}

自定义字体
android Typeface使用TTF字体文件设置字体
我们可以在程序中放入ttf字体文件,在程序中使用Typeface设置字体。
第一步,在assets目录下新建fonts目录,把ttf字体文件放到这。
第二步,程序中调用:
AssetManager mgr=getAssets();//得到AssetManager
Typeface tf=TypefacecreateFromAsset(mgr, "fonts/ttfttf");//根据路径得到Typeface
tvsetTypeface(tf);//设置字体
2在xml文件中使用android:textStyle=”bold” 可以将英文设置成粗体, 但是不能将中文设置成粗体,
将中文设置成粗体的方法是:
TextView tv = (TextView)findViewById(RidTextView01);
tvgetPaint()setFakeBoldText(true);//中文仿“粗体”--使用TextPaint的仿“粗体”设置setFakeBoldText为true。
>// 自定义字体
custom = new TextView(this);
//xxttf located at assets/fonts/
typeface = TypefacecreateFromAsset(getAssets(),"fonts/xxttf");
customsetTypeface(typeface);
自定义字体
1android Typeface使用TTF字体文件设置字体
我们可以在程序中放入ttf字体文件,在程序中使用Typeface设置字体。
第一步,在assets目录下新建fonts目录,把ttf字体文件放到这。
第二步,程序中调用:
AssetManager mgr=getAssets();//得到AssetManager
Typeface tf=TypefacecreateFromAsset(mgr, "fonts/ttfttf");//根据路径得到Typeface
tvsetTypeface(tf);//设置字体
2在xml文件中使用android:textStyle=”bold” 可以将英文设置成粗体, 但是不能将中文设置成粗体,
将中文设置成粗体的方法是:
TextView tv = (TextView)findViewById(RidTextView01);
tvgetPaint()setFakeBoldText(true);//中文仿“粗体”--使用TextPaint的仿“粗体”设置setFakeBoldText为true。

第二种通过java代码去设置:
①第一种构造方法
viewsetTypeface(typeface,typefaceStyle);
属性不明白看源码:
[java] view plain copy / Sets the typeface and style in which the text should be displayed, and turns on the fake bold and italic bits in the Paint if the Typeface that you provided does not have all the bits in the style that you specified @attr ref androidRstyleable#TextView_typeface @attr ref androidRstyleable#TextView_textStyle / public void setTypeface(Typeface tf, int style) { if (style > 0) { if (tf == null) { tf = TypefacedefaultFromStyle(style); } else { tf = Typefacecreate(tf, style); } setTypeface(tf); // now compute what (if any) algorithmic styling is needed int typefaceStyle = tf != null tfgetStyle() : 0; int need = style & ~typefaceStyle;

android设置字体
(1)针对正常字体
//普通正常字体
normal=newTextView(this);
//设置字体内容,请注意:目前Android主要针对拉丁语系可使用字型设定,中文暂不支持
normalsetText("Normal Font FYI");
//设置字体大小
normalsetTextSize(200f);
//设置字型为默认,正常字体
normalsetTypeface(TypefaceDEFAULT,TypefaceNORMAL);
//增加该字体并显示到布局linearLayout中
linearLayoutaddView(normal,newLinearLayoutLayoutParams(LayoutParamsWRAP_CONTENT,LayoutParamsWRAP_CONTENT));

(2)针对粗体字体
//粗体字体
bold=newTextView(this);
boldsetText("Bold Font FYI");
boldsetTextSize(200f);
//设置字体颜色为蓝色
boldsetTextColor(ColorBLUE);
//设置字型为默认粗体,粗体字体
boldsetTypeface(TypefaceDEFAULT_BOLD, TypefaceBOLD);
linearLayoutaddView(bold,newLinearLayoutLayoutParams(LayoutParamsWRAP_CONTENT,LayoutParamsWRAP_CONTENT));

(3)针对斜体字体
//斜体字体
italic=newTextView(this);
italicsetTextSize(20f);
italicsetText("Italic Font FYI");
//设置字体颜色为红色
italicsetTextColor(ColorRED);
//设置字型为等宽字型,斜体字体
italicsetTypeface(TypefaceMONOSPACE,TypefaceITALIC);
linearLayoutaddView(italic,newLinearLayoutLayoutParams(LayoutParamsWRAP_CONTENT,LayoutParamsWRAP_CONTENT));

(4)针对粗斜体字体
//粗斜体字体
italic_bold=newTextView(this);
italic_boldsetTextSize(20f);
italic_boldsetText("Italic & Bold Font FYI");
//设置字体颜色为**
italic_boldsetTextColor(ColorYELLOW);
//设置字型为等宽字型,斜体字体
italic_boldsetTypeface(TypefaceMONOSPACE,TypefaceBOLD_ITALIC);
linearLayoutaddView(italic_bold,newLinearLayoutLayoutParams(LayoutParamsWRAP_CONTENT,LayoutParamsWRAP_CONTENT));
(5)针对中文仿“粗体”
//针对Android字型的中文字体问题
chinese=newTextView(this);
chinesesetText("中文粗体显示效果");
//设置字体颜色
chinesesetTextColor(ColorMAGENTA);
chinesesetTextSize(200f);
chinesesetTypeface(TypefaceDEFAULT_BOLD, TypefaceBOLD);
//使用TextPaint的仿“粗体”设置setFakeBoldText为true。目前还无法支持仿“斜体”方法
tp=chinesegetPaint();
tpsetFakeBoldText(true);
linearLayoutaddView(chinese,newLinearLayoutLayoutParams(LayoutParamsWRAP_CONTENT,LayoutParamsWRAP_CONTENT));

(6)自定义创建字型
//自定义字体字型
custom=newTextView(this);
//字体MgOpenCosmeticaBoldttf放置于assets/font/路径下
typeface=TypefacecreateFromAsset(getAssets(),"font/MgOpenCosmeticaBoldttf");
customsetTypeface(typeface);
customsetText("Custom Font FYI");
customsetTextSize(200f);
//设置字体颜色
customsetTextColor(ColorCYAN);
linearLayoutaddView(custom,newLinearLayoutLayoutParams(LayoutParamsWRAP_CONTENT,LayoutParamsWRAP_CONTENT));


欢迎分享,转载请注明来源:内存溢出

原文地址: https://outofmemory.cn/yw/12967846.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2023-05-29
下一篇 2023-05-29

发表评论

登录后才能评论

评论列表(0条)

保存