android 自定义View生成验证码
来源:互联网 发布:土豆for mac下载 官方 编辑:程序博客网 时间:2024/06/06 07:33
//自定义view 生成验证码
package com.yh.indentfyingcode.views;
import java.util.Random;
import com.example.indentfyingcode.R;
import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Rect;
import android.util.AttributeSet;
import android.util.Log;
import android.util.TypedValue;
import android.view.View;
/**自定义view 生成验证码
* @author yh
* 2016.2.24
* */
public class IndentfyingCode extends View {
private static final char[] CHARS = { '0', '1', '2', '3', '4', '5', '6',
'7', '8', '9', 'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j',
'k', 'l', 'm', 'n', 'o', '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', 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W',
'X', 'Y', 'Z' };
/**验证码*/
private String text;
private int textColor;
private int textSize;
private Rect rect;
private Paint mPaint;
private Random random;
private int textCodeWid;
private int textCodeHei;
private Context context;
/**默认噪线条数*/
private int lines=3;
/**验证码长度*/
private int textLength=5;
public IndentfyingCode(Context context) {
this(context, null);
// TODO Auto-generated constructor stub
this.context=context;
}
public IndentfyingCode(Context context, AttributeSet attrs) {
this(context, attrs, 0);
// TODO Auto-generated constructor stub
this.context=context;
}
public IndentfyingCode(Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
// TODO Auto-generated constructor stub
this.context=context;
random = new Random();
TypedArray a = context.getTheme().obtainStyledAttributes(attrs,
R.styleable.IndentfyingCode, defStyle, 0);
for (int i = 0; i < a.getIndexCount(); i++) {
switch (a.getIndex(i)) {
case R.styleable.IndentfyingCode_titleText:
// text = a.getString(a.getIndex(i));
text = text(textLength);
break;
case R.styleable.IndentfyingCode_titleTextColor:
textColor = a.getColor(a.getIndex(i), Color.BLACK);
break;
case R.styleable.IndentfyingCode_titleTextSize:
textSize = a.getDimensionPixelSize(a.getIndex(i),
(int) TypedValue.applyDimension(
TypedValue.COMPLEX_UNIT_SP, 14, getResources()
.getDisplayMetrics()));
break;
}
}
a.recycle();
mPaint = new Paint();
mPaint.setTextSize(textSize);
mPaint.setColor(textColor);
rect = new Rect();
mPaint.getTextBounds(text, 0, text.length(), rect);
textCodeWid=rect.width();
textCodeHei=rect.height();
this.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
text = text(textLength);
postInvalidate();
}
});
}
private String text(int n) {
StringBuffer sb = new StringBuffer();
for (int i = 0; i < n; i++) {
sb.append(CHARS[random.nextInt(CHARS.length)]);
}
String s = sb.toString();
sb = null;
return s;
}
@Override
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
// TODO Auto-generated method stub
super.onMeasure(widthMeasureSpec, heightMeasureSpec);
;
setMeasuredDimension(
setsize(widthMeasureSpec, rect.width(), getPaddingLeft(),
getPaddingRight()),
setsize(heightMeasureSpec, rect.height(), getPaddingTop(),
getPaddingBottom()));
}
private int setsize(int widthMeasureSpec, int rectsize, int pading1,
int pading2) {
int measureMode = MeasureSpec.getMode(widthMeasureSpec);
int measureSize = MeasureSpec.getSize(widthMeasureSpec);
switch (measureMode) {
case MeasureSpec.EXACTLY:
break;
case MeasureSpec.AT_MOST:
measureSize = Math.min(measureSize, rectsize);
break;
}
return measureSize + pading1 + pading2;
}
@Override
protected void onDraw(Canvas canvas) {
// TODO Auto-generated method stub
super.onDraw(canvas);
mPaint.setColor(Color.WHITE);
canvas.drawRect(0, 0, getMeasuredWidth(), getMeasuredHeight(), mPaint);
mPaint.setColor(textColor);
for (int i = 0; i < text.length(); i++) {
String s=text.substring(i, i+1);
int textWid=px2dip( context, textSize);
mPaint.setColor(Color.rgb(random.nextInt(256), random.nextInt(256), random.nextInt(256)));
canvas.drawText(s,getWidth() / 2 - textCodeWid / 2+i*textWid, getHeight()
/ 2 + textCodeHei/ 2, mPaint);
}
for (int i = 0; i <lines; i++) {
mPaint.setColor(Color.rgb(random.nextInt(256), random.nextInt(256), random.nextInt(256)));
int x1=random.nextInt(getWidth());
int x2=random.nextInt(getWidth());
int y1=random.nextInt(getHeight());
int y2=random.nextInt(getHeight());
canvas.drawLine(x1, x2, y1, y2, mPaint);
}
}
/** 像素转dip */
public static int px2dip(Context context, float pxValue) {
final float scale = context.getResources().getDisplayMetrics().density;
return (int) (pxValue / scale + 0.5f) ;
}
}
//自定义属性
<?xml version="1.0" encoding="utf-8"?>
<resources>
<!-- string,color,demension,integer,enum,reference,float,boolean,fraction,flag -->
<attr name="titleText" format="string" />
<attr name="titleTextColor" format="color" />
<attr name="titleTextSize" format="dimension" />
<declare-styleable name="IndentfyingCode">
<attr name="titleText" />
<attr name="titleTextColor" />
<attr name="titleTextSize" />
</declare-styleable>
</resources>
// 在activity中测试
package com.yh.indentfyingcode;
import com.example.indentfyingcode.R;
import android.app.Activity;
import android.os.Bundle;
/**
* @author yh
* 2016.2.24
* */
public class IndentfyingCodeActivity extends Activity{
@Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.indentfyingcodelayout);
}
}
//布局文件
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:y="http://schemas.android.com/apk/res/com.example.indentfyingcode"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#383838"
android:orientation="vertical" >
<View android:layout_width="wrap_content"
android:layout_height="0dp"
android:layout_weight="1"/>
<com.yh.indentfyingcode.views.IndentfyingCode
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:padding="10dp"
y:titleText="3546d"
y:titleTextColor="#FF0000"
y:titleTextSize="30sp" />
<View android:layout_width="wrap_content"
android:layout_height="0dp"
android:layout_weight="1"/>
</LinearLayout>
- android 自定义View生成验证码
- Android:自定义View(随机生成验证码)
- Android 自定义View之随机生成图片验证码
- Android 自定义View之随机生成图片验证码
- Android 自定义View (验证码)
- android 自定义控件以及自定义view学习(随机验证码生成)
- android 自定义控件以及自定义view学习(随机验证码生成)
- Android自定义View--验证码控件
- Android自定义View(一)(验证码)
- Android自定义View实现验证码
- android自定义view之刷新验证码
- android自定义验证码倒计时View
- Android自定义View实现随机验证码
- Android发送验证码倒计时自定义View
- Android 自定义view - 随机验证码
- android自定义View ---- RandomCodeView验证码
- android自定义View基础系列一(点击随机生成验证码效果)
- android自定义View---生成虚线的View
- CentOS升级Python3.5并安装lxml等模块
- ProcessBook 调用webservice 异常处理
- UGUI UI框架实现遇到的问题
- 概率语言模型 Probabilistic Language Modeling (二) --- 模型估计算法介绍
- Qualcomm官方QDN1月EDM:属于你的Dragon Board 410c
- android 自定义View生成验证码
- 数据结构之队列(六)
- Leetcode ☞ 292. Nim Game
- 用二级指针操作数组的方法
- python 五种异常处理机制
- smarty项目传到服务器,需要注意templates_c
- Android 语音控制 第三方框架 讯飞-灵犀语音的
- Linux之module_param()函数学习
- 计算机视觉领域的一些牛人博客,超有实力的研究机构等的网站链接