package com.ten.txtView;
import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.PointF;
import android.graphics.Rect;
import android.support.annotation.Nullable;
import android.util.AttributeSet;
import android.util.TypedValue;
import android.view.View;
import java.util.HashSet;
import java.util.Random;
import java.util.Set;
/**
* Created by Ten on 2017/5/23 0023.
*/
public class TxtView extends View {
private String mTxtText;
private int mTxtColor;
private int mTxtSize;
private int mTxtBackgroundColor;
private Random random = new Random();
private Rect mBound;
private Paint mPaint;
public TxtView(Context context) {
this(context, null);
}
public TxtView(Context context, @Nullable AttributeSet attrs) {
this(context, attrs, 0);
}
// 获取自定义样式的属性
public TxtView(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
super(context, attrs, defStyleAttr);
TypedArray array = context.getTheme().obtainStyledAttributes(attrs,
R.styleable.txtView, defStyleAttr, 0);
int num = array.length();
for (int i = 0; i < num; i++) {
int attr = array.getIndex(i);
switch (attr) {
case R.styleable.txtView_txtContent:
mTxtText = array.getString(attr);
break;
case R.styleable.txtView_txtColor:
mTxtColor = array.getColor(attr, Color.BLACK);
break;
case R.styleable.txtView_txtBackgroundColor:
mTxtBackgroundColor = array.getColor(attr, Color.YELLOW);
break;
case R.styleable.txtView_txtSize:
mTxtSize = array.getDimensionPixelSize(attr, (int) TypedValue.applyDimension(
TypedValue.COMPLEX_UNIT_SP, 16, getResources().getDisplayMetrics()
));
break;
}
}
array.recycle();
mPaint = new Paint();
mPaint.setTextSize(mTxtSize);
mBound = new Rect();
mPaint.getTextBounds(mTxtText, 0, mTxtText.length(), mBound);
this.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
mTxtText = randomText();
postInvalidate();
}
});
}
private String randomText() {
Random random = new Random();
Set<Integer> set = new HashSet<>();
while (set.size() < 4) {
int randomInt = random.nextInt(10);
set.add(randomInt);
}
StringBuffer stringBuffer = new StringBuffer();
for (Integer i : set) {
stringBuffer.append("" + i);
}
return stringBuffer.toString();
}
@Override
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
int widthMode = MeasureSpec.getMode(widthMeasureSpec);
int widthSize = MeasureSpec.getSize(widthMeasureSpec);
int heightMode = MeasureSpec.getMode(heightMeasureSpec);
int heightSize = MeasureSpec.getSize(heightMeasureSpec);
int width, height;
if (widthMode == MeasureSpec.EXACTLY) {
width = widthSize;
} else {
mPaint.setTextSize(mTxtSize);
mPaint.getTextBounds(mTxtText, 0, mTxtText.length(), mBound);
float textWidth = mBound.width();
int desired = (int) (getPaddingLeft() + textWidth + getPaddingRight());
width = desired;
}
if (heightMode == MeasureSpec.EXACTLY) {
height = heightSize;
} else {
mPaint.setTextSize(mTxtSize);
mPaint.getTextBounds(mTxtText, 0, mTxtText.length(), mBound);
float textHeight = mBound.height();
int desired = (int) (getPaddingTop() + textHeight + getPaddingBottom());
height = desired;
}
setMeasuredDimension(width, height);
}
@Override
protected void onDraw(Canvas canvas) {
mPaint.setColor(mTxtBackgroundColor);
canvas.drawRect(0, 0, getMeasuredWidth(), getMeasuredHeight(), mPaint);
mPaint.setAntiAlias(true);
mPaint.setTextSize(mTxtSize);
randomTextStyle(mPaint);
canvas.drawText(mTxtText, getWidth() / 2 - mBound.width() / 2, getHeight() / 2 + mBound.height() / 2,
mPaint);
for (int i = 0; i<4;i++){
drawLine(canvas,mPaint);
}
mPaint.setColor(randomColor());
for (int i = 0; i < 50; i++) {
drawPoint(canvas, mPaint);
}
}
public String getCode(){
return mTxtText;
}
// 画干扰点
private void drawPoint(Canvas canvas, Paint paint) {
PointF pointF = new PointF(random.nextInt(canvas.getWidth()), random.nextInt(canvas.getHeight()));
canvas.drawPoint(pointF.x, pointF.y, paint);
}
//画干扰线
private void drawLine(Canvas canvas, Paint paint) {
int color = randomColor();
int startX = random.nextInt(canvas.getWidth());
int startY = random.nextInt(canvas.getHeight());
int stopX = random.nextInt(canvas.getWidth());
int stopY = random.nextInt(canvas.getHeight());
paint.setStrokeWidth(1);
paint.setColor(color);
canvas.drawLine(startX, startY, stopX, stopY, paint);
}
//生成随机颜色
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);
}
//随机生成文字样式,颜色,粗细,倾斜度
private void randomTextStyle(Paint paint) {
int color = randomColor();
paint.setColor(color);
}
}
android 数字验证码
需积分: 13 201 浏览量
2017-05-23
18:20:24
上传
评论
收藏 3KB ZIP 举报
Ten_it
- 粉丝: 2
- 资源: 11