Example usage for android.util DisplayMetrics DisplayMetrics

List of usage examples for android.util DisplayMetrics DisplayMetrics

Introduction

In this page you can find the example usage for android.util DisplayMetrics DisplayMetrics.

Prototype

public DisplayMetrics() 

Source Link

Usage

From source file:Main.java

@TargetApi(Build.VERSION_CODES.JELLY_BEAN_MR1)
public static int getScreenRealH(Context context) {
    int h;/*w w  w.ja v a2s .  c o m*/
    WindowManager winMgr = (WindowManager) context.getSystemService(Context.WINDOW_SERVICE);
    Display display = winMgr.getDefaultDisplay();
    DisplayMetrics dm = new DisplayMetrics();
    if (Build.VERSION.SDK_INT >= 17) {
        display.getRealMetrics(dm);
        h = dm.heightPixels;
    } else {
        try {
            Method method = Class.forName("android.view.Display").getMethod("getRealMetrics",
                    DisplayMetrics.class);
            method.invoke(display, dm);
            h = dm.heightPixels;
        } catch (Exception e) {
            display.getMetrics(dm);
            h = dm.heightPixels;
        }
    }
    return h;
}

From source file:Main.java

public static int getScreenHeight(Context context) {
    WindowManager wm = (WindowManager) context.getSystemService(Context.WINDOW_SERVICE);
    DisplayMetrics outMetrics = new DisplayMetrics();
    wm.getDefaultDisplay().getMetrics(outMetrics);
    return outMetrics.heightPixels;
}

From source file:Main.java

/**
 * Resamples the captured photo to fit the screen for better memory usage.
 *
 * @param context   The application context.
 * @param imagePath The path of the photo to be resampled.
 * @return The resampled bitmap/*from  w  ww  .j a  v a  2 s  . c om*/
 */
static Bitmap resamplePic(Context context, String imagePath) {

    // Get device screen size information
    DisplayMetrics metrics = new DisplayMetrics();
    WindowManager manager = (WindowManager) context.getSystemService(Context.WINDOW_SERVICE);
    manager.getDefaultDisplay().getMetrics(metrics);

    int targetH = metrics.heightPixels;
    int targetW = metrics.widthPixels;

    // Get the dimensions of the original bitmap
    BitmapFactory.Options bmOptions = new BitmapFactory.Options();
    bmOptions.inJustDecodeBounds = true;
    BitmapFactory.decodeFile(imagePath, bmOptions);
    int photoW = bmOptions.outWidth;
    int photoH = bmOptions.outHeight;

    // Determine how much to scale down the image
    int scaleFactor = Math.min(photoW / targetW, photoH / targetH);

    // Decode the image file into a Bitmap sized to fill the View
    bmOptions.inJustDecodeBounds = false;
    bmOptions.inSampleSize = scaleFactor;

    return BitmapFactory.decodeFile(imagePath);
}

From source file:Main.java

public static int getDevDensityDpi(Activity mactivity) {
    DisplayMetrics metric = new DisplayMetrics();
    mactivity.getWindowManager().getDefaultDisplay().getMetrics(metric);
    int densityDpi = metric.densityDpi;
    return densityDpi;
}

From source file:Main.java

public static float getDevScaledDensity(Activity mactivity) {
    DisplayMetrics metric = new DisplayMetrics();
    mactivity.getWindowManager().getDefaultDisplay().getMetrics(metric);
    float scaledDensity = metric.scaledDensity;
    return scaledDensity;
}

From source file:Main.java

/**
 * Returns display metrics for the specified view.
 * /*ww  w  .  j  a va  2  s .  co m*/
 * @param view An instance of View (must be the child of an Activity).
 * 
 * @return DisplayMetrics instance, or null if there was a problem.
 */
public static DisplayMetrics getDisplayMetrics(View view) {
    if (view == null)
        return null;

    Context context = view.getContext();
    if (!(context instanceof Activity))
        return null;
    DisplayMetrics metrics = new DisplayMetrics();
    ((Activity) context).getWindowManager().getDefaultDisplay().getMetrics(metrics);
    return metrics;
}

From source file:Main.java

/**
 * get a screen shot with default screen size.
 *///from w w w .j a  v a 2 s  .  c o  m
public static Bitmap getScreenShot(Activity activity) {
    DisplayMetrics metrics = new DisplayMetrics();
    activity.getWindowManager().getDefaultDisplay().getMetrics(metrics);
    int width = metrics.widthPixels;
    int height = metrics.heightPixels;
    return getScreenShot(activity, width, height);
}

From source file:Main.java

/**
 * Checks if the device is a tablet or a phone
 *
 * @param activityContext//from   w ww. jav a2  s  . c  om
 *          The Activity Context.
 * @return Returns true if the device is a Tablet
 */
public static boolean isTabletDevice(Context activityContext) {
    // Verifies if the Generalized Size of the device is XLARGE to be
    // considered a Tablet
    boolean xlarge = ((activityContext.getResources().getConfiguration().screenLayout
            & Configuration.SCREENLAYOUT_SIZE_MASK) >= Configuration.SCREENLAYOUT_SIZE_LARGE);
    // If XLarge, checks if the Generalized Density is at least MDPI
    // (160dpi)
    if (xlarge) {
        DisplayMetrics metrics = new DisplayMetrics();
        Activity activity = (Activity) activityContext;
        activity.getWindowManager().getDefaultDisplay().getMetrics(metrics);
        // MDPI=160, DEFAULT=160, DENSITY_HIGH=240, DENSITY_MEDIUM=160,
        // DENSITY_TV=213, DENSITY_XHIGH=320
        if (metrics.densityDpi == DisplayMetrics.DENSITY_DEFAULT
                || metrics.densityDpi == DisplayMetrics.DENSITY_HIGH
                || metrics.densityDpi == DisplayMetrics.DENSITY_MEDIUM
                || metrics.densityDpi == DisplayMetrics.DENSITY_TV
                || metrics.densityDpi == DisplayMetrics.DENSITY_XHIGH) {
            // Yes, this is a tablet!
            return true;
        }
    }
    // No, this is not a tablet!
    return false;
}

From source file:Main.java

public static int getDPI() {
    if (sContext != null) {
        DisplayMetrics metrics = new DisplayMetrics();
        WindowManager wm = ((Activity) sContext).getWindowManager();
        if (wm != null) {
            Display d = wm.getDefaultDisplay();
            if (d != null) {
                d.getMetrics(metrics);/*from w  ww  .j a v a 2  s.  c o m*/
                return (int) (metrics.density * 160.0f);
            }
        }
    }
    return -1;
}

From source file:Main.java

public static int getDPI() {
    if (sActivity != null) {
        DisplayMetrics metrics = new DisplayMetrics();
        WindowManager wm = sActivity.getWindowManager();
        if (wm != null) {
            Display d = wm.getDefaultDisplay();
            if (d != null) {
                d.getMetrics(metrics);/*from w w w  .  ja  v a  2  s . co m*/
                return (int) (metrics.density * 160.0f);
            }
        }
    }
    return -1;
}