sometimes in order to safety procedures, we often need to take safety measures, as we used Alipay, at certain time to return to the application will allow users to use gestures to unlock applications recently due to the needs of the project, is also required to do such a function, when the user cut out this application for 15 minutes after Back, let the user's gestures unlock, the difficulty of the whole demand is how to achieve this gesture lock, start a little bit of thread, no idea to achieve this gesture unlocking function, after the Google after a very good blog, according to the blogger's idea can achieve a very good gesture lock View, and Referring to the code of the next big God, the following is a custom gesture that I do unlock View based on his ideas and code fragments. First look at the effect map.

, which is the initial effect diagram of the custom View:

is the effect of drawing gestures:

 Util.ArrayList; import java.util.List; import java.util.Timer; import java.util.TimerTask; import android.content.Context; import android.graphics.Canvas; import; Android.view.MotionEvent; import android.view.View; import com.example.gesturelock.GestureLockView.OnGestureFinishListener; public class MyGestureLockView extends View {/ * * * / * * * / private Paint] PaintInnerCycle; private Paint paintLines; private Paint paintKeyError; private MyCycle[] cycles; private Path. Ring key; private int eventX, eventY; private Boolean canContinue = true; private Boolean. UCH = Color.rgb (025, 066, 103); / / private int INNER_CYCLE_ONTOUCH = Color.rgb (002, 210, 255); / / / private int LINE_COLOR = Color.argb (127, 002, 210, 255); / / ); public void setOnGestureFinishListener (OnGestureFinishListener onGestureFinishListener) {this.onGestureFinishListener = onGestureFinishListener;} public void setKey (String key) {} RS, int defStyle) {super (context, attrs, defStyle); init ();} public MyGestureLockView (Context context). It {() {paintNormal = new Paint (); paintNormal.setAntiAlias (true); paintNormal.setStrokeWidth (3); paintNormal.setStyle (Paint.Style.STROKE); paintOnTouch = new Paint (); 3. Aint.Style.STROKE); paintInnerCycle = new Paint (); paintInnerCycle.setAntiAlias (true); paintInnerCycle.setStyle (Paint.Style.FILL); paintLines = new Paint (); paintLines.setAntiAlias. (6); PaintKeyError = new Paint (); paintKeyError.setAntiAlias (true); paintKeyError.setStyle (Paint.Style.STROKE); paintKeyError.setStrokeWidth (3);} @Override protected void onMeasure. Super.onMeasure (widthMeasureSpec, heightMeasureSpec);} @Override protected void onLayout (Boolean changed, int left, int); (cycles = = null & & (perSize = getWidth () / 6) > 0) {cycles = new MyCycle[9]; for (int = 0; 3) {0; 3; 3 + 2. 1)); cycle.setOy (perSize * (I * 2 + 1)); cycle.setR (perSize * 0.5f); cycles[i * 3 + j] = cycle;}}}} / * * draw the required content * / @Override protected void {/ /} Ethod stub super.onDraw (canvas); for (int i = 0; I < cycles.length; i++) {if (! .isOnTouch ()) {paintOnTouch.setColor (OUT_CYCLE_ONTOUCH); paintInnerCycle.setColor (INNER_CYCLE_ONTOUCH); paintLines.setColor (LINE_COLOR);} else {paintNormal.setColor (OUT_CYCLE_NORMAL); paintInnerCycle.setColor (INNER_CYCLE_ONTOUCH); TLines.setColor (LINE_COLOR);} if (cycles[i].isOnTouch ()) {canvas.drawCircle (cycles[i].getOx (), cycles[i].getOy (), cycles[i].getR (), paintOnTouch); drawInnerBuleCycle (cycles[i], canvas);} {{{(}), Cycles[i].getOy (), cycles[i].getR (), paintNormal);}} drawLine (canvas);} / * * * plotting the small circle in the big circle * * @param canvas * / private void drawInnerBuleCycle (MyCycle cycle, {} () ()). Cycle.getR () / 3, paintInnerCycle);} private void drawLine (Canvas canvas) {linePath.reset (); if (linedCycles.size () > 0) {for = {for = {0;} {= = 0) {/ / / / / / settings LinePath.moveTo (cycles[index].getOx () (), cycles[i].getOy ()) for the starting point of the entire path;} else {linePath.lineTo (cycles[i].getOx ()), cycles[i].getOy ());}} linePath.lineTo (eventX, eventY); canvas.drawPath (linePath,)}}}. / * * * modify the corresponding state values * / @Override public Boolean onTouchEvent (MotionEvent event) {if (canContinue) {switch (event.getAction ()) {case MotionEvent.ACTION_DOWN:] = (int) event.getX (); eventY = (int) event.getY (); for (int i = 0; I < cycles.length; i++) {{] {]) {] S.add (cycles[i].getNum ());}}} break; case MotionEvent.ACTION_UP: canContinue = false; StringBuffer sb = new StringBuffer (); for. } result = key.equals (sb.toString ()); if (onGestureFinishListener! = null) {onGestureFinishListener.OnGestureFinish (result);} timer = new Timer (); timer.schedule () {{} / / / back to the initial state eventX = eventY = 0; for (int i = 0; I < cycles.length; i++) {cycles[i].setOnTouch (false);} linedCycles.clear (). Invalidate ();}}, 1000); break;}} invalidate (); return true;}  

<p> custom circle class: </p>
<pre> package com.example.gesturelock; Private float R; / / private Integer num; / / false= delta public int {} {{} {} {} {} {} {} {} {} {} {} {} {} {} {} Return oy;} public void setOy (int oy) {this.oy = oy;} public float getR () {} An isOnTouch () {return onTouch;} public void setOnTouch (Boolean onTouch) {this.onTouch = onTouch;} </p>
<p> 1. custom a View and MyCircle class, draw the instance of the nine MyCircle class to the onTouch event, modify the onTouch event, modify the state of the MyCircle instance according to the different events, and call the update invaildate update View</p>
<p> 3. to rewrite the () method according to the different state. Making the whole View</p>
<p> above is the whole content of this article. I hope it will be helpful to everyone's learning, and I hope you will support the script home more. </p>
<div>
<h4> that you might be interested in: </h4>
<ul>
<li><a title= Android 5 seconds to learn to use gesture unlocking function


This concludes the body part