本来是在写着自定义view,写着写着又向其中添加了点击刷新内容,所以就觉得这很像是一个数字验证码的功能。点击可以随机变换view上的数字,其中,我又增加了干扰线,与干扰点。
效果如下:
流程如下:
一,在资源目录values下新建attrs.xml文档
<!--随机4位验证码-->
<attr name="txtContent" format="string" />
<attr name="txtColor" format="color" />
<attr name="txtBackgroundColor" format="color" />
<attr name="txtSize" format="dimension" />
<declare-styleable name="txtView">
<attr name="txtContent" />
<attr name="txtColor" />
<attr name="txtBackgroundColor" />
<attr name="txtSize" />
</declare-styleable>
二,接下来就是在view的构造方法中获得我们自定义的属性,重写onDraw就ok了,在这例子,涉及到view在布局文件中设置WRAP_CONTENT或 MATCH_PARENT,系统帮我们测量的结果就是MATCH_PARENT的长度。所以,当设置了WRAP_CONTENT时,我们需要自己进行测量,即重写onMesure方法”。
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();
}
});
}
@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);
}
}
至此,我们的一个获取4位随机验证就完成了。如果想要获取验证码的可以,通过getCode() 这个方法获取得到。