Package Com.example.view;
Import COM.EXAMPLE.IMAGELOCK.R;
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;
Import Android.widget.Toast;
public class Ninepointlineview extends View {/** * defines 3 Paint, and a coordinate dot picture/Paint linepaint = new Paint ();
Paint whitelinepaint = new Paint ();
Paint textpaint = new Paint ();
Bitmap Defaultbitmap = Bitmapfactory.decoderesource (Getresources (), r.drawable.lock);
Pointinfo[] points = new pointinfo[9];
int width, height;
The length of the radius of the punctuation int defaultbitmapradius = Defaultbitmap.getwidth ()/2; The diameter of the origin when the password is drawn, the radius Bitmap selectedbitmap = Bitmapfactory.decoderesource (Getresources (), r.drAwable.indicator_lock_area);
int selectedbitmapdiameter = Selectedbitmap.getwidth ();
int Selectedbitmapradius = SELECTEDBITMAPDIAMETER/2;
StringBuffer lockstring = new StringBuffer ();
Context context;
/** constructor *********************************************/Public Ninepointlineview (context) {super (context);
This.context = context;
This.setbackgroundcolor (Color.White);
Initpaint ();
Public Ninepointlineview (context, AttributeSet attrs) {Super (context, attrs);
This.context = context;
This.setbackgroundcolor (Color.White);
Initpaint ();
private void Initpaint () {//Line-Wrap 9 Origin Linepaint.setcolor (color.red);
Linepaint.setstrokewidth (Defaultbitmap.getwidth ());
Linepaint.setantialias (TRUE);
Linepaint.setstrokecap (Cap.round);
In-line--5 whitelinepaint.setcolor (Color.green) less than the original point diameter;
Whitelinepaint.setstrokewidth (Defaultbitmap.getwidth ()-5); Whitelinepaint.setaNtialias (TRUE);
Whitelinepaint.setstrokecap (Cap.round);
Font settings textpaint.settextsize (30);
Textpaint.setantialias (TRUE);
Textpaint.settypeface (Typeface.monospace); /********************************************************** * Measuring * * * @Override protected void Onmeasur
E (int widthmeasurespec, int heightmeasurespec) {width = getwidth ();
Height = getheight ();
if (width!= 0 && height!= 0) {initpoints (points);
} super.onmeasure (Widthmeasurespec, Heightmeasurespec);
}/** * Initialize origin/private void initpoints (pointinfo[] points) {int len = points.length;
2 The spacing of the origin int seletedspacing = (Width-selectedbitmapdiameter * 3)/4;
The 1th origin coordinates int seletedx = seletedspacing;
int seletedy = height-width + seletedspacing;
The 1th origin inside the small circle coordinates int defaultx = Seletedx + Selectedbitmapradius-defaultbitmapradius; int defaulty = Seletedy + selectedbitmApradius-defaultbitmapradius;
for (int i = 0; i < len; i++) {//4th, 7 origin if (i = = 3 | | i = = 6) {Seletedx = seletedspacing;
The first Origin y-coordinate + diameter + 2-point distance seletedy + + = Selectedbitmapdiameter + seletedspacing;
Defaultx = Seletedx + Selectedbitmapradius-defaultbitmapradius;
The first Origin y-coordinate + diameter + 2-point distance defaulty + + = Selectedbitmapdiameter + seletedspacing;
} Points[i] = new Pointinfo (i, Defaultx, Defaulty, Seletedx, seletedy);
The origin coordinates xy is the diameter + 2 point distance Seletedx + = Selectedbitmapdiameter + seletedspacing;
Defaultx + + Selectedbitmapdiameter + seletedspacing; }/*****************************************************************/@Override protected void onlayout (bool
EAN changed, int left, int. top, int right, int bottom) {super.onlayout (changed, left, top, right, bottom);
} private int startx = 0, starty = 0;
Pointinfo startpoint = null; @OverRide protected void OnDraw (Canvas Canvas) {drawninepoint (Canvas);
Super.ondraw (canvas);
}/** * * @param canvas/private void drawninepoint (canvas canvas) {if (startpoint!= null) {
Draweachline (canvas, startpoint); for (Pointinfo pointinfo:points) {if (pointinfo!=null) {if (pointinfo.isselected ())
{Canvas.drawbitmap (Selectedbitmap, Pointinfo.getseletedx (), pointinfo.getseletedy (), NULL);
} canvas.drawbitmap (Defaultbitmap, Pointinfo.getdefaultx (), pointinfo.getdefaulty (), NULL); }} 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 ());
Draweachline (canvas, points[n]); } 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);
}/** * ******************************************************************** * * Boolean isUp = false;
int MoveX, Movey;
@Override public boolean ontouchevent (Motionevent event) {Boolean flag = true;
IsUp the default is to false--whether the drawing is finished, this time is true if (isUp) {Finishdraw ();
Toast.maketext (Context, "drawing complete, finger left, in click", 0). Show ();
Flag = false;
else {handlingevent (event);
Flag = true;
Toast.maketext (context, "finger once Drawn", 0). Show ();
}//whether to handle the event return flag;
private void Finishdraw () {for (Pointinfo temp:points) {temp.setselected (false);
Temp.setnextid (Temp.getid ());
Lockstring.delete (0, Lockstring.length ());
IsUp = false;
Invalidate (); } private void Handlingevent (MotionevEnt event) {switch (event.getaction ()) {Case MotionEvent.ACTION_MOVE:moveX = (int) event.getx ();
Movey = (int) event.gety ();
for (Pointinfo temp:points) {if (Temp.isinmyplace (MoveX, Movey) && temp.isselected () ==false) {
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 ();
Break
Case Motionevent.action_down://Get a pressed xy coordinate int downx = (int) event.getx ();
int downY = (int) event.gety ();
for (Pointinfo temp:points) {///if fit within range if (Temp.isinmyplace (Downx, DownY)) {//Set it to checked state
Temp.setselected (TRUE); Sets the selected dot to the starting point dot startpoint = temp;
The center of the circle as the starting point startx = Temp.getcenterx ();
Starty = Temp.getcentery ();
Lockstring.append (Temp.getid ());
Break
} invalidate ();
Break
Case MotionEvent.ACTION_UP:startX = Starty = MoveX = Movey = 0;
Finished drawing isUp = true;
Invalidate ();
Break
Default:break;
}/** * Origin bean/Private class Pointinfo {private Boolean selected;
private int id;
private int NextID;
private int defaultx;
private int defaulty;
private int seletedx;
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 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;
The public void Setnextid (int nextid) {this.nextid = NextID; /** * If an XY value is in the left and right range of a certain origin, it means OK/public boolean isinmyplace (int x, int y) {Boolean x = x > Seletedx
&& x < (Seletedx + selectedbitmapdiameter);
Boolean iny = y > Seletedy && y < (seletedy + selectedbitmapdiameter);
Return (InX && iny); }
}
}
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.