Easy to implement Android custom nine Sudoku pattern unlock _android

Source: Internet
Author: User
Tags pow

Android implementation of nine Sudoku pattern unlock, with the pattern into a digital password function, the code is as follows:

Lockpatternview.java

Package Com.jackie.lockpattern; 
Import Android.content.Context; 
Import Android.graphics.Canvas; 
Import Android.graphics.Paint; 
Import Android.graphics.Point; 
Import Android.text.TextUtils; 
Import Android.util.AttributeSet; 
Import Android.util.TypedValue; 
Import android.view.MotionEvent; 
 
Import Android.view.View; 
Import java.util.ArrayList; 
 
Import java.util.List; 
 /** * Created by Jackie on 2015/12/24. 
 * Pattern unlock/public class Lockpatternview extends View {/** * round brush/private Paint mcirclepaint; 
 /** * Line of the brush/private Paint mlinepaint; 
 /** * Center Array * * Private pointview[][] Mpointviewarray = new Pointview[3][3]; 
 
 
 /** * Save the collection of selected points * * Private list<pointview> mselectedpointviewlist; 
 
 /** * Unlock the pattern of edges * * private int mpatternwidth; 
 
 /** * Pattern Listener * * Private onpatternchangelistener Monpatternchangelistener; 
 
 /** * Radius/private float Mradius; 
 
 /** * The subscript for each circle/private int mindex = 1;/** * Whether the first point is selected * * Private Boolean misselected; 
 
 /** * Whether draw the end * * Private Boolean misfinished; 
 
 /** * is sliding and there is no point selected * * Private Boolean mismovingwithoutcircle = false; 
 
 Private float mcurrentx, mcurrenty; 
 /** * Normal Color * * private static final int normal_color = 0XFF70DBDB; 
 
 /** * The color of the selected state/private static final int selected_color = 0xff979797; 
 Public Lockpatternview {This (context, NULL); 
 
  Public Lockpatternview (context, AttributeSet attrs) {Super (context, attrs); 
  Mcirclepaint = new Paint (); 
  Mcirclepaint.setantialias (TRUE); 
  Mcirclepaint.setdither (TRUE); 
  Mcirclepaint.setcolor (Normal_color); 
 
  Mcirclepaint.setstyle (Paint.Style.FILL); 
  Mlinepaint = new Paint (); 
  Mlinepaint.setantialias (TRUE); 
  Mlinepaint.setdither (TRUE); 
  Mlinepaint.setstrokewidth (20); 
  Mlinepaint.setcolor (Selected_color); 
 
  Mlinepaint.setstyle (Paint.Style.STROKE); Mradius = Typedvalue.applydimensioN (Typedvalue.complex_unit_dip, Getresources (). Getdisplaymetrics ()); 
 Mselectedpointviewlist = new arraylist<> (); @Override protected void onmeasure (int widthmeasurespec, int heightmeasurespec) {super.onmeasure (widthmeasures 
 
  PEC, Heightmeasurespec); 
  Edges mpatternwidth = Math.min (Getmeasuredwidth (), Getmeasuredheight ()) that takes a smaller value in the screen length and width as a pattern; 
 Setmeasureddimension (Mpatternwidth, mpatternwidth); 
 
  @Override protected void OnDraw (Canvas Canvas) {//Draw round drawcircle (Canvas); Redraw the selected circle again, distinguishing the selected point from the unselected point for (Pointview pointview:mselectedpointviewlist) {Mcirclepaint.setcolor (Selected_c 
   Olor); 
   Canvas.drawcircle (pointview.x, Pointview.y, Mradius, Mcirclepaint); Mcirclepaint.setcolor (Normal_color); Each redraw, resetting the color of the brush to ensure that no other circles are drawn}//dot and dot draw line if (Mselectedpointviewlist.size () > 0) {point Pointviewa = MS Electedpointviewlist.get (0); The first selected point is Point A for (int i = 0; i < mselectedpointviewlist.size (); i++) {Point POINTVIEWB = Mselectedpointviewlist.get (i); 
    The other sequentially traversed points are point B drawLine (canvas, Pointviewa, POINTVIEWB); 
   Pointviewa = POINTVIEWB; //point to draw trajectory with mouse current position if (Mismovingwithoutcircle &!misfinished) {drawLine (canvas, Pointviewa, New Pointvie 
   W ((int) mcurrentx, (int) mcurrenty)); 
 } super.ondraw (canvas);  /** * Draw round * @param canvas canvas/private void drawcircle (canvas canvas) {//initialization point position for (int i = 0; i < Mpointviewarray.length; 
    i++) {for (int j = 0; J < Mpointviewarray.length; J + +) {//center coordinate int cx = MPATTERNWIDTH/4 * (j + 1); 
 
    int cy = MPATTERNWIDTH/4 * (i + 1); 
    Place the center of the circle in an array of points pointview Pointview = new Pointview (CX, CY); 
    Pointview.setindex (Mindex); 
    MPOINTVIEWARRAY[I][J] = Pointview; 
    Canvas.drawcircle (CX, CY, Mradius, Mcirclepaint); 
   mindex++; 
 } mindex = 1; /** * Draw line * @param canvas canvas * @param pointa The first point * @param pointb the second point * * private void DrawLine (Canvas Canvas, point Pointa, point pointb) {canvas.drawline (pointa.x, Pointa.y, pointb.x, POINTB). 
 Y, Mlinepaint); 
  @Override public boolean ontouchevent (Motionevent event) {Mcurrentx = Event.getx (); 
  Mcurrenty = Event.gety (); 
 
  Pointview Selectedpointview = null; 
     Switch (event.getaction ()) {case Motionevent.action_down://Redraw if (Monpatternchangelistener!= null) { 
    Monpatternchangelistener.onpatternstarted (TRUE); 
    } mselectedpointviewlist.clear (); 
 
    Misfinished = false; 
 
    Selectedpointview = Checkselectpoint (); 
    if (Selectedpointview!= null) {//The first pressed position within the circle, is selected misselected = true; 
   } break; 
    Case MotionEvent.ACTION_MOVE:if (misselected) {Selectedpointview = Checkselectpoint (); 
    } if (Selectedpointview = = null) {mismovingwithoutcircle = true; 
   } break; 
    Case MotionEvent.ACTION_UP:mIsFinished = true; misselected = false; 
    Break ///Collect selected points if (!misfinished && misselected && selectedpointview!= null) {if (!mselectedpo 
   Intviewlist.contains (Selectedpointview)) {Mselectedpointviewlist.add (Selectedpointview); 
   } if (misfinished) {if (mselectedpointviewlist.size () = = 1) {mselectedpointviewlist.clear (); else if (Mselectedpointviewlist.size () < 5 && mselectedpointviewlist.size () > 0) {//Drawing error if (MO 
    Npatternchangelistener!= null) {monpatternchangelistener.onpatternchange (null); 
    } else {//Draw success String Patternpassword = ""; if (Monpatternchangelistener!= null) {for (Pointview pointview:mselectedpointviewlist) {Patternpassword 
     + + Pointview.getindex (); } if (! 
     Textutils.isempty (Patternpassword)) {monpatternchangelistener.onpatternchange (Patternpassword); 
  }}} invalidate (); 
 return true; /** * Determines whether the currently pressed position is centeredThe * @return in the array returns the selected point/Private Pointview Checkselectpoint () {for (int i = 0; i < mpointviewarray.length i 
    + +) {for (int j = 0; J < Mpointviewarray.length; J + +) {Pointview Pointview = mpointviewarray[i][j]; 
    if (Iswithincircle (Mcurrentx, Mcurrenty, pointview.x, Pointview.y, Mradius)) {return pointview; 
 }} return null; /** * Determine whether the point is within the circle * @param x point x axis coordinate * @param y point y axis coordinate * @param CX Center x coordinate * @param cy Center y coordinate * @param Radius radius * @return True is within a circle, false means the outside of the circle/private Boolean iswithincircle (float x, float y, float cx, float CY, 
   Float radius) {//If the distance between the point and the center is less than the radius, the proof point is within the circle if (Math.sqrt (Math.pow (X-CX, 2) + Math.pow (Y-cy, 2)) <= radius) { 
  return true; 
 return false; 
  /** * Set pattern listener/public void Setonpatternchangelistener (Onpatternchangelistener onpatternchangelistener) { if (Onpatternchangelistener!= null) {This.monpatternchangelistener = OnpatternchangElistener;  }/** * Pattern listener/public interface Onpatternchangelistener {/** * pattern change * @param patternpassword 
 
  Pattern cipher */void Onpatternchange (String patternpassword); 
 Whether the/** * pattern is redrawn * @param isstarted redraw/void Onpatternstarted (Boolean isstarted);  } 
}

Pointview.java

Package Com.jackie.lockpattern; 
 
Import Android.graphics.Point; 
 
/** 
 * Created by Jackie on 2015/12/25. 
 * Custom Point Object * 
/public class Pointview extends points { 
 //subscript public int index for converting passwords 
 ; 
 
 public Pointview (int x, int y) { 
  super (x, y); 
 } 
 
 public int GetIndex () {return 
  index; 
 } 
 
 public void Setindex (int index) { 
  this.index = index; 
 } 

Mainactivity.java

Package Com.jackie.lockpattern; 
Import android.app.Activity; 
Import Android.os.Bundle; 
 
Import Android.widget.TextView; public class Mainactivity extends activity implements Lockpatternview.onpatternchangelistener {private TextView Mlockpa 
 Tternhint; 
 
 Private Lockpatternview Mlockpatternview; 
  @Override protected void OnCreate (Bundle savedinstancestate) {super.oncreate (savedinstancestate); 
 
  Setcontentview (R.layout.activity_main); 
  Mlockpatternhint = (TextView) Findviewbyid (r.id.lock_pattern_hint); 
  Mlockpatternview = (Lockpatternview) Findviewbyid (R.id.lock_pattern_view); 
 
 Mlockpatternview.setonpatternchangelistener (this); @Override public void Onpatternchange (String patternpassword) {if (Patternpassword = null) {Mlockpattern 
  Hint.settext ("At least 5 points"); 
  else {mlockpatternhint.settext (Patternpassword); }} @Override public void onpatternstarted (Boolean isstarted) {if (isstarted) {Mlockpatternhint.settext (" Please draw pattern ");  } 
 } 
}

The effect chart is as follows:

Attached Source Address: https://github.com/shineflower/LockPattern.git

The above is the entire content of this article, I hope to help you learn, but also hope that we support the cloud habitat community.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.