Android Development--------Own definition view picture Gobang

Source: Internet
Author: User


Define your own 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);}}} /** * Completely define your own space: * * deal with your evil not processing time love you way: * Ontouchevent () * onkeyevent () * * * * * * * * Setontouchlistener () * Setonkeyliste NER (); * * 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 infer 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&gt ; = (SPACING/2)) {colcc++;} if (rowcc>= (SPACING/2)) {rowcc++;} Infer 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--------Own definition view picture Gobang

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.