Android Nine Sudoku Programming code _android

Source: Internet
Author: User
Tags clear screen

This article describes the design code for the Nine program.

A Related introduction

(i) Effect display

1. The effect of the program just run:

2. Click on the page to select and slide the screen

3. Select the display after the password

(b) Functional Introduction

1. After clicking on a circle, add a small circle in the center of the circle.
2. The page slide appears with a following line.
3. When you slide to another circle, a straight line is generated that connects.
4. When the number of circles selected is greater than or equal to 4, the finger is raised and the password is saved.
4. When the number of circles selected is the maximum, save the password immediately.

(iii) The knowledge points involved

This example uses a custom view to draw the nine Sudoku and saves the image, where the nine dots represent nine values, and the corresponding numerical proofs are drawn to that point.
The knowledge that is used in the program:
1, the description of the image, circle and point of the description, line of the description
2, the location of the judge, to determine whether the location of the user point in a circle
3, the preservation of data and related judgments

Two Programming

(i) Design of Custom view

 Package Com.lwz.gongge;
Import Android.content.Context;
Import Android.graphics.Canvas;
Import Android.graphics.Color;
Import Android.graphics.Paint;
Import Android.util.AttributeSet;
Import android.view.MotionEvent;

Import Android.view.View;
Import java.util.ArrayList;

Import java.util.List;
 /** * Created by the Administrator on 2016/10/28 0028.
  */public class Ninegridview extends View {//constructor method public Ninegridview (context) {super (context);
 Init ();
  }//Construction method public Ninegridview (context, AttributeSet attrs) {Super (context, attrs);
 Init ();
 //define a drawing of a solid circle brush Paint paintcircle;
 Defines a drawing line of the brush Paint paintline;
 Defines a width value, probably one-fourth of the screen, representing the x-axis distance between each dot int width;
 Defines a background color int backcolor;
 Define a set to hold the clicked Dot 0 to 8 for nine dots list<integer> listpassword = new arraylist<> ();
 The position of the touch screen float currx, CurrY;
 Number of passwords int minpassnum = 4;

 int maxpassnum = 9;
  Initialize private void init () {//define background color BackColor = Color.rgb (0x17, 0x16, 0x25);
Instantiate the brush and make Basic settings  Paintline = new Paint ();
  Paintline.setantialias (TRUE);
  Paintline.setdither (TRUE);

  Paintline.setcolor (Color.rgb (0x37, 0x91, 0xe6));
  Paintcircle = new Paint ();
  Paintcircle.setantialias (TRUE);
  Paintcircle.setdither (TRUE);

 Paintcircle.setcolor (BackColor); }//Measure the callback method in this method, the width of the screen can be obtained @Override protected void onmeasure (int widthmeasurespec, int heightmeasurespec) {sup
  Er.onmeasure (Widthmeasurespec, Heightmeasurespec);
 width = getwidth ()/4;
  }///Screen image rendering callback method @Override protected void OnDraw (Canvas Canvas) {Super.ondraw (Canvas);

  Set the color of the screen, (Clear screen) Canvas.drawcolor (BackColor); Draw a line, if there is data in the data collection to draw line if (Listpassword.size () > 0) {//Find the last painting point to do the drawing line//Here is the coordinate point int x = Listpassword.get (list
   Password.size ()-1)% 3 + 1;//x axis direction int y = Listpassword.get (Listpassword.size ()-1)/3 + 1;//y axis Direction//Set drawing line Size
   Paintline.setstrokewidth (8);
   Draw the line, from the position of the concrete point to the position of the touch screen canvas.drawline (x * width, y * width, Currx, CurrY, paintline);
 and draw a circle to cover those lines in the circle.  Canvas.drawcircle (x * width, y * width, WIDTH/3, paintcircle); If there are other points in the data of the collection if (Listpassword.size () > 1) {//In order draw line for (int i = 0; i < listpassword.size ()-1; I
     + +) {//Prevent crossing//get the coordinate point of the current one I and back one I//previous point int x1 = listpassword.get (i)% 3 + 1;
     int y1 = Listpassword.get (i)/3 + 1;
     The coordinate point of the latter point int x2 = listpassword.get (i + 1)% 3 + 1;
     int y2 = listpassword.get (i + 1)/3 + 1;
     Set the size of the brush paintline.setstrokewidth (8);
     Draw a line from the position of the previous point to the position of the next point canvas.drawline (x1 * Width, y1 * width, x2 * width, y2 * width, paintline);
    Then draw a circle to cover those lines within the circle, which covers the back of a round line canvas.drawcircle (x1 * Width, y1 * width, WIDTH/3, paintcircle);
  //Draw nine circles with line//Set width paintline.setstrokewidth (2);
  Set the Paintline.setstyle (Paint.Style.STROKE) that the hollow must have;
    Start drawing a circle of 9, where the radius of the circle is temporarily set to 3 of the circle distance for (int i = 0; i < 3; i++) {for (int j = 0; J < 3; J +) {//A parameter is the coordinate value of the x axis of a dot) Two parameters are the coordinate values of the y-axis of a dot Canvas.drawcirclE (Width * (i + 1), Width * (j + 1), WIDTH/3, paintline);
  The y-axis of the image here is not necessarily in the center, but the whole is a square, the effect is almost OK}//Draw a solid circle inside the circle, the color and the color of the outside circle is the same, with the same pen//here to judge you across a few points, the collection to traverse//set the solid style
  Paintline.setstyle (Paint.Style.FILL);
   for (int i = 0; i < listpassword.size (); i++) {//Remove the number int p = listpassword.get (i) in the collection
   int x = p% 3;
   int y = P/3;
  A parameter is the coordinate value of the x axis of a dot//two parameter is the coordinate value of the Y axis of the dot canvas.drawcircle (width * (x + 1), Width * (y + 1), WIDTH/6, paintline); }//Touch screen Listener Event @Override public boolean ontouchevent (Motionevent event) {//Get user-clicked coordinate position float x = Event.getx ()
  ;

  Float y = event.gety (); To judge the user's behavior and make the appropriate operation switch (Event.getaction ()) {Case motionevent.action_down://touch screen//To determine whether the user clicks within a range of dots if (conne
     Tcircle (x, y)!=-1) {//update position CURRX = x;
     CurrY = y;
    Add the position of this point to the set of data that holds the Listpassword.add (connetcircle (x, y));
   } break; Case motionevent.action_up://Finger lift//If the value of the password is reached to the minimum number of digits, save the password to the main page if (listpassword.size() >= minpassnum) {//If the other side implements a listener event, give him the data if (listener!= null) {//Pass the password past Listener.tolistenert
     Hepassword (Getpasswordstring ());
    }//Empty data listpassword.clear ();
   Break
    Case motionevent.action_move://When the finger is moved//update position CURRX = x;
    CurrY = y;
    Move to another circle, then add the data to the collection//get the position int point = Connetcircle (x, y); If the point is in the circle and the data does not contain the value of the point, add the value of the point to the set if (Points!=-1 &&!listpassword.contains ((Integer)) {L
    Istpassword.add (point);  ///If the value of the password is saved to the maximum value, save the password to the main page if (listpassword.size () >= maxpassnum) {//If the other side implements the listener event, give him the data if (listener
     != null) {//Pass the password past Listener.tolistenerthepassword (getpasswordstring ());
    } break;
 }//Whatever the above is the last thing to refresh the screen invalidate ();//screen redraw return true; //To determine whether the user clicks on a point within a dot private boolean isincircle (float x, float y, float cx, float cy) {//x, y represents the coordinate position//cx, CY represents the Circle coordinate position//Circle point radius is WIDTH/3////If the position of the center minus the square radius of the centreSquared so this point is within the circle of Return (X-CX) * (X-CX) + (y-cy) * (Y-cy) < (WIDTH/3) * (WIDTH/3); //To determine whether the user clicks on a dot in the nine dot of which the private int connetcircle (float x, float y) {//x, y represents the coordinates position//In order to judge each circle to see if in its inside//dot sit Superscript position--> (width,width)-(2*width,width)-(3*width,width)//---> (width,2*width)-(2*width,2*width)-(3*width,  2*width)//---> (width,3*width)-(2*width,3*width)-(3*width,3*width) for (int i = 0; i < 3; i++) {for (int J = 0; J < 3; J + +) {if (isincircle (x, y, Width * (j + 1), Width * (i + 1))) {//(I,J)//If clicked is a circle, add the value of this point to the collection//0 1 2 (0,0 )/(0,1)/(0/2)//3 4 5 (1,0)/(1,1)/(1/2)//6 7 8 (2,0)/(2,1)/(2/2)//If 7, then the cursor value (2,1)//Returns the value of that point re
    Turn (3 * i + j);
 }///If it is not nine dots, return-1 return-1;
 //Create a callback interface for the main page to listen for this event interface Onfinishlistener {void Tolistenerthepassword (String s);

 //Set a listener interface object Onfinishlistener Listener; Sets the method of the Listener interface public void Setlistener (Onfinishlistener listener) {this.Listener = listener;
  //Get the password of the string public string getpasswordstring () {//define password String pass = "";
  Remove the number for the password in the collection for (int i = 0; i < listpassword.size (); i++) {pass + = Listpassword.get (i);
 return pass;
 }

}

(ii) The class that the main method calls this view

Package Com.lwz.gongge;

Import Android.os.Bundle;
Import android.support.v7.app.AppCompatActivity;
Import Android.widget.Toast;


The public class Mainactivity extends Appcompatactivity {

 //Custom view passes the password
 String password = "";

 @Override
 protected void onCreate (Bundle savedinstancestate) {
  super.oncreate (savedinstancestate);
  Define the custom view and instantiate
  Ninegridview nine = new Ninegridview (this);
  Displays the Custom view
  Setcontentview (nine);

  Get a custom password by listening method

  //Set Listener event
  Nine.setlistener (new Ninegridview.onfinishlistener () {
   @Override Public
   void Tolistenerthepassword (String passes) {
    ///pass is the custom view inside, the data passed over
    password = passing;
    Toast Password
    toast.maketext (mainactivity.this, "Password:" + password, toast.length_short). Show ();}}

Above is the code of the nine design. No XML layout files are used here, and are implemented in code.

After the custom view is completed, the invocation and use are simpler, can be used as a tool class, get to the custom view pass the password, can do other save operations, such as saving to the local file or database, here just do the toast operation, let the user see the effect.

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.