Using the Android custom control to implement sliding unlock nine lattice _android

Source: Internet
Author: User
Tags gety map class touch

This article outlines:

Analysis of the sliding unlock nine Sudoku:

1, need to customize the control;
2, need to rewrite the event ontouchevent ();
3, need to give nine points set ordinal number and coordinates, here with the Map class on line;
4, need to determine whether to slip to one of nine points, and store the number of points that have slipped, and need a way to return them, here with the list class on the line;

Sliding unlock current still more popular, today wrote a simple slide unlock nine Sudoku routines, share out let beginners see.

Mine is this:

Demo

First, customize a view

/** * Nine * * * public class Ninegridview extends View {private int width;//the control's wide private int height;//The control's high privat E Paint mpaintbigcircle;//is used for drawing outer circle private Paint mpaintsmallcircle;//for drawing inner circle private Paint mpaintline;//for drawing lines private Pain T mpainttext;//is used to draw text private Path path;//gesture dashes need to use it private Map<integer, float[]> pointcontainer;//store nine points of coordinates PR Ivate list<integer> pointerslipped;//stored the nine Sudoku password public list<integer> getpointerslipped () {return pointe
  rslipped;
  public void setpointerslipped (list<integer> pointerslipped) {this.pointerslipped = pointerslipped;
  Public Ninegridview {Super (context);
    Public Ninegridview (context, AttributeSet attrs) {Super (context, attrs);
    Mpaintbigcircle = new Paint ();
    Mpaintbigcircle.setcolor (Color.Blue);  Mpaintbigcircle.setstyle (Paint.Style.STROKE);//not filled with mpaintbigcircle.setantialias (true);/anti-aliasing open mpaintsmallcircle =
    New Paint (); MpaintsmaLlcircle.setcolor (Color.green);
    Mpaintsmallcircle.setstyle (Paint.Style.FILL)/full, that is, the painted geometry is solid mpaintsmallcircle.setantialias (true);
    Mpaintline = new Paint ();
    Mpaintline.setcolor (Color.green);
    Mpaintline.setstyle (Paint.Style.STROKE);
    Mpaintline.setstrokewidth (20);
    Mpaintline.setantialias (TRUE);
    Mpainttext = new Paint ();
    Mpainttext.setcolor (Color.White);
    Mpainttext.settextalign (Paint.Align.CENTER), to the central alignment mpainttext.settextsize (50);
    Mpainttext.setantialias (TRUE);
    Path = new Path ();
    Pointcontainer = new hashmap<> ();
  pointerslipped = new arraylist<> (); @Override protected void onmeasure (int widthmeasurespec, int heightmeasurespec) {super.onmeasure (widthmeasuresp
    EC, HEIGHTMEASURESPEC);
    width = getdefaultsize (Getsuggestedminimumwidth (), widthmeasurespec);
  Height = getdefaultsize (getsuggestedminimumheight (), heightmeasurespec); The y-coordinate PR of the X coordinate obtained by private float pivotx;//touch screen, private float pivoty;//touch screenivate float selectedx;//The x coordinate of the currently selected dot, private float selectedy;//the y-coordinate of the currently selected dot, private float selectedxold;//The x-coordinate of the previously selected dot rivate float selectedyold;//The y-coordinate of the previously selected dot to private Boolean ishasmoved = false;//to determine if path has invoked the MoveTo () method @Override Publi C Boolean ontouchevent (Motionevent event) {switch (event.getaction ()) {case Motionevent.action_down:p
        Ivotx = Event.getx ();
        Pivoty = Event.gety ();
        Each touch screen needs to clear the pointerslipped, that is, reset password pointerslipped.clear ();
        LOG.D ("pointtouched", Pivotx + "," + Pivoty);
        Getselectedpointindex (Pivotx, Pivoty);
      Invalidate ()//Redraw break;
        Case MotionEvent.ACTION_MOVE:pivotX = Event.getx ();
        Pivoty = Event.gety ();
        Getselectedpointindex (Pivotx, Pivoty);
        Invalidate ();
      Break
        Case MOTIONEVENT.ACTION_UP:/** * Reset Path/Path.reset () when the finger leaves the screen;
        Ishasmoved = false;
        String indexsequence = ""; Print outLast gesture password value for (int index:pointerslipped) {indexsequence + = "/" +INDEX;
        } log.d ("index", indexsequence);
    Break
    } invalidate ();
  return true; /** * Gets and stores the serial number of the passed dots * @param pivotx * @param pivoty/private void Getselectedpointindex (float pivotx,
    float pivoty) {int index = 0;  if (Pivotx > Patternmargin && pivotx < Patternmargin + Bigcircleradius * 2) {if (Pivoty > Height/
        2 && Pivoty < HEIGHT/2 + Bigcircleradius * 2) {Selectedx = Pointcontainer.get (1) [0];
        Selectedy = Pointcontainer.get (1) [1];
        index = 1;
      LOG.D ("Selectedpoint", Selectedx + "," + selectedy); else if (Pivoty > HEIGHT/2 + added && Pivoty < HEIGHT/2 + added + Bigcircleradius * 2) {Selec
        TedX = Pointcontainer.get (4) [0];
        Selectedy = Pointcontainer.get (4) [1];
      index = 4; else if (Pivoty > HEIGHT/2 + added * 2 && PIvoty < HEIGHT/2 + Added * 2 + Bigcircleradius * 2) {Selectedx = Pointcontainer.get (7) [0];
        Selectedy = Pointcontainer.get (7) [1];
      index = 7;
      Or else if (Pivotx > Patternmargin + added && Pivotx < Patternmargin + added + Bigcircleradius * 2) { if (Pivoty > HEIGHT/2 && pivoty < HEIGHT/2 + Bigcircleradius * 2) {Selectedx = Pointcontai
        Ner.get (2) [0];
        Selectedy = Pointcontainer.get (2) [1];
      index = 2; else if (Pivoty > HEIGHT/2 + added && Pivoty < HEIGHT/2 + added + Bigcircleradius * 2) {Selec
        TedX = Pointcontainer.get (5) [0];
        Selectedy = Pointcontainer.get (5) [1];
      index = 5;
        else if (Pivoty > HEIGHT/2 + added * 2 && pivoty &LT;HEIGHT/2 + added * 2 + Bigcircleradius * 2) {
        Selectedx = Pointcontainer.get (8) [0];
        Selectedy = Pointcontainer.get (8) [1];
      index = 8; } else if (Pivotx > patterNmargin + added * 2 && Pivotx < Patternmargin + Added * 2 + Bigcircleradius * 2) {if (Pivoty > height
        /2 && Pivoty < HEIGHT/2 + Bigcircleradius * 2) {Selectedx = Pointcontainer.get (3) [0];
        Selectedy = Pointcontainer.get (3) [1];
      index = 3; else if (Pivoty > HEIGHT/2 + added && Pivoty < HEIGHT/2 + added + Bigcircleradius * 2) {Selec
        TedX = Pointcontainer.get (6) [0];
        Selectedy = Pointcontainer.get (6) [1];
      index = 6;
        else if (Pivoty > HEIGHT/2 + added * 2 && Pivoty < HEIGHT/2 + Added * 2 + Bigcircleradius * 2) {
        Selectedx = Pointcontainer.get (9) [0];
        Selectedy = Pointcontainer.get (9) [1];
      index = 9; } if (selectedx!=selectedxold| |
      Selectedy!=selectedyold) {///when this coordinate is not at the same time as the last coordinate store this point serial number Pointerslipped.add (index);
      Selectedxold = Selectedx;
      Selectedyold = Selectedy; if (!ishasmoved) {//when first touched nineOne point, path calls MoveTo;
        Path.moveto (Selectedx,selectedy);
      Ishasmoved = true;
      }else{//path move to the current dot coordinate path.lineto (selectedx,selectedy);
  }} private String text = "Please draw unlock pattern"; Private float x;//Draw Circle x coordinate private float y;//draw circular ordinate private float added;//horizontal vertical direction Each dot center spacing private float Patternmar gin = 100;//Nine Sudoku distance boundary distance private float Bigcircleradius = 90;//Outer circle radius Private float Smallcircleradius = 25;//inner circle radius Private
    The ordinal of an int index;//dot @Override protected void OnDraw (Canvas Canvas) {Super.ondraw (Canvas);
    Added = (Width-patternmargin * 2)/3;
    x = Patternmargin + added/2;
    y = added/2 + height/2;
    index = 1;
    Canvas.drawcolor (Color.Black);
    Canvas.drawtext (text, WIDTH/2, HEIGHT/4, Mpainttext);  /** * Draw Nine dot pattern * * for (int column = 0; column < 3; column++) {for (int row = 0; row < 3; row++)
        {canvas.drawcircle (x, Y, Bigcircleradius, mpaintbigcircle); Canvas.dRawcircle (x, Y, Smallcircleradius, mpaintsmallcircle);
        Pointcontainer.put (index, new float[]{x, y});
        index++;
      x + + added;
      } y + + added;
    x = Patternmargin + added/2;
    } x = Patternmargin + added/2;
    y = added/2 + height/2;
  Canvas.drawpath (path, mpaintline); }
}

Why avoid repetition?

Because the touch screen, will be called many times ontouchevent () method, so that the stored gesture password will be inaccurate, I have in the above code to deal with, has avoided duplication, look at printing information:

Here to write a picture description

Obviously, the password does not repeat the number of adjacent digits, of course, there is a situation where the fingers back and forth between the two points, this situation also need to avoid, there is no processing. Of course, I didn't do enough ...

Customize the DP and PX conversion tool classes used in view:

public class Sizeconvert {
  /**
   * Converts a DP to SP
  /public static int dip2px (context, float dipvalue) { c5/>final Float scale = context.getresources (). Getdisplaymetrics (). density;
    return (int) (Dipvalue * scale + 0.5f);
  }
  /**
   * SP DP
   /public
  static int Px2dip (context, float Pxvalue) {
    final float scale = CONTEXT.G Etresources (). Getdisplaymetrics (). density;
    return (int) (Pxvalue/scale + 0.5f);
  }
}

Main activity:

public class Ninegridactivity extends baseactivity{
  @Override
  protected void OnCreate (Bundle Savedinstancestate) {
    super.oncreate (savedinstancestate);
    Setcontentview (R.layout.view_nine_grid);
  }

Layout file View_nine_grid in layout:

<?xml version= "1.0" encoding= "Utf-8"?> <linearlayout xmlns:android=
"http://schemas.android.com/apk/" Res/android "
       android:orientation=" vertical "
       android:layout_width=" match_parent "
       android:layout_" height= "Match_parent" >
  <com.test.shiweiwei.myproject.selfish_view. Ninegridview
    android:layout_width= "match_parent"
    android:layout_height= "match_parent"/>
< /linearlayout>

Summarize

I wrote just the most basic nine Sudoku sliding decryption project, the actual use of the nine Sudoku decryption than this to be complex, there are many special effects and other more rigorous processing, the handling of events is not so hasty, if you want to write beautiful, still have to spend more time.

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.