-1

I just began to develop a app with java, and I only got some experience in C. In my code in Activity.java (in android studio) I got things like, just to give some examples:

    meteorite1.setX(meteoritePlacementX(meteorite1.getX()));
    meteorite1.setY(-2000);
    gnome.setX(330);
    gnome.setY(800);
    meteorite2.setX(meteoritePlacementX(meteorite2.getX()));
    meteorite2.setY(meteoritePlacementY(meteorite1.getY()));
    meteorite3.setX(meteoritePlacementX(meteorite3.getX()));
    meteorite3.setY(meteoritePlacementY(meteorite2.getY()));
    meteorite4.setX(meteoritePlacementX(meteorite4.getX()));
    meteorite4.setY(meteoritePlacementY(meteorite3.getY()));
    meteorite5.setX(meteoritePlacementX(meteorite5.getX()));
    meteorite5.setY(meteoritePlacementY(meteorite4.getY()));

    meteoritedestruction1.setX(0);
    meteoritedestruction1.setY(-2000);
    meteoritedestruction2.setX(0);
    meteoritedestruction2.setY(-2000);
    meteoritedestruction3.setX(0);
    meteoritedestruction3.setY(-2000);
    meteoritedestruction4.setX(0);
    meteoritedestruction4.setY(-2000);
    meteoritedestruction5.setX(0);
    meteoritedestruction5.setY(-2000);

    star1.setX(300);
    star2.setX(150);
    star3.setX(50);
    star4.setX(500);
    star5.setX(600);
    star6.setX(350);
    star7.setX(80);
    star8.setX(450);
    tinystar1.setX(302);
    tinystar2.setX(240);
    tinystar3.setX(57);
    tinystar4.setX(660);
    tinystar5.setX(400);

    star1.setY(300);
    star2.setY(-300);
    star3.setY(-100);
    star4.setY(100);
    star5.setY(300);
    star6.setY(500);
    star7.setY(700);
    star8.setY(900);
    tinystar1.setY(300);
    tinystar2.setY(-400);
    tinystar3.setY(-200);
    tinystar4.setY(150);
    tinystar5.setY(30);    

and

public float meteoritePlacementX(float X){

    float MeteoriteNewX = 0f;

    int random = (int )(Math.random() * 480 - 50);

    MeteoriteNewX = random;

    return MeteoriteNewX;
}

Which workes fine, but just on my phone (720 x 1280 pixels (~294 ppi pixel density)) which I tested my code at. Now I published my app, but on other device, the layout of the app is totally out of sync (which makes sense to me now, cause x and y are different for every screen). Buttons and pictures workes fine, but moving object like

meteorite1.setY(meteorite1.getY() + 20); 

where I use x and y are broken on other devices. I use the relative layout.

So long story short; Is there a way to change x and y, so it becomes relative to the screen? Otherwise I need to change the whole code.

King Rool
  • 17
  • 5

1 Answers1

1

In general using placement based on hard coded pixel values is not a good practice. Not only would this break with backwards compatibility but also think about what you would have to do when 2k+ phones come out, you would need an entire refactor. Look at this question and the answer by Guillaume Perrot you can get get the maximum and minimum pixel values relative to the user's phone and use those instead of the 480 - 50 and your star set functions.

For the movement do

DisplayMetrics displayMetrics = new DisplayMetrics();
WindowManager wm = (WindowManager)getApplicationContext().getSystemService(Context.WINDOW_SERVICE); // the results will be higher than using the activity context object or the getWindowManager() shortcut
wm.getDefaultDisplay().getMetrics(displayMetrics);
int maxWidth = displayMetrics.widthPixels;
//Make this percentage whatever you want
float movementPercentage = 0.02
//Will move the object 2 percent up the y axis 
meteorite1.setY(meteorite1.getY() + maxWidth*movementPercentage); 
Community
  • 1
  • 1
CodeMonkey
  • 268
  • 5
  • 16
  • Yea you are totally right! But if I change the values of x and y, I make it compatibel with another device, but my orginal device can't run the app normally, am I right? How can I replace like meteorite1.setY(meteorite1.getY() + 20); so it becomes relative? With dpi? – King Rool Aug 31 '16 at 20:02
  • @KingRool Updated my answer – CodeMonkey Aug 31 '16 at 20:21