Android自定义View--验证码控件

运行效果:

Android自定义View--验证码控件_第1张图片
该例子主要用了
1.自定义View的onDraw()方法
2.Utils类中的一些设置圆点,横线,数字坐标的方法

onDraw()绘图过程由ViewRoot对象的performTraversals()方法调用onDraw()方法发起绘制该View树,值得注意的是每次发起绘图时,并不会重新绘制每个View树的视图,而只会重新绘制那些“需要重绘”的视图,View类内部变量包含了一个标志位DRAWN,当该视图需要重绘时,就会为该View添加该标志位。但是onDraw()方法还是会调用,是否重绘还是要看标志位。

布局文件:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    tools:context=".MainActivity" >

    <com.example.checkview.widget.CheckView 
        android:id="@+id/cv"
        android:layout_width="200dip"
        android:layout_height="50dip"
        />

    <EditText 
                android:id="@+id/et"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        />

    <Button
        android:id="@+id/button1"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:text="验证" />

    <Button
        android:id="@+id/button2"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:text="刷新" />

LinearLayout>

Utils工具类:

private static int roundNum=10;//小圆点默认值
    public static int getRoundNum() {
        return roundNum;
    }
    /**
     * 设置小圆点数量
     * @param roundNum
     */
    public static void setRoundNum(int roundNum) {
        CheckViewUtils.roundNum = roundNum;
    }

    public static int getCheckNumCount() {
        return checkNumCount;
    }
    /**
     * 设置验证码数量
     * @param checkNumCount
     */
    public static void setCheckNumCount(int checkNumCount) {
        CheckViewUtils.checkNumCount = checkNumCount;
    }
    private static int checkNumCount=4;//验证码位数

    private static Random Rd=new Random();
    /**
     * 获取验证码
     * @return
     */
    public static int[] getCheckNum(){
        int[] checkNum=new int[checkNumCount];
        for(int i=0;iint)(Rd.nextInt(10));
        }
        return checkNum;
    }

    /**
     * 获取圆点位置
     * @param width
     * @param height
     * @return
     */
    public  static int[] getRound(int width,int height){
        int[] roundArr={0,0};
        roundArr[0]=(int)(Rd.nextInt(width));
        roundArr[1]=(int)(Rd.nextInt(height));
        return roundArr;
    }
    /**
     * 获取线条的起点和终点坐标
     * @param width
     * @param height
     * @return
     */
    public  static int[] getLine(int width,int height){
        int[] lineArr={0,0,0,0};
        for(int i=0;i<4;i+=2){
            lineArr[i]=(int)(Rd.nextInt(width));
            lineArr[i+1]=(int)(Rd.nextInt(height));
        }
        return lineArr;
    }
    /**
     * 验证是否通过
     * @param numString 用户输入数字
     * @param checkNum  验证的数字
     * @return
     */
    public  static boolean validationNum(String numString,int[] checkNum){
        if(numString.length()!=checkNumCount)
            return false;
        String checkNumString="";
        for(int i=0;iif(checkNumString.equals(numString)){
            return true;
        }else{
            return false;
        }
    }
    /**
     * 获取Y点坐标
     * @param height
     * @return
     */
    public static int getY(int height){
        int y=(int)(Rd.nextInt(height));
        if(y>height-60){
            y-=60;
        }
        if(y<60){
            y+=60;
        }
        return y;
    }

自定义View:

public class CheckView extends View {

    private Paint paint=new Paint();

    public int[] checkNum;

    public CheckView(Context context, AttributeSet attrs) {
        super(context, attrs);
        // TODO Auto-generated constructor stub
        paint.setTextSize(60);
        paint.setStrokeWidth(6);
    }

    @Override
    protected void onDraw(Canvas canvas) {
        canvas.drawColor(Color.BLUE);
        int width=getWidth();
        int height=getHeight();

        int checkNumX=40;
        if(checkNum==null){
                canvas.drawText("请设置setCheckNum", checkNumX, CheckViewUtils.getY(height), paint);
        }else{
            for(int i=0;i"", checkNumX, CheckViewUtils.getY(height), paint);
                checkNumX+=width/5;
            }
        }

        for(int i=0;i<2;i++){
            int[] line=CheckViewUtils.getLine(width, height);//获取线条起点和终点
            canvas.drawLine(line[0], line[1], line[2], line[3], paint);
        }


        for(int i=0;i<10;i++){
            int[] round=CheckViewUtils.getRound(width, height);//获取小圆点位置
            canvas.drawPoint(round[0], round[1], paint);
        }

    }

    public int[] getCheckNum() {//获取验证码
        return checkNum;
    }

    /**
     * 该方法必须被调用
     * @param checkNum
     */
    public void setCheckNum(boolean bl) {
        if(bl){
            this.checkNum = CheckViewUtils.getCheckNum();//获取验证码
        }
    }
    /**
     * 刷新验证码
     * invalidate和postInvalidate方法会重新调用onDraw()方法
     */
    public void notifyDataSetChanged(){
//      invalidate();
        setCheckNum(true);
        postInvalidate();
    }
}

MainActivity:

public class MainActivity extends Activity {

    private Button btnSubmit;
    private CheckView cv;
    private Button btnS;
    private EditText et;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        btnSubmit=(Button) findViewById(R.id.button1);
        et=(EditText) findViewById(R.id.et);
        cv=(CheckView) findViewById(R.id.cv);
        cv.setCheckNum(true);//设置启用验证码数字,必须设置该属性
        btnS=(Button) findViewById(R.id.button2);
        btnS.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {
                // TODO Auto-generated method stub
                cv.notifyDataSetChanged();
            }
        });
        btnSubmit.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {
                // TODO Auto-generated method stub
                String etString=et.getText().toString().trim();
                int[] checkNum=cv.getCheckNum();
                if(CheckViewUtils.validationNum(etString, checkNum)){
                    Toast.makeText(MainActivity.this, "验证通过", 1).show();
                }else{
                    Toast.makeText(MainActivity.this, "验证失败", 1).show();
                }
            }
        });

    }
}

源码下载:http://download.csdn.net/detail/qq1377399077/8672545

你可能感兴趣的:(Android项目实例)