| First, the problem description |
Familiar with web development children's shoes know that in order to prevent malicious cracking, malicious submissions, swipe tickets, etc. when we submit form data, we will use the random captcha function. In Android applications We also need this feature, how to do it, we will customize a random verification Code view control to achieve this requirement, and has the versatility, need to add the view component directly in the interface.
| Second, Case introduction |
Case Run effect
Components involved in the case
1, Checkview custom code control, mainly rewrite the OnDraw method to achieve graphical drawing
2, config: For the verification Code control parameters of the configuration, such as Pip points, number of dashes, background color settings
3, Checkutil: Verification code related tools, such as random point coordinates, random line start and end point coordinates, verification code check and other functions
4. Mainactivity: Test Application
| Third, the function realization |
1. Write Config component
/** * Function: Configuration for Verification Code control parameters * */public class Config { //captcha Update time public static final int ptede_time =; Points set public static final int point_num = +; Number of segments set public static final int line_num = 2; Set background color public static final int color=color.blue; Random data length public static int text_length=4; Set the Captcha font size public static int text_size=30;}
2, CheckutilComponents
/** * Function: Verification Code related Tool class * */public class checkutil{/** * Generate random number * @return */public static int [] Getchecknum () {int [] Tempchecknum = new Int[config.text_length]; for (int i = 0; i < config.text_length; i++) {tempchecknum[i] = (int) (Math.random () * 10); } return tempchecknum; }/** * Randomly generates the starting point and end point coordinates of the dash * @param height of incoming Checkview * @param width value of incoming Checkview * @return start point Coordinates and end point coordinates */public static int[] GetLine (int height, int width) {int [] Tempchecknum = {0,0,0,0}; for (int i = 0; i < 4; i+=2) {tempchecknum[i] = (int) (Math.random () * width); Tempchecknum[i + 1] = (int) (Math.random () * height); } return tempchecknum; }/** * Randomly generates the center point coordinate of the point * @param height of incoming Checkview * @param width value of the input Checkview * @return */ public static int[] GetPoint (int height, int width) {int [] Tempchecknum = {0,0,0,0}; TEMPCHECKNUM[0] = (int) (Math.random () * width); TEMPCHECKNUM[1] = (int) (Math.random () * height); return tempchecknum; }/** * Verify correct * @param usercheck user Input Verification code * @param checknum validation control generates random number * @return */Publi C Static Boolean checknum (String usercheck, int[] checknum) {if (Usercheck.length ()! = 4) {return FA Lse } String checkstring = ""; for (int i = 0; i < 4; i++) {checkstring + = Checknum[i]; } if (Usercheck.equals (checkstring)) {return true; } else {return false; }}/** * Calculates the draw Y-point position of the verification code * @param height value of incoming Checkview * @return */public static int GetPos Iton (int height) {int temppositoin = (int) (Math.random () * height); if (Temppositoin <) {Temppositoin + = 20; } return temppositoin; }}
3. Custom Captcha Control Checkview
public class Checkview extends view{Context mcontext; int [] checknum = null; Paint Mtemppaint = new paint (); Captcha Public Checkview (context context, AttributeSet Attrs) {Super (context, attrs); Mcontext = context; Mtemppaint.setantialias (TRUE); Mtemppaint.settextsize (config.text_size); Mtemppaint.setstrokewidth (3); } public void OnDraw (canvas canvas) {canvas.drawcolor (config.color); Final int height = getheight ();//Gets Checkview control's height final int width = getwidth ();//Gets the width of the checkview control int dx = 4 0; for (int i = 0; i < 4; i + +) {//Draw text on validation control Canvas.drawtext ("" + checknum[i], DX, Checkutil.getpositon (height) , Mtemppaint); DX + = WIDTH/5; } int [] line; for (int i = 0; i < config.line_num; i + +) {//Dash line = checkutil.getline (height, width); Canvas.drawline (Line[0], line[1], line[2], line[3], mtemppaint); }//Draw a small circleDot int [] point; for (int i = 0; i < config.point_num; i + +) {//Draw point Point=checkutil.getpoint (height, width); Canvas.drawcircle (Point[0], point[1], 1, mtemppaint); }} public void Setchecknum (int [] chencknum) {//Set verification code checknum = Chencknum; } public int[] Getchecknum () {//Get Verification code return checknum; } public void Invalichenknum () {invalidate (); } }
4. Write mainactivity test code
public class Mainactivity extends Activity implements view.onclicklistener{private checkaction Mcheckview; Private TextView Mshowpassviwe; Private EditText Meditpass; Private Button Msubmit; Private Button mref; Verification code: private int [] Checknum =null; public void OnCreate (Bundle savedinstancestate) {super.oncreate (savedinstancestate); Setcontentview (R.layout.main); Initview (); Initchecknum (); } public void Initview () {Mcheckview = (Checkview) Findviewbyid (R.id.checkview); Mshowpassviwe = (TextView) Findviewbyid (R.id.checkpass); Meditpass = (EditText) Findviewbyid (r.id.checktest); Msubmit = (Button) Findviewbyid (r.id.submit); Mref = (Button) Findviewbyid (R.ID.REF); Msubmit.setonclicklistener (this); Mref.setonclicklistener (this); }//Initialize verification code and refresh interface public void Initchecknum () {checknum = Checkutil.getchecknum (); Mcheckview.setchecknum (checknUM); Mcheckview.invalichenknum (); } public void OnClick (View v) {switch (V.getid ()) {r.id.submit:string userinput = Meditpass.gettext (). toString (); if (Checkutil.checknum (Userinput, Checknum)) {setpassstring ("through"); Toast.maketext (This, "Pass,"). Show (); }else{setpassstring ("failed"); Toast.maketext (This, "failed", +). Show (); } break; Case R.id.ref:initchecknum (); Break Default:break; }} public void Setpassstring (String passstring) {mshowpassviwe.settext (passstring); }}
Android Implementation Random verification code--Custom View