vendredi 27 mars 2015

Android - button random position weird behavior

So I'm trying to appear 3 buttons on a random position. First im giving them a random position based on the screen size using this code:



float x = new Random().nextInt(metrics.widthPixels / 2);
float y = new Random().nextInt(metrics.heightPixels / 2);
lbutton1.setX(x);
lbutton1.setY(y);
float x1 = new Random().nextInt(metrics.widthPixels / 2);
float y1 = new Random().nextInt(metrics.heightPixels / 2);
lbutton2.setX(x1);
lbutton2.setY(y1);
float x2 = new Random().nextInt(metrics.widthPixels / 2);
float y2 = new Random().nextInt(metrics.heightPixels / 2);
lbutton3.setX(x2);
lbutton3.setY(y2);


Buttons at this point are set GONE in .xml file. When I trying to call their method so they become VISIBLE im doing it with using this code:



lbutton1.setVisibility(View.VISIBLE);
lbutton2.setVisibility(View.VISIBLE);
lbutton3.setVisibility(View.VISIBLE);


Problem is that while button1 appears in the position set by setX(x),setY(y), the other 2 buttons X position (from what i can tell) does not take the value of x1,x2 but it takes random values that I cant explain where they are taken from. All buttons Y position is ok.. Any ideas why could this be happening?





Aucun commentaire:

Enregistrer un commentaire