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

public static DisplayMetrics getDisplayMetrics(Activity activity) {
    if (dm == null) {
        dm = new DisplayMetrics();
        activity.getWindowManager().getDefaultDisplay().getMetrics(dm);
    }/*  ww  w.j a va 2  s.c o  m*/
    return dm;
}

From source file:Main.java

/**
 * Get the required size of the favicon, depending on current screen density.
 * @param activity The current activity.
 * @return The size of the favicon, in pixels.
 *//*w ww  .j  a  va 2 s.c  om*/
public static int getFaviconSize(Activity activity) {
    if (mFaviconSize == -1) {
        DisplayMetrics metrics = new DisplayMetrics();
        activity.getWindowManager().getDefaultDisplay().getMetrics(metrics);

        switch (metrics.densityDpi) {
        case DisplayMetrics.DENSITY_LOW:
            mFaviconSize = 12;
            break;
        case DisplayMetrics.DENSITY_MEDIUM:
            mFaviconSize = 24;
            break;
        case DisplayMetrics.DENSITY_HIGH:
            mFaviconSize = 32;
            break;
        default:
            mFaviconSize = 24;
        }
    }

    return mFaviconSize;
}

From source file:Main.java

/**
 * @param ctx/*from   ww  w . j  a  v a 2s .  com*/
 */
public static float getDensity(Context ctx) {
    if (mMetrics == null) {
        mMetrics = new DisplayMetrics();
        ((WindowManager) ctx.getSystemService(Context.WINDOW_SERVICE)).getDefaultDisplay().getMetrics(mMetrics);
    }
    return mMetrics.density;
}

From source file:Main.java

/**
 * Get the required size of the favicon, depending on current screen density.
 * @param activity The current activity.
 * @return The size of the favicon, in pixels.
 *//*from  www .  j a v a2 s  .  c  om*/
public static int getFaviconSizeForBookmarks(Activity activity) {
    if (mFaviconSizeForBookmarks == -1) {
        DisplayMetrics metrics = new DisplayMetrics();
        activity.getWindowManager().getDefaultDisplay().getMetrics(metrics);

        switch (metrics.densityDpi) {
        case DisplayMetrics.DENSITY_LOW:
            mFaviconSizeForBookmarks = 12;
            break;
        case DisplayMetrics.DENSITY_MEDIUM:
            mFaviconSizeForBookmarks = 16;
            break;
        case DisplayMetrics.DENSITY_HIGH:
            mFaviconSizeForBookmarks = 24;
            break;
        default:
            mFaviconSizeForBookmarks = 16;
        }
    }

    return mFaviconSizeForBookmarks;
}

From source file:Main.java

/**
 * Gets the screen density for the device.
 * //from  w w w  . j a  v  a 2 s.  c o  m
 * @param activity
 *          is the current activity.
 * 
 * @return A double value representing the device's screen density.
 */
public static double getDensity(Activity activity) {
    if (density == -1) {
        DisplayMetrics displayMetrics = new DisplayMetrics();
        activity.getWindowManager().getDefaultDisplay().getMetrics(displayMetrics);
        density = displayMetrics.density;
    }

    return density;
}

From source file:Main.java

private static DisplayMetrics getDisplayMetrics(Context context) {
    DisplayMetrics dm = new DisplayMetrics();
    ((Activity) context).getWindowManager().getDefaultDisplay().getMetrics(dm);
    return dm;/*from  w ww.  j  a va  2 s . c  o m*/
}

From source file:Main.java

public static int getScreenOrientation(Activity activity) {
    int rotation = activity.getWindowManager().getDefaultDisplay().getRotation();
    DisplayMetrics dm = new DisplayMetrics();
    activity.getWindowManager().getDefaultDisplay().getMetrics(dm);
    int width = dm.widthPixels;
    int height = dm.heightPixels;
    int orientation;
    // if the device's natural orientation is portrait:
    if ((rotation == Surface.ROTATION_0 || rotation == Surface.ROTATION_180) && height > width
            || (rotation == Surface.ROTATION_90 || rotation == Surface.ROTATION_270) && width > height) {
        switch (rotation) {
        case Surface.ROTATION_0:
            orientation = ActivityInfo.SCREEN_ORIENTATION_PORTRAIT;
            break;
        case Surface.ROTATION_90:
            orientation = ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE;
            break;
        case Surface.ROTATION_180:
            orientation = ActivityInfo.SCREEN_ORIENTATION_REVERSE_PORTRAIT;
            break;
        case Surface.ROTATION_270:
            orientation = ActivityInfo.SCREEN_ORIENTATION_REVERSE_LANDSCAPE;
            break;
        default://from  ww  w .jav a  2 s  .c o m
            Log.e("getScreenOrientation", "Unknown screen orientation. Defaulting to portrait.");
            orientation = ActivityInfo.SCREEN_ORIENTATION_PORTRAIT;
            break;
        }
    } else {
        // if the device's natural orientation is landscape or if the device
        // is square:

        switch (rotation) {
        case Surface.ROTATION_0:
            orientation = ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE;
            break;
        case Surface.ROTATION_90:
            orientation = ActivityInfo.SCREEN_ORIENTATION_PORTRAIT;
            break;
        case Surface.ROTATION_180:
            orientation = ActivityInfo.SCREEN_ORIENTATION_REVERSE_LANDSCAPE;
            break;
        case Surface.ROTATION_270:
            orientation = ActivityInfo.SCREEN_ORIENTATION_REVERSE_PORTRAIT;
            break;
        default:
            Log.e("getScreenOrientation", "Unknown screen orientation. Defaulting to landscape.");
            orientation = ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE;
            break;
        }
    }

    return orientation;
}

From source file:Main.java

public static float getDisplayDensity(Context context) {
    if (sDensity == Float.MAX_VALUE) {
        Display display = ((WindowManager) context.getSystemService(Context.WINDOW_SERVICE))
                .getDefaultDisplay();/*  w  ww.java2s. c o m*/
        DisplayMetrics metrics = new DisplayMetrics();
        display.getMetrics(metrics);
        sDensity = metrics.density;
    }
    return sDensity;
}

From source file:Main.java

public static Drawable showUninstallAPKIcon(Context context, String apkPath) {
    String PATH_PackageParser = "android.content.pm.PackageParser";
    String PATH_AssetManager = "android.content.res.AssetManager";
    try {//  w w  w  . ja  va  2 s .co  m
        Class<?> pkgParserCls = Class.forName(PATH_PackageParser);
        Class<?>[] typeArgs = new Class[1];
        typeArgs[0] = String.class;
        Constructor<?> pkgParserCt = pkgParserCls.getConstructor(typeArgs);
        Object[] valueArgs = new Object[1];
        valueArgs[0] = apkPath;
        Object pkgParser = pkgParserCt.newInstance(valueArgs);
        DisplayMetrics metrics = new DisplayMetrics();
        metrics.setToDefaults();
        typeArgs = new Class[4];
        typeArgs[0] = File.class;
        typeArgs[1] = String.class;
        typeArgs[2] = DisplayMetrics.class;
        typeArgs[3] = Integer.TYPE;
        Method pkgParser_parsePackageMtd = pkgParserCls.getDeclaredMethod("parsePackage", typeArgs);
        valueArgs = new Object[4];
        valueArgs[0] = new File(apkPath);
        valueArgs[1] = apkPath;
        valueArgs[2] = metrics;
        valueArgs[3] = 0;
        Object pkgParserPkg = pkgParser_parsePackageMtd.invoke(pkgParser, valueArgs);
        Field appInfoFld = pkgParserPkg.getClass().getDeclaredField("applicationInfo");
        ApplicationInfo info = (ApplicationInfo) appInfoFld.get(pkgParserPkg);
        Class<?> assetMagCls = Class.forName(PATH_AssetManager);
        Constructor<?> assetMagCt = assetMagCls.getConstructor((Class[]) null);
        Object assetMag = assetMagCt.newInstance((Object[]) null);
        typeArgs = new Class[1];
        typeArgs[0] = String.class;
        Method assetMag_addAssetPathMtd = assetMagCls.getDeclaredMethod("addAssetPath", typeArgs);
        valueArgs = new Object[1];
        valueArgs[0] = apkPath;
        assetMag_addAssetPathMtd.invoke(assetMag, valueArgs);
        Resources res = context.getResources();
        typeArgs = new Class[3];
        typeArgs[0] = assetMag.getClass();
        typeArgs[1] = res.getDisplayMetrics().getClass();
        typeArgs[2] = res.getConfiguration().getClass();
        Constructor<?> resCt = Resources.class.getConstructor(typeArgs);
        valueArgs = new Object[3];
        valueArgs[0] = assetMag;
        valueArgs[1] = res.getDisplayMetrics();
        valueArgs[2] = res.getConfiguration();
        res = (Resources) resCt.newInstance(valueArgs);
        if (info.icon != 0) {
            Drawable icon = res.getDrawable(info.icon);
            return icon;
        }
    } catch (Exception e) {
        e.printStackTrace();
    }
    return null;
}

From source file:Main.java

@TargetApi(Build.VERSION_CODES.JELLY_BEAN_MR1)
public static int getSoftKeyBarHeight(Context context) {
    // getRealMetrics is only available with API 17 and +
    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN_MR1) {
        WindowManager wm = (WindowManager) context.getSystemService(Context.WINDOW_SERVICE);
        DisplayMetrics metrics = new DisplayMetrics();
        wm.getDefaultDisplay().getMetrics(metrics);
        int usableHeight = metrics.heightPixels;
        wm.getDefaultDisplay().getRealMetrics(metrics);
        int realHeight = metrics.heightPixels;
        if (realHeight > usableHeight) {
            return realHeight - usableHeight;
        } else {//from  www.ja  v a 2 s .c  o m
            return 0;
        }
    }
    return 0;
}