Search code examples
androidandroid-resolution

How to compare the resolutions with some other resolutions


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.


Solution

  • 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