Android Nine-Sudoku slide unlock Boot instance source Learning _android

Source: Internet
Author: User
Tags drawtext gety stringbuffer
The effect of the map due to the site can not upload, later fill.
Ninepointlineview.java
Copy Code code as follows:

Package Org.demo.custon_view;
Import Org.demo.utils.MLog;
Import Android.content.Context;
Import Android.graphics.Bitmap;
Import Android.graphics.BitmapFactory;
Import Android.graphics.Canvas;
Import Android.graphics.Color;
Import Android.graphics.Paint;
Import Android.graphics.Paint.Cap;
Import Android.graphics.Typeface;
Import Android.util.AttributeSet;
Import android.view.MotionEvent;
Import Android.view.View;
public class Ninepointlineview extends View {
Paint linepaint = new Paint ();
Paint whitelinepaint = new Paint ();
Paint textpaint = new Paint ();
Since all two pictures are squares, it's OK to get a length.
Bitmap Defaultbitmap = Bitmapfactory.decoderesource (Getresources (),
R.drawable.lock);
int defaultbitmapradius = Defaultbitmap.getwidth ()/2;
Initializes the diameter, radius of the selected picture
Bitmap Selectedbitmap = Bitmapfactory.decoderesource (Getresources (),
R.drawable.indicator_lock_area);
int selectedbitmapdiameter = Selectedbitmap.getwidth ();
int Selectedbitmapradius = SELECTEDBITMAPDIAMETER/2;
Array with 9 points defined
Pointinfo[] points = new pointinfo[9];
The point of the corresponding Action_down
Pointinfo startpoint = null;
The width of the screen is high
int width, height;
X,y coordinates obtained when action_move
int MoveX, Movey;
Whether the occurrence of action_up
Boolean isUp = false;
End-generated user lock sequence
StringBuffer lockstring = new StringBuffer ();
Public Ninepointlineview {
Super (context);
This.setbackgroundcolor (Color.White);
Initpaint ();
}
Public Ninepointlineview (context context, AttributeSet Attrs) {
Super (context, attrs);
This.setbackgroundcolor (Color.White);
Initpaint ();
}
@Override
protected void onmeasure (int widthmeasurespec, int heightmeasurespec) {
MLOG.I ("Onmeasure");
Initialize screen size
width = getwidth ();
Height = getheight ();
if (width!= 0 && height!= 0) {
Initpoints (points);
}
MLOG.I ("width, height =" + width + "," + height);
Super.onmeasure (Widthmeasurespec, Heightmeasurespec);
}
@Override
protected void OnLayout (Boolean changed, int left, int. top, int right,
int bottom) {
MLOG.I ("OnLayout");
Super.onlayout (changed, left, top, right, bottom);
}
private int startx = 0, starty = 0;
@Override
protected void OnDraw (Canvas Canvas) {
Canvas.drawtext ("User's sliding Order:" + lockstring, 0, Textpaint);
if (MoveX!= 0 && movey!= 0 && startx!= 0 && starty!= 0) {
Draw the currently active segment
DrawLine (Canvas, StartX, Starty, MoveX, Movey);
}
Drawninepoint (canvas);
Super.ondraw (canvas);
}
Remember, this down and move and up is a pair, and if you don't release from up, you won't get down again;
When you get down, be sure to consume the event, or move and up will not be received by this view's ontouchevent.
@Override
public boolean ontouchevent (Motionevent event) {
Boolean flag = true;
if (isUp) {//If it is finished, reset the properties and lockstring of each point
Finishdraw ();
When up, to return false, the event is released to the system, otherwise the down event cannot be obtained
Flag = false;
else {//not finished sliding, continue drawing
Handlingevent (event);
To return true here, this event is consumed on behalf of the view, otherwise no move and up events are received
Flag = true;
}
return flag;
}
private void Handlingevent (Motionevent event) {
Switch (event.getaction ()) {
Case Motionevent.action_move:
MoveX = (int) event.getx ();
Movey = (int) event.gety ();
MLOG.I ("OnMove:" + MoveX + "," + Movey);
for (Pointinfo temp:points) {
if (Temp.isinmyplace (MoveX, Movey) && temp.isnotselected ()) {
Temp.setselected (TRUE);
StartX = Temp.getcenterx ();
Starty = Temp.getcentery ();
int len = Lockstring.length ();
if (len!= 0) {
int Preid = Lockstring.charat (len-1)-48;
Points[preid].setnextid (Temp.getid ());
}
Lockstring.append (Temp.getid ());
Break
}
}
Invalidate (0, height-width, width, height);
Break
Case Motionevent.action_down:
int downx = (int) event.getx ();
int downY = (int) event.gety ();
MLOG.I ("Ondown:" + Downx + "," + DownY);
for (Pointinfo temp:points) {
if (Temp.isinmyplace (Downx, DownY)) {
Temp.setselected (TRUE);
StartPoint = temp;
StartX = Temp.getcenterx ();
Starty = Temp.getcentery ();
Lockstring.append (Temp.getid ());
Break
}
}
Invalidate (0, height-width, width, height);
Break
Case MOTIONEVENT.ACTION_UP:
MLOG.I ("Onup");
StartX = Starty = MoveX = Movey = 0;
IsUp = true;
Invalidate ();
Break
Default
MLOG.I ("Receive other events!!") ");
Break
}
}
private void Finishdraw () {
for (Pointinfo temp:points) {
Temp.setselected (FALSE);
Temp.setnextid (Temp.getid ());
}
Lockstring.delete (0, Lockstring.length ());
IsUp = false;
Invalidate ();
}
private void Initpoints (pointinfo[] points) {
int len = points.length;
int seletedspacing = (Width-selectedbitmapdiameter * 3)/4;
Displays the upper-left coordinate of the picture when selected
int seletedx = seletedspacing;
int seletedy = height-width + seletedspacing;
Top left corner coordinates of picture when not selected
int defaultx = Seletedx + Selectedbitmapradius-defaultbitmapradius;
int defaulty = seletedy + Selectedbitmapradius-defaultbitmapradius;
Draw each point well
for (int i = 0; i < len; i++) {
if (i = = 3 | | | i = = 6) {
Seletedx = seletedspacing;
Seletedy + + Selectedbitmapdiameter + seletedspacing;
Defaultx = Seletedx + Selectedbitmapradius
-Defaultbitmapradius;
Defaulty + + Selectedbitmapdiameter + seletedspacing;
}
Points[i] = new Pointinfo (i, Defaultx, Defaulty, Seletedx, seletedy);
Seletedx + + Selectedbitmapdiameter + seletedspacing;
Defaultx + + Selectedbitmapdiameter + seletedspacing;
}
}
private void Initpaint () {
Initlinepaint (Linepaint);
Inittextpaint (Textpaint);
Initwhitelinepaint (Whitelinepaint);
}
/**
* Initialize the text brush
* @param paint
*/
private void Inittextpaint (Paint Paint) {
Textpaint.settextsize (30);
Textpaint.setantialias (TRUE);
Textpaint.settypeface (Typeface.monospace);
}
/**
* Initialize the black line brush
*
* @param paint
*/
private void Initlinepaint (Paint Paint) {
Paint.setcolor (Color.gray);
Paint.setstrokewidth (Defaultbitmap.getwidth ());
Paint.setantialias (TRUE);
Paint.setstrokecap (Cap.round);
}
/**
* Initialize white line brush
*
* @param paint
*/
private void Initwhitelinepaint (Paint Paint) {
Paint.setcolor (Color.White);
Paint.setstrokewidth (Defaultbitmap.getwidth ()-5);
Paint.setantialias (TRUE);
Paint.setstrokecap (Cap.round);
}
/**
* Draw the completed part
*
* @param canvas
*/
private void Drawninepoint (Canvas Canvas) {
if (startpoint!= null) {
Draweachline (canvas, startpoint);
}
Draw a picture of each point
for (Pointinfo pointinfo:points) {
if (pointinfo.isselected ()) {//Draw large Circle
Canvas.drawbitmap (Selectedbitmap, Pointinfo.getseletedx (),
Pointinfo.getseletedy (), NULL);
}
Draw Point
Canvas.drawbitmap (Defaultbitmap, Pointinfo.getdefaultx (),
Pointinfo.getdefaulty (), NULL);
}
}
/**
* Recursively draws line segments between each two points
*
* @param canvas
* @param point
*/
private void Draweachline (Canvas Canvas, Pointinfo point) {
if (Point.hasnextid ()) {
int n = point.getnextid ();
DrawLine (Canvas, Point.getcenterx (), Point.getcentery (),
Points[n].getcenterx (), Points[n].getcentery ());
Recursion
Draweachline (canvas, points[n]);
}
}
/**
* Draw black lines first, then draw white line above, to achieve the effect of white line
*
* @param canvas
* @param startx
* @param starty
* @param stopx
* @param stopy
*/
private void DrawLine (Canvas Canvas, float startx, float starty,
Float stopx, float stopy) {
Canvas.drawline (StartX, Starty, Stopx, Stopy, Linepaint);
Canvas.drawline (StartX, Starty, Stopx, Stopy, Whitelinepaint);
}
/**
* Used to indicate a point
*
* @author ZKWLX
*
*/
Private class Pointinfo {
The ID of a point
private int id;
ID of the next point to which the current point is pointing, when there is no time for its own ID
private int NextID;
is selected
Private Boolean selected;
The top left corner of the picture at the default X coordinate
private int defaultx;
The top-left corner of the picture at the default y-coordinate
private int defaulty;
When selected, the top left corner of the picture is X coordinate
private int seletedx;
The y-coordinate of the top left corner of the picture when selected
private int seletedy;
public pointinfo (int id, int defaultx, int defaulty, int seletedx,
int seletedy) {
This.id = ID;
This.nextid = ID;
This.defaultx = Defaultx;
This.defaulty = defaulty;
This.seletedx = Seletedx;
This.seletedy = Seletedy;
}
public boolean isselected () {
return selected;
}
public Boolean isnotselected () {
return!isselected ();
}
public void SetSelected (Boolean selected) {
this.selected = selected;
}
public int getId () {
return ID;
}
public int Getdefaultx () {
return DEFAULTX;
}
public int getdefaulty () {
return defaulty;
}
public int Getseletedx () {
return seletedx;
}
public int Getseletedy () {
return seletedy;
}
public int Getcenterx () {
return Seletedx + Selectedbitmapradius;
}
public int getcentery () {
return seletedy + Selectedbitmapradius;
}
public Boolean Hasnextid () {
return NextID!= ID;
}
public int Getnextid () {
return NextID;
}
public void Setnextid (int nextid) {
This.nextid = NextID;
}
/**
* Coordinates (X,Y) is within the range of the current point
*
* @param x
* @param y
* @return
*/
public boolean isinmyplace (int x, int y) {
Boolean InX = x > Seletedx
&& x < (Seletedx + selectedbitmapdiameter);
Boolean iny = y > Seletedy
&& y < (seletedy + selectedbitmapdiameter);
Return (InX && iny);
}
}
}

Ninepointview.java
Copy Code code as follows:

Package Org.demo.custon_view;
Import Org.demo.utils.MLog;
Import Android.content.Context;
Import Android.graphics.Bitmap;
Import Android.graphics.BitmapFactory;
Import Android.graphics.Canvas;
Import Android.graphics.Color;
Import Android.graphics.Matrix;
Import Android.graphics.Paint;
Import Android.graphics.Paint.Cap;
Import Android.graphics.Path;
Import Android.graphics.Typeface;
Import Android.util.AttributeSet;
Import android.view.MotionEvent;
Import Android.view.View;
public class Ninepointview extends View {
Paint linepaint = new Paint ();
Paint textpaint = new Paint ();
Path PATH = new Path ();
Since all two pictures are squares, it's OK to get a length.
Bitmap Defaultbitmap = Bitmapfactory.decoderesource (Getresources (),
R.drawable.lock);
int defaultbitmapradius = Defaultbitmap.getwidth ()/2;
Initializes the diameter, radius of the selected picture
Bitmap Selectedbitmap = Bitmapfactory.decoderesource (Getresources (),
R.drawable.indicator_lock_area);
int selectedbitmapdiameter = Selectedbitmap.getwidth ();
int Selectedbitmapradius = SELECTEDBITMAPDIAMETER/2;
Picture of the initialization indicator
Bitmap Indicatebitmap = Bitmapfactory.decoderesource (Getresources (),
R.drawable.indicator_lock_area_next);
Bitmap tempbitmap = null;
Array with 9 points defined
Pointinfo[] points = new pointinfo[9];
The width of the screen is high
int width, height;
X,y coordinates obtained when action_move
int MoveX, Movey;
Whether the occurrence of action_up
Boolean isUp = false;
End-generated user lock sequence
StringBuffer lockstring = new StringBuffer ();
Matrix matrix = new Matrix ();
Public Ninepointview {
Super (context);
This.setbackgroundcolor (Color.White);
Initlinepaint (Linepaint);
Inittextpaint (Textpaint);
}
Public Ninepointview (context context, AttributeSet Attrs) {
Super (context, attrs);
This.setbackgroundcolor (Color.White);
Initlinepaint (Linepaint);
Inittextpaint (Textpaint);
}
@Override
protected void onmeasure (int widthmeasurespec, int heightmeasurespec) {
MLOG.I ("Onmeasure");
width = getwidth ();
Height = getheight ();
if (width!= 0 && height!= 0) {
Initpoints (points);
}
MLOG.I ("width, height =" + width + "," + height);
Super.onmeasure (Widthmeasurespec, Heightmeasurespec);
}
@Override
protected void OnLayout (Boolean changed, int left, int. top, int right,
int bottom) {
MLOG.I ("OnLayout");
Super.onlayout (changed, left, top, right, bottom);
}
private int startx = 0, starty = 0;
@Override
protected void OnDraw (Canvas Canvas) {
Canvas.drawtext ("User's sliding Order:" + lockstring, 0, Textpaint);
if (MoveX!= 0 && movey!= 0 && startx!= 0 && starty!= 0) {
Draw the currently active segment
Canvas.drawline (StartX, Starty, MoveX, Movey, Linepaint);
}
Drawninepoint (canvas, linepaint);
Super.ondraw (canvas);
}
Remember, this down and move and up is a pair, and if you don't release from up, you won't get down again;
When you get down, be sure to consume the event, or move and up will not be received by this view's ontouchevent.
@Override
public boolean ontouchevent (Motionevent event) {
Boolean flag = true;
if (isUp) {//If it is finished, reset the entire canvas
Finishdraw ();
When up, to return false, the event is released to the system, otherwise the down event cannot be obtained
Flag = false;
else {//not finished sliding, continue drawing
Handlingevent (event);
Returns true here, otherwise the view does not consume this event and is handed to the system, and no more move and up events are received
Flag = true;
}
return flag;
}
private void Handlingevent (Motionevent event) {
Switch (event.getaction ()) {
Case Motionevent.action_move:
MoveX = (int) event.getx ();
Movey = (int) event.gety ();
MLOG.I ("OnMove:" + MoveX + "," + Movey);
for (Pointinfo temp:points) {
if (Temp.isinmyplace (MoveX, Movey) && temp.isnotselected ()) {
Temp.setselected (TRUE);
StartX = Temp.getcenterx ();
Starty = Temp.getcentery ();
int len = Lockstring.length ();
if (len!= 0) {
int Preid = Lockstring.charat (len-1)-48;
Points[preid].setnextid (Temp.getid ());
}
Lockstring.append (Temp.getid ());
Break
}
}
Invalidate (0, height-width, width, height);
Break
Case Motionevent.action_down:
int downx = (int) event.getx ();
int downY = (int) event.gety ();
MLOG.I ("Ondown:" + Downx + "," + DownY);
for (Pointinfo temp:points) {
if (Temp.isinmyplace (Downx, DownY)) {
Temp.setselected (TRUE);
StartX = Temp.getcenterx ();
Starty = Temp.getcentery ();
Lockstring.append (Temp.getid ());
Break
}
}
Invalidate (0, height-width, width, height);
Break
Case MOTIONEVENT.ACTION_UP:
MLOG.I ("Onup");
StartX = Starty = MoveX = Movey = 0;
IsUp = true;
Invalidate ();
Break
Default
MLOG.I ("Receive other events!!") ");
Break
}
}
private void Finishdraw () {
for (Pointinfo temp:points) {
Temp.setselected (FALSE);
Temp.setnextid (Temp.getid ());
}
Lockstring.delete (0, Lockstring.length ());
IsUp = false;
Invalidate ();
}
private void Initpoints (pointinfo[] points) {
int len = points.length;
int seletedspacing = (Width-selectedbitmapdiameter * 3)/4;
Displays the upper-left coordinate of the picture when selected
int seletedx = seletedspacing;
int seletedy = height-width + seletedspacing;
Top left corner coordinates of picture when not selected
int defaultx = Seletedx + Selectedbitmapradius-defaultbitmapradius;
int defaulty = seletedy + Selectedbitmapradius-defaultbitmapradius;
for (int i = 0; i < len; i++) {
if (i = = 3 | | | i = = 6) {
Seletedx = seletedspacing;
Seletedy + + Selectedbitmapdiameter + seletedspacing;
Defaultx = Seletedx + Selectedbitmapradius
-Defaultbitmapradius;
Defaulty + + Selectedbitmapdiameter + seletedspacing;
}
Points[i] = new Pointinfo (i, Defaultx, Defaulty, Seletedx, seletedy);
Seletedx + + Selectedbitmapdiameter + seletedspacing;
Defaultx + + Selectedbitmapdiameter + seletedspacing;
}
}
private void Inittextpaint (Paint Paint) {
Textpaint.settextsize (30);
Textpaint.setantialias (TRUE);
Textpaint.settypeface (Typeface.monospace);
}
/**
* Initialization Line Brush
*
* @param paint
*/
private void Initlinepaint (Paint Paint) {
Paint.setcolor (Color.gray);
Paint.setstrokewidth (Defaultbitmap.getwidth ());
Paint.setantialias (TRUE);
Paint.setstrokecap (Cap.round);
}
/**
* Draw the completed part
*
* @param canvas
*/
private void Drawninepoint (Canvas Canvas, Paint Paint) {
Draw the user's line first.
for (Pointinfo pointinfo:points) {
if (Pointinfo.hasnextid ()) {
int n = pointinfo.getnextid ();
Canvas.drawline (Pointinfo.getcenterx (), Pointinfo.getcentery (),
Points[n].getcenterx (), Points[n].getcentery (), paint);
}
}
Draw a picture of each point
for (Pointinfo pointinfo:points) {
if (pointinfo.isselected ()) {
if (Pointinfo.hasnextid ()) {
Matrix.reset ();
int i = (int) math.abs (math.random () * 1000-640);
MLOG.I ("Random to the angle:" + i);
Matrix.setrotate (i);
Tempbitmap = Bitmap.createbitmap (indicatebitmap, 0, 0,
Indicatebitmap.getwidth (),
Indicatebitmap.getheight (), Matrix, false);
Canvas.drawbitmap (Tempbitmap, Pointinfo.getseletedx (),
Pointinfo.getseletedy (), paint);
} else {
Canvas.drawbitmap (Selectedbitmap, Pointinfo.getseletedx (),
Pointinfo.getseletedy (), paint);
}
}
Canvas.drawbitmap (Defaultbitmap, Pointinfo.getdefaultx (),
Pointinfo.getdefaulty (), paint);
}
}
Private class Pointinfo {
The ID of a point
private int id;
ID of the next point to which the current point is pointing, when there is no time for its own ID
private int NextID;
is selected
Private Boolean selected;
The top left corner of the picture at the default X coordinate
private int defaultx;
The top-left corner of the picture at the default y-coordinate
private int defaulty;
When selected, the top left corner of the picture is X coordinate
private int seletedx;
The y-coordinate of the top left corner of the picture when selected
private int seletedy;
public pointinfo (int id, int defaultx, int defaulty, int seletedx,
int seletedy) {
This.id = ID;
This.nextid = ID;
This.defaultx = Defaultx;
This.defaulty = defaulty;
This.seletedx = Seletedx;
This.seletedy = Seletedy;
}
public boolean isselected () {
return selected;
}
public Boolean isnotselected () {
return!isselected ();
}
public void SetSelected (Boolean selected) {
this.selected = selected;
}
public int getId () {
return ID;
}
public int Getdefaultx () {
return DEFAULTX;
}
public int getdefaulty () {
return defaulty;
}
public int Getseletedx () {
return seletedx;
}
public int Getseletedy () {
return seletedy;
}
public int Getcenterx () {
return Seletedx + Selectedbitmapradius;
}
public int getcentery () {
return seletedy + Selectedbitmapradius;
}
public Boolean Hasnextid () {
return NextID!= ID;
}
public int Getnextid () {
return NextID;
}
public void Setnextid (int nextid) {
This.nextid = NextID;
}
/**
* Coordinates (X,Y) is within the range of the current point
*
* @param x
* @param y
* @return
*/
public boolean isinmyplace (int x, int y) {
Boolean InX = x > Seletedx
&& x < (Seletedx + selectedbitmapdiameter);
Boolean iny = y > Seletedy
&& y < (seletedy + selectedbitmapdiameter);
if (InX && iny) {
return true;
} else {
return false;
}
}
}
}

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.