-1

In My app I have to do some resolution specific work and I have to get the resolution and send it to server accordingly. Now My server have images which entertains the following resolutions...

  1. 320 * 480
  2. 480 * 800
  3. 800 * 1200
  4. 720 * 1280
  5. 1080 * 1920
  6. 1440 * 2560

I am getting resolutions in the following way

Display display = getWindowManager().getDefaultDisplay();
Point size = new Point();
display.getSize(size);
int width = size.x;
int height = size.y;

so As we know there is so may resolutions so there would be some devices whose resolutions slightly different from the set of resolutions I have mentioned above such as I have a device which has 800px * 1172px

waht I want

So tell me how can I compare the device resolution with the set of resolution and send the resolution which maintain the resolution categorized images.

also if the device has resolution which is not exist in my pre determined set of resolutions , then I want to send the resolution closer to it.

Please help me , I do not know how to do it.

Allay Khalil
  • 674
  • 3
  • 11
  • 31

1 Answers1

2

800px * 1172px

you are aware that this is actually a 800x1200 display? The way you get the screen dimensions takes the device's on-screen navigation bar into account. See this and similar questions if you need to take the navigation bar into account.

I want to send the resolution closer to it.

Possible solution that uses arrays of your supported dimensions and finds the closest value to your passed width/height:

final int[] supportedHeight = {320, 480, 720, 800, 1080, 1440};
final int[] supportedWidth = {480, 800, 1200, 1280, 1920, 2560};


public static int getClosest(int n, int[] values){
    int dst = Math.abs(values[0] - n);
    int position = 0;
    for(int i = 1; i < values.length; i++){
        int newDst = Math.abs(values[i] - n);
        if(newDst < dst){
            position = i;
            dst = newDst;
        }
    }
    return values[position];
}

 Log.d("h", getClosest(1337, supportedHeight)+""); //1440
 Log.d("w", getClosest(1172, supportedWidth)+"");  //1200
Community
  • 1
  • 1
Droidman
  • 11,485
  • 17
  • 93
  • 141
  • I know some one is going to say this . But I just put it as an example – Allay Khalil Jan 19 '16 at 08:00
  • can you tell me how is this working , and what are the parameters getClosest(int n, int[] values) – Allay Khalil Jan 19 '16 at 08:03
  • I added more appropriate variable names. Basically this code iterates your array and looks for the index of the number which is closest to your passed number. So, add your supported heights and widths to arrays, then use this method passing the values you are getting from the device. This way you'll find the best matching width/height that you are supporting – Droidman Jan 19 '16 at 08:06
  • to keep it simple: `values` is your appropriate widths/heights array, `n` is the value that you get from the device – Droidman Jan 19 '16 at 08:08
  • great, or what if I have the device size as the perfect size which I declared in array , for example 480 * 800 , which is also in the array . so in this case , will it send me this size ? – Allay Khalil Jan 19 '16 at 08:09
  • yes, it will. You can test it with different values and log the results – Droidman Jan 19 '16 at 08:12
  • Just changed the name of arrays height with width and it is working all fine. Thank you – Allay Khalil Jan 19 '16 at 08:22