java - Coordinates not corresponding -


i'm pretty new android , i'm struggling trying understand going on in application. i'm trying build small game in circle appears on screen, in random position , disappears user clicks on it. new circle appears in position.

the problem comparing coordinates of user's click, , coordinates of center of circle, these seem totally different…
problem seems in circle coordinates, because if try force position center of view, appears in totally wrong position, @ right bottom of view.

what doing wrong?

this code of function drawing circles in view (which big parent).

public void drawdots(){     paint paint = new paint();     paint.setcolor(color.magenta);     bitmap bg = bitmap.createbitmap(480, 800, bitmap.config.argb_8888);      view ll = (view) findviewbyid(r.id.circle);     ll.setbackground(new bitmapdrawable(bg));      centerx = rand.nextint(ll.getwidth());     centery = rand.nextint(ll.getheight());      canvas canvas = new canvas(bg);     canvas.drawcircle(centerx, centery, radius, paint);     log.i("point center x :" + centerx, " y " + centery);      ll.setontouchlistener(new view.ontouchlistener() {         int x = 0, y = 0;         public boolean ontouch(view v, motionevent event) {              if (event.getaction() == motionevent.action_down) {                 x = (int) event.getx();                 y = (int) event.gety();                 log.i("click x: " + x, "click y: " + y);                  if((x < (centerx + radius) && x > (centerx - radius))) && ((y > centery + radius) && (y < centery - radius)) ){                     log.i("x ok: " + x + " y  ok: " + y, "counter: " + counter);                     drawdots();                  }else{                     log.i("x not ok " + x,  " circle area between: " + (centerx-radius) + " e " + (centerx + radius));                     log.i("y not ok " + y,  " circle area between: " + (centery-radius) + " e " + (centery + radius));                 }             }             return true;         }     }); } 

and layout. textview , button needed start timer removed layout user clicks on start button.

<relativelayout xmlns:android="http://schemas.android.com/apk/res/android"     xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent"     android:layout_height="match_parent" android:paddingleft="@dimen/activity_horizontal_margin"     android:paddingright="@dimen/activity_horizontal_margin"     android:paddingtop="@dimen/activity_vertical_margin"     android:paddingbottom="@dimen/activity_vertical_margin" tools:context=".mainactivity">  <textview     android:id="@+id/timervalue"     android:layout_width="wrap_content"     android:layout_height="wrap_content"     android:layout_centerhorizontal="true"     android:layout_marginbottom="37dp"     android:textsize="40sp"     android:textcolor="#ffffff"     android:text="@string/timerval" />  <button     android:id="@+id/startbutton"     android:layout_width="90dp"     android:layout_height="45dp"     android:layout_alignparentleft="true"     android:layout_centervertical="true"     android:layout_marginleft="38dp"     android:text="@string/startbuttonlabel" />  <view android:layout_width="fill_parent"     android:layout_height="fill_parent"     android:id="@+id/circle"/> 

sorry bad explanation dunno problem is.

edit tried solution , seems improve situation if consider x-axis, y-axis there correspondence in way seems coordinates of click totally out of range... circle drawn on canvas doesn't match screen

even though don't understand why needed

the problem here:

centerx = rand.nextint(ll.getwidth());

ll.getwidth() returns 0 , rand.nextint() won't work when ll.getwidth() 0.

so cannot call nextint(0). check out random#nextint().


to fix make check:

if(ll.getwidth() == 0)    centerx = 0; else    centerx = rand.nextint(ll.getwidth()); 

but i'm not doing find better solution.

p.s - same have other case, still doubt need of rand.nextint(ll.getwidth());. (you may edit question , make clear want achieve)

as newbie code nice enough gain upvotes :)

~happycoding


Comments

Popular posts from this blog

c++ - Difference between pre and post decrement in recursive function argument -

php - Nothing but 'run(); ' when browsing to my local project, how do I fix this? -

php - How can I echo out this array? -