Custom View Implementation Gobang
With the diagram:
Code:
Package Com.example.fiveson;import Java.util.linkedlist;import Java.util.list;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;public class FiveSon extends view {Public Fiveson (context context) {super (context);//TODO auto-generated Constructor Stubinit (context, NULL);} Public Fiveson (context context, AttributeSet Attrs) {Super (context, attrs);//TODO auto-generated constructor Stubinit ( context, attrs);} Public Fiveson (context context, AttributeSet attrs, int defstyleattr) {Super (context, attrs, defstyleattr);//TODO Auto-g Enerated constructor Stubinit (context, attrs);} Private Paint paint = new paint ();p rivate paint circlepaint = new paint ();//flag: Information represents the first few rows of the row? Private list<int[]> chesses = new linkedlist<int[]> ();p rivate void init (context context, AttributeSet attrs) {Chesses.add (new int[]{3,5}); Chesses.add (New int[]{3,6});} int spacing = n, @Overrideprotected void OnDraw (canvas canvas) {//TODO auto-generated method Stubsuper.ondraw (canvas); NT width = getwidth (); int height = getheight ();//1 plot X coordinate int num = Width/spacing;paint.setcolor (color.black); for (int i= 0;i<=num;i++) {canvas.drawline (spacing*i, 0, spacing*i, height, paint);} num = height/spacing;for (int i=0;i<=num;i++) {canvas.drawline (0,spacing*i, width,spacing*i, paint);} 2 draw the y-coordinate//3 draw the flag if (!chesses.isempty ()) {int count = Chesses.size (); for (int i=0;i<count;i++) {//column information int[] location = Chesses.get (i); int rawx = location[1] * Spacing;int Rawy = location[0] * SPACING;IF (i%2==0) {Circlepaint.setcolor ( Color.Black);} else {circlepaint.setcolor (color.green);} Canvas.drawcircle (RAWX, Rawy, Circlepaint);}}} /** * Fully Customizable space: * * deal with your evil not processing time love you way: * Ontouchevent () * onkeyevent () * * * * * * * * * * Setontouchlistener () * Setonkeylisten ER (); * * External operation: * Setontouchlistener (Ontouchlistener L) * Setonkeylistener (onkeylistener L) * * * * * *@Overridepublic boolean ontouchevent (Motionevent event) {Boolean iscomsumed = False;int action = event.getaction (); if ( Action = = Motionevent.action_down) {//parse the clicked coordinates to determine the number of rows and columns of the click int ex = (int) event.getx (); int ey = (int) event.gety (); Int col Num = (int) (ex/spacing), int rowNum = (int) (ey/spacing), int colcc = ex% spacing;int ROWCC = ey% spacing;if (colcc> ; = (SPACING/2)) {colcc++;} if (rowcc>= (SPACING/2)) {rowcc++;} Determine when add int len = Chesses.size (), if (len>0) {Boolean has = false;for (int i = len-1;i>=0;i--) {int[] chs = CHESSES.G ET (i); if (RowNum = = Chs[0] && colnum = = Chs[1]) {has = True;break;}} if (!has) {Chesses.add (new int[]{rownum,colnum}); invalidate ();}} else {chesses.add (new int[]{rownum,colnum}); invalidate ();} Iscomsumed = true;} else {iscomsumed = Super.ontouchevent (event);} Return Super.ontouchevent (event);}}
Android Development--------Custom View picture Gobang