大佬教程收集整理的这篇文章主要介绍了Android自定义控件深入学习 Android生成随机验证码,大佬教程大佬觉得挺不错的,现在分享给大家,也给大家做个参考。
在上一篇的文章中介绍了自定义控件的属性,详情见《详解Android自定义控件属性TypedArray以及attrs》。那么在这基础上实现随机验证码生成,里面的代码是自定义控件以及涉及到自定义view绘画。
1、先看实现的效果图
看到这个效果图是不是感觉还可以。那么就看看源码吧。
2、attr文件
<?xml version="1.0" encoding="utf-8"?> <resources> <attr name="titleText" format="String" /> <attr name="titleTextColor" format="color" /> <attr name="titleTextSize" format="dimension" /> <declare-styleable name="AuthCodeView"> <attr name="titleText" /> <attr name="titleTextColor" /> <attr name="titleTextSize" /> </declare-styleable> </resources>
3、布局layout
<LinearLayout xmlns:android="http://scheR_690_11845@as.android.com/apk/res/android" xmlns:tools="http://scheR_690_11845@as.android.com/tools" xmlns:authcodeview="http://scheR_690_11845@as.android.com/apk/res/com.example.authcodeview" android:id="@+id/LinearLayout1" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical" > <LinearLayout android:layout_width="match_parent" android:layout_height="wrap_content" > <com.example.authcodeview.view.AuthCodeView android:id="@+id/AuthCodeView" android:layout_width="wrap_content" android:layout_height="wrap_content" android:padding="10dp" authcodeview@R_205_6964@Text="3712" authcodeview@R_205_6964@TextColor="#00ffff" authcodeview@R_205_6964@TextSize="40sp" /> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="点击验证码,换一张" /> </LinearLayout> <LinearLayout android:layout_width="match_parent" android:layout_height="wrap_content" > <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="输入验证码" /> <EditText android:id="@+id/editText1" android:layout_width="match_parent" android:layout_height="wrap_content" android:ems="10" android:inputType="@R_874_10793@er" > <requestFocus /> </EditText> </LinearLayout> <Button android:id="@+id/button1" android:layout_width="match_parent" android:layout_height="wrap_content" android:text="验证" /> </LinearLayout>
4、自定义AuthCodeView.class
继承View,重写了
onMeasure(int widthMeasureSpec,int heightMeasureSpeC)
onDraw(Canvas canvas)方法。
看代码,有详细注释了。
package com.example.authcodeview.view; import java.util.Random; import com.example.authcodeview.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.TypedValue; import android.view.View; public class AuthCodeView extends View { // 点数设置 public static final int POINT_NUM = 100; // 线段数设置 public static final int LINE_NUM = 2; //文本 private String mtitleText; // 文本的颜色 privatE int mtitleTextColor; // 文本的大小 privatE int mtitleTextSize; String[] mcheckNum = new String[4]; Random random = new Random(); //绘制时控制文本绘制的范围 private Rect mBound; private Paint mPaint; public AuthCodeView(Context context,AttributeSet attrs) { this(context,attrs,0); } public AuthCodeView(Context context) { this(context,null); } /** * 获得我自定义的样式属性 * * @param context * @param attrs * @param defStyle */ public AuthCodeView(Context context,AttributeSet attrs,int defStylE) { super(context,defStylE); /** * 获得我们所定义的自定义样式属性 */ TypedArray a = context.getTheme().obtainStyledAttributes(attrs,R.styleable.AuthCodeView,defStyle,0); //获取在attr文件下,名字为AuthCodeView的declare-styleable属性有几个 int n = a.geTindexCount(); for (int i = 0; i < n; i++) { int attr = a.geTindex(i); switch (attr) { //这个属性可以不要,因为都是随机产生 case R.styleable.AuthCodeView_titleText: mtitleText = a.getString(attr); break; case R.styleable.AuthCodeView_titleTextColor: // 默认颜色设置为黑色 mtitleTextColor = a.getColor(attr,Color.bLACK); break; case R.styleable.AuthCodeView_titleTextSize: // 默认设置为16sp,TypeValue也可以把sp转化为px mtitleTextSize = a.getDimensionPixelSize(attr,(int) TypedValue.applyDimension( TypedValue.COMPLEX_UNIT_SP,16,getresources().getDisplaymetrics())); break; } } a.recycle(); mtitleText = randomText(); /** * 获得绘制文本的宽和高 */ mPaint = new Paint(); mPaint.setTextSize(mtitleTextSizE); mBound = new Rect(); mPaint.getTextBounds(mtitleText,mtitleText.length(),mBound); this.setOnClickListener(new OnClickListener() { @Override public void onClick(View v) { mtitleText = randomText(); posTinvalidate(); } }); } //随机产生验证码 private String randomText() { StringBuffer sbReturn = new StringBuffer(); for (int i = 0; i < 4; i++) { StringBuffer sb = new StringBuffer(); int randomInt = random.nexTint(10); mcheckNum[i] = sb.append(randomint).toString(); sbReturn.append(randomint); } return sbReturn.toString(); } //获取验证码 public String getAuthCode() { return mtitleText; } //重写这个方法,设置自定义view控件的大小 @Override protected void onMeasure(int widthMeasureSpec,int heightMeasureSpeC) { // super.onMeasure(widthMeasureSpec,heightMeasureSpec); int width = 0; int height = 0; /** * 设置宽度 */ int specMode = MeasureSpec.getMode(widthMeasureSpec); int specSize = MeasureSpec.getSize(widthMeasureSpec); switch (specModE) { case MeasureSpec.EXACTLY:// 明确指定了 width = getPaddingLeft() + getPaddingRight() + specSize; break; case MeasureSpec.AT_MOST:// 一般为WARP_CONTENT width = getPaddingLeft() + getPaddingRight() + mBound.width(); break; } /** * 设置高度 */ specMode = MeasureSpec.getMode(heightMeasureSpec); specSize = MeasureSpec.getSize(heightMeasureSpec); switch (specModE) { case MeasureSpec.EXACTLY:// 明确指定了 height = getPaddingTop() + getPaddingBottom() + specSize; break; case MeasureSpec.AT_MOST:// 一般为WARP_CONTENT height = getPaddingTop() + getPaddingBottom() + mBound.height(); break; } setMeasuredDimension(width,height); } @Override protected void onDraw(Canvas canvas) { //画背景颜色 mPaint.setColor(Color.bLUE); canvas.drawRect(0,getMeasuredWidth(),getMeasuredHeight(),mPaint); //划线 mPaint.setColor(mtitleTextColor); int [] line; for(int i = 0; i < LINE_NUM; i ++) { //设置线宽 mPaint.setstrokeWidth(5); line = getLine(getMeasuredHeight(),getMeasuredWidth()); canvas.drawLine(line[0],line[1],line[2],line[3],mPaint); } // 绘制小圆点 int [] point; int randomInt; for(int i = 0; i < POINT_NUM; i ++) { //随机获取点的大小 randomInt = random.nexTint(5); point = getPoint(getMeasuredHeight(),getMeasuredWidth()); canvas.drawCircle(point[0],point[1],randomInt,mPaint); } //绘制验证控件上的文本 int dx = 20; for(int i = 0; i < 4; i ++){ canvas.drawText("" + mcheckNum[i],dx,getHeight() / 2 + getPositon(mBound.height() / 2),mPaint); dx += (getWidth() / 2 - mBound.width() / 2) + i / 5 + 20; } // canvas.drawText(mtitleText,getWidth() / 2 - mBound.width() / 2,getHeight() / 2 + mBound.height() / 2,mPaint); } //计算验证码的绘制y点位置 privatE int getPositon(int height) { int tempPositoin = (int) (Math.random() * height); if (tempPositoin < 20) { tempPositoin += 20; } return tempPositoin; } // 随机产生点的圆心点坐标 public static int[] getPoint(int height,int width) { int[] tempcheckNum = { 0,0 }; tempcheckNum[0] = (int) (Math.random() * width); tempcheckNum[1] = (int) (Math.random() * height); return tempcheckNum; } //随机产生划线的起始点坐标和结束点坐标 public static int[] getLine(int height,0 }; for (int i = 0; i < 4; i += 2) { tempcheckNum[i] = (int) (Math.random() * width); tempcheckNum[i + 1] = (int) (Math.random() * height); } return tempcheckNum; } } 5、在MainActivity中怎么使用这个自定义AuthCodeView package com.example.authcodeview; import com.example.authcodeview.view.AuthCodeView; import android.os.bundle; import android.app.Activity; import android.view.View; import android.view.View.onClickListener; import android.widget.EditText; import android.widget.Toast; public class MainActivity extends Activity implements OnClickListener { private AuthCodeView mauthCodeView; @Override protected void onCreate(Bundle savedInstanceStatE) { super.onCreate(savedInstanceStatE); setContentView(R.layout.activity_main); initUI(); } private void initUI() { mauthCodeView = (AuthCodeView)findViewById(R.id.AuthCodeView); findViewById(R.id.button1).setOnClickListener(this); } @Override public void onClick(View v) { switch (v.getId()) { case R.id.button1: EditText editText = (EditText)findViewById(R.id.editText1); String codeString = editText.getText().toString().trim(); if (codeString.equals(mauthCodeView.getAuthCode())) { Toast.makeText(this,"验证码验证正确!",Toast.LENGTH_LONG).show(); }else { Toast.makeText(this,"验证码错误!",Toast.LENGTH_LONG).show(); } break; default: break; } } }
源码下载:Android生成随机验证码Demo
以上就是本文的全部内容,希望对大家的学习有所帮助。
以上是大佬教程为你收集整理的Android自定义控件深入学习 Android生成随机验证码全部内容,希望文章能够帮你解决Android自定义控件深入学习 Android生成随机验证码所遇到的程序开发问题。
如果觉得大佬教程网站内容还不错,欢迎将大佬教程推荐给程序员好友。
本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。