Note that there are some explanatory texts on larger screens.

plurals
  1. POCan i use motionEvent .getRawY() with View . getTop()?
    primarykey
    data
    text
    <p>Im making a program about the game Go and im trying to write some code to allow a person to place a bead, which really just shows an invisible bead. But after an hour of going back and forth with it i just cant seem to figure out why the Y coordinate of my motionevent always seems 2 rows down from where it needs to be.</p> <p>My code for filling my array</p> <pre><code>for(int y=0; y&lt; 9;y++) { for(int x=0;x&lt;9;x++) { String name="Whitebead" + ((9*y)+x+2); //The +2 is because of a naming problem WhitePieces[x][y]=(ImageView) findViewById(getResources().getIdentifier(name, "id", getPackageName())); } } </code></pre> <p>My code for handling motion events</p> <pre><code> @Override public boolean onTouchEvent(MotionEvent e) { if(e.getAction() == MotionEvent.ACTION_DOWN) { float X=e.getRawX(); float Y=e.getRawY(); if(X&gt;Board.getLeft() &amp;&amp; X&lt;Board.getRight()) if(Y&lt; Board.getTop() &amp;&amp; Y&gt;Board.getBottom()); player1.placepiece(X, Y); } return super.onTouchEvent(e); } </code></pre> <p>And finally my code that resolves which bead to what coordinate</p> <pre><code> public void placepiece(float X, float Y) { int[] pieceindex=resolvePiece(X,Y); pieces[pieceindex[0]][pieceindex[1]].setVisibility(ImageView.VISIBLE); } private int[] resolvePiece(float x, float y) { int Xindex=0; int[] oldcoords= new int[2]; //cordinates are like so {xCoord, Ycoord} int[] newcoords= new int[2]; oldcoords[0]=pieces[0][0].getLeft(); //set the oldcoordinates to the first index oldcoords[1]=pieces[0][0].getTop(); for(int i=1; i&lt;9;i++) //go through the 9 indexs to find the closest X value { newcoords[0]=pieces[i][0].getLeft(); newcoords[1]=pieces[i][0].getTop(); if(Math.abs((int)x-newcoords[0])&lt;Math.abs((int)x-oldcoords[0])) { Xindex=i; oldcoords[0]=newcoords[0]; } } int Yindex=0; oldcoords[0]=pieces[0][0].getLeft(); //Reset oldcoords again oldcoords[1]=pieces[0][0].getTop(); for(int n=1; n&lt;9;n++) //Go through the 9 indexes for the closest Y value { newcoords[0]=pieces[0][n].getLeft(); newcoords[1]=pieces[0][n].getTop(); if(Math.abs((int)y-newcoords[1])&lt;Math.abs((int)y-oldcoords[1])) { Yindex=n; oldcoords[1]=newcoords[1]; } } int[] rInt= new int[]{Xindex,Yindex}; return rInt; } </code></pre> <p>//////EDIT: Fixed I figured it out, at the top of the android window is about and inch of space where the title and battery life and stuff go, when you get motion coordinates it takes in the whole screen, where .getTop() only gets from where linear layout Starts. SO instead of using .getTop or.getLeft i used .getLocationInWindow(oldcoord[]) and it places the info i needed into my oldcoord array.</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload