Example usage for android.content.res Resources getDisplayMetrics

List of usage examples for android.content.res Resources getDisplayMetrics

Introduction

In this page you can find the example usage for android.content.res Resources getDisplayMetrics.

Prototype

public DisplayMetrics getDisplayMetrics() 

Source Link

Document

Return the current display metrics that are in effect for this resource object.

Usage

From source file:Main.java

private static Bitmap loadAssetBitmap(Context context, String assetPath) {
    InputStream is = null;/*  w w w .  j ava 2  s. c  om*/
    try {
        Resources resources = context.getResources();
        Options options = new Options();
        options.inDensity = DisplayMetrics.DENSITY_HIGH;
        options.inScreenDensity = resources.getDisplayMetrics().densityDpi;
        options.inTargetDensity = resources.getDisplayMetrics().densityDpi;
        is = context.getAssets().open(assetPath);
        Bitmap bitmap = BitmapFactory.decodeStream(is, new Rect(), options);
        if (bitmap != null) {
            drawableCache.put(assetPath, bitmap);
        }
        return bitmap;
    } catch (Exception e) {
        e.printStackTrace();
    } finally {
        if (is != null) {
            try {
                is.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }
    return null;
}

From source file:Main.java

public static Bitmap downSampleBitmap(Uri uri, Activity act, Boolean needRotate) {
    DisplayMetrics displaymetrics = new DisplayMetrics();
    act.getWindowManager().getDefaultDisplay().getMetrics(displaymetrics);
    Resources r = act.getResources();
    int px = (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, 50, r.getDisplayMetrics()); // 50: magic num
    int targetWidth = displaymetrics.heightPixels;
    int targetHeight = displaymetrics.widthPixels - px;

    Bitmap resizedBitmap = decodeSampledBitmap(uri, targetWidth, targetHeight, act);
    Bitmap returnBitmap = null;//from w w  w.j av a  2 s  . c o m
    ExifInterface exif;
    try {
        float degree = 0;
        exif = new ExifInterface(uri.toString());
        int orient = exif.getAttributeInt(ExifInterface.TAG_ORIENTATION, ExifInterface.ORIENTATION_NORMAL);
        if (resizedBitmap != null && needRotate) {
            degree = getDegree(orient);
            if (degree != 0) {
                returnBitmap = createRotatedBitmap(resizedBitmap, degree);
            }
            returnBitmap = returnBitmap == null ? resizedBitmap : returnBitmap;
        }
    } catch (IOException e) {
        Log.v(TAG, "not found file at downsample");
        e.printStackTrace();
    }
    return returnBitmap;
}

From source file:Main.java

/**
 * Sets the system locale for this process.
 *
 * @param res the resources to use. Pass current resources.
 * @param newLocale the locale to change to.
 * @return the old locale.//  ww  w  .j ava  2  s .  com
 */
public static Locale setSystemLocale(final Resources res, final Locale newLocale) {
    final Configuration conf = res.getConfiguration();
    final Locale saveLocale = conf.locale;
    conf.locale = newLocale;
    res.updateConfiguration(conf, res.getDisplayMetrics());
    return saveLocale;
}

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 ww.ja v a 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

public static TextPaint setTextSize(Context c, TextPaint paint, float size) {
    Resources r;
    if (c == null) {
        r = Resources.getSystem();
    } else {//w  w w. j  av a 2s  .  c om
        r = c.getResources();
    }
    if (r != null) {
        paint.setTextSize(TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_SP, size, r.getDisplayMetrics()));
    }
    return paint;
}

From source file:net.luna.common.util.ParseUtils.java

public static int pxToDp(Context context, int px) {
    Resources res = context.getResources();
    float density = res.getDisplayMetrics().density;
    return (int) (px / density + 0.5f);
}

From source file:Main.java

public static void setLocale(Context paramContext, Locale paramLocale) {
    Resources localResources = paramContext.getResources();
    Configuration localConfiguration = localResources.getConfiguration();
    if (localConfiguration.locale.equals(paramLocale)) {

    } else {//w  w w. ja  va  2s.co  m
        DisplayMetrics localDisplayMetrics = localResources.getDisplayMetrics();
        localConfiguration.locale = paramLocale;
        localResources.updateConfiguration(localConfiguration, localDisplayMetrics);
        Resources.getSystem().updateConfiguration(localConfiguration, localDisplayMetrics);
    }
}

From source file:com.inductivebiblestudyapp.ui.dialogs.SimpleTooltipDialog.java

/**
 * Converts dp to px. // w ww . j  av  a  2  s .c  om
 * @param dp the dp in
 * @return The raw px out
 */
protected static int dpToPx(Resources res, double dp) {
    return (int) (dp * res.getDisplayMetrics().density);
}

From source file:net.luna.common.util.ParseUtils.java

public static int spToPx(Context context, int sp) {
    Resources res = context.getResources();
    return (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_SP, sp, res.getDisplayMetrics());
}

From source file:codepath.watsiapp.utils.Util.java

public static float getPixelsFont(Activity activity, float sp) {
    Resources r = activity.getResources();
    float px = TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_SP, sp, r.getDisplayMetrics());
    return px;/*  w w  w. j ava2 s .c o  m*/
}