get Best Image Size - Android Graphics

Android examples for Graphics:Image Size

Description

get Best Image Size

Demo Code


//package com.java2s;
import android.content.Context;
import android.content.res.Configuration;
import android.os.Build;
import android.util.DisplayMetrics;

public class Main {
    private static final int SMALLEST_IMAGE_SIZE = 480;

    public static int getBestImageSize(Context context) {

        if ((Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB_MR2)
                && context != null) {
            Configuration config = context.getResources()
                    .getConfiguration();
            DisplayMetrics dm = context.getResources().getDisplayMetrics();

            final float screenWidthInPixels;
            if (config.orientation == Configuration.ORIENTATION_PORTRAIT) {
                screenWidthInPixels = config.screenWidthDp * dm.density;
            } else {
                screenWidthInPixels = config.screenHeightDp * dm.density;
            }/*from   ww  w. j  a v  a2  s  .  c  o  m*/

            if (screenWidthInPixels <= SMALLEST_IMAGE_SIZE)
                return SMALLEST_IMAGE_SIZE;
            else if (screenWidthInPixels <= 600)
                return 640;
            else if (screenWidthInPixels < 1000)
                return 720;
            else if (screenWidthInPixels >= 1200)
                return 1440;
            else
                return 1080;
        } else {
            // On old android versions, a generic, small screen resolution is assumed.
            return SMALLEST_IMAGE_SIZE;
        }
    }
}

Related Tutorials