jeudi 6 août 2015

Random position of textview within bounds

I have a textview that changes positions(to a random position with bounds), as well as font size. I can do this but I am having trouble accounting for the size of the text and the screen density.

I get the bounds here -

public void onWindowFocusChanged (boolean hasFocus) {
    Display display = getWindowManager().getDefaultDisplay();
    size = new Point();
    display.getSize(size);
    screenWidth = size.x-buttonA.getLeft() - (size.x - buttonC.getRight()) - 550;
    screenHeight = size.y - (size.y - buttonA.getTop()) - 400;

}

and then change the position here -

    int posX =  (int)(Math.random()*(screenWidth))+(buttonA.getLeft()/2);
    int posY =  (int)(Math.random()*(screenHeight)) + 50;
    testLabel.setX(posX);
    testLabel.setY(posY);

I have made this for the xhdpi screens when I was testing it and it works fine on those but it goes off screen on other screen densities. But I was having trouble accounting for that. I was also having trouble getting the height and width of the text view once it's changed because its not actually drawn on the screen when i need it to account for the change of position.




Aucun commentaire:

Enregistrer un commentaire