I am using this code to get my screen width and height and send my button to a random position on the screen but sometimes my button is completely off-screen. I don't believe that the resolution my code gets me is the true res. Any help would be appreciated.
DisplayMetrics displayMetrics = new DisplayMetrics();
getWindowManager().getDefaultDisplay().getMetrics(displayMetrics);
float height = displayMetrics.heightPixels;
float width = displayMetrics.widthPixels;
float maxX = width;
float maxY = height;
float minX = width/10;
float minY = height/15;
Random rand = new Random();
float finalX = rand.nextFloat() * abs(maxX - minX);
float finalY = rand.nextFloat() * abs(maxY - minY);
_height.setText("Current Y: " + String.valueOf(finalY));
_width.setText("Current X: " + String.valueOf(finalX));
max_height.setText("Max height: " + String.valueOf(height));
max_width.setText("Max width: " + String.valueOf(width));
btnClick.setX(finalX);
btnClick.setY(finalY);
btnClick.setVisibility(View.VISIBLE);
Here are 2 pictures displaying my problem. Button slightly on-screen Button completely off-screen
Aucun commentaire:
Enregistrer un commentaire