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:com.desno365.mods.DesnoUtils.java

@SuppressWarnings("unused")
public static int convertPixelsToDp(int px, Context context) {
    Resources resources = context.getResources();
    DisplayMetrics metrics = resources.getDisplayMetrics();
    int dp;// w w w.j av  a 2s .  c o m
    dp = (int) (px / metrics.density);
    return dp;
}

From source file:de.vanita5.twittnuker.util.CustomTabUtils.java

public static Bitmap getTabIconFromFile(final File file, final Resources res) {
    if (file == null || !file.exists())
        return null;
    final String path = file.getPath();
    final BitmapFactory.Options o = new BitmapFactory.Options();
    o.inJustDecodeBounds = true;// w  w  w .ja v a2s.  c o  m
    BitmapFactory.decodeFile(path, o);
    if (o.outHeight <= 0 || o.outWidth <= 0)
        return null;
    o.inSampleSize = (int) (Math.max(o.outWidth, o.outHeight) / (48 * res.getDisplayMetrics().density));
    o.inJustDecodeBounds = false;
    return BitmapFactory.decodeFile(path, o);
}

From source file:com.linkbubble.util.Util.java

private static void initStatics(Context context) {
    final Resources resources = context.getResources();
    final DisplayMetrics metrics = resources.getDisplayMetrics();
    final float density = metrics.density;

    sIconWidth = sIconHeight = (int) resources.getDimension(R.dimen.app_icon_size);
    sIconTextureWidth = sIconTextureHeight = sIconWidth;
}

From source file:com.fastbootmobile.encore.utils.Utils.java

public static int dpToPx(Resources res, int dp) {
    final DisplayMetrics displayMetrics = res.getDisplayMetrics();
    return Math.round(dp * (displayMetrics.xdpi / DisplayMetrics.DENSITY_DEFAULT));
}

From source file:com.gh4a.utils.HttpImageGetter.java

private static Bitmap renderSvgToBitmap(Resources res, InputStream is, int maxWidth, int maxHeight) {
    //noinspection TryWithIdenticalCatches
    try {/* ww w  . j  a va  2s. c om*/
        SVG svg = SVG.getFromInputStream(is);
        if (svg != null) {
            svg.setRenderDPI(DisplayMetrics.DENSITY_DEFAULT);
            Float density = res.getDisplayMetrics().density;
            int docWidth = (int) (svg.getDocumentWidth() * density);
            int docHeight = (int) (svg.getDocumentHeight() * density);
            if (docWidth < 0 || docHeight < 0) {
                float aspectRatio = svg.getDocumentAspectRatio();
                if (aspectRatio > 0) {
                    float heightForAspect = (float) maxWidth / aspectRatio;
                    float widthForAspect = (float) maxHeight * aspectRatio;
                    if (widthForAspect < heightForAspect) {
                        docWidth = Math.round(widthForAspect);
                        docHeight = maxHeight;
                    } else {
                        docWidth = maxWidth;
                        docHeight = Math.round(heightForAspect);
                    }
                } else {
                    docWidth = maxWidth;
                    docHeight = maxHeight;
                }

                // we didn't take density into account anymore when calculating docWidth
                // and docHeight, so don't scale with it and just let the renderer
                // figure out the scaling
                density = null;
            }

            while (docWidth >= maxWidth || docHeight >= maxHeight) {
                docWidth /= 2;
                docHeight /= 2;
                if (density != null) {
                    density /= 2;
                }
            }

            Bitmap bitmap = Bitmap.createBitmap(docWidth, docHeight, Bitmap.Config.ARGB_8888);
            Canvas canvas = new Canvas(bitmap);
            if (density != null) {
                canvas.scale(density, density);
            }
            svg.renderToCanvas(canvas);
            return bitmap;
        }
    } catch (SVGParseException e) {
        // fall through
    } catch (NullPointerException e) {
        // https://github.com/BigBadaboom/androidsvg/issues/81
        // remove me when there's a 1.2.3 release
    }
    return null;
}

From source file:com.xtremelabs.imageutils.ImageLoader.java

public static int convertDpToPixels(Context context, int dp) {
    Resources r = context.getResources();
    return (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, dp, r.getDisplayMetrics());
}

From source file:com.android.argb.edhlc.Utils.java

public static float convertDpToPixel(float dp, Context context) {
    Resources resources = context.getResources();
    DisplayMetrics metrics = resources.getDisplayMetrics();
    return dp * (metrics.densityDpi / DisplayMetrics.DENSITY_DEFAULT);
}

From source file:com.gm.goldencity.util.Utils.java

/**
 * Changing default language//from   w w  w  .j  a v  a2 s .c  om
 *
 * @param context       Application context
 * @param language_code Lang code to FA or EN - BR and etc.
 */
public static void changeLanguage(Context context, String language_code) {
    Resources res = context.getResources();
    // Change locale settings in the app.
    DisplayMetrics dm = res.getDisplayMetrics();
    android.content.res.Configuration conf = res.getConfiguration();
    conf.locale = new Locale(language_code);
    res.updateConfiguration(conf, dm);
}

From source file:com.gm.goldencity.util.Utils.java

/**
 * Changing default language//from   www. j ava  2  s. com
 *
 * @param context       Application context
 * @param language_code Lang code to FA or EN - BR and etc.
 * @param title         Will set to activity
 */
public static void changeLanguage(Context context, String language_code, String title) {
    Resources res = context.getResources();
    // Change locale settings in the app.
    DisplayMetrics dm = res.getDisplayMetrics();
    android.content.res.Configuration conf = res.getConfiguration();
    conf.locale = new Locale(language_code);
    res.updateConfiguration(conf, dm);

    Activity activity = (Activity) context;
    activity.setTitle(title);
}

From source file:org.lucasr.dspec.DesignSpec.java

/**
 * Creates a new {@link DesignSpec} instance from a resource ID using a {@link View}
 * that will provide the {@link DesignSpec}'s intrinsic dimensions.
 *
 * @param view The {@link View} who will own the new {@link DesignSpec} instance.
 * @param resId The resource ID pointing to a raw JSON resource.
 * @return The newly created {@link DesignSpec} instance.
 *//*from ww w . j  av  a2 s  . c om*/
public static DesignSpec fromResource(View view, int resId) {
    final Resources resources = view.getResources();
    final DesignSpec spec = new DesignSpec(resources, view);
    if (resId == 0) {
        return spec;
    }

    final JSONObject json;
    try {
        json = RawResource.getAsJSON(resources, resId);
    } catch (IOException e) {
        throw new IllegalStateException("Could not read design spec resource", e);
    }

    final float density = resources.getDisplayMetrics().density;

    spec.setBaselineGridCellSize(
            density * json.optInt(JSON_KEY_BASELINE_GRID_CELL_SIZE, DEFAULT_BASELINE_GRID_CELL_SIZE_DIP));

    spec.setBaselineGridVisible(json.optBoolean(JSON_KEY_BASELINE_GRID_VISIBLE, DEFAULT_BASELINE_GRID_VISIBLE));
    spec.setKeylinesVisible(json.optBoolean(JSON_KEY_KEYLINES_VISIBLE, DEFAULT_KEYLINES_VISIBLE));
    spec.setSpacingsVisible(json.optBoolean(JSON_KEY_SPACINGS_VISIBLE, DEFAULT_SPACINGS_VISIBLE));

    spec.setBaselineGridColor(
            Color.parseColor(json.optString(JSON_KEY_BASELINE_GRID_COLOR, DEFAULT_BASELINE_GRID_COLOR)));
    spec.setKeylinesColor(Color.parseColor(json.optString(JSON_KEY_KEYLINES_COLOR, DEFAULT_KEYLINE_COLOR)));
    spec.setSpacingsColor(Color.parseColor(json.optString(JSON_KEY_SPACINGS_COLOR, DEFAULT_SPACING_COLOR)));

    final JSONArray keylines = json.optJSONArray(JSON_KEY_KEYLINES);
    if (keylines != null) {
        final int keylineCount = keylines.length();
        for (int i = 0; i < keylineCount; i++) {
            try {
                final JSONObject keyline = keylines.getJSONObject(i);
                spec.addKeyline(keyline.getInt(JSON_KEY_OFFSET),
                        From.valueOf(keyline.getString(JSON_KEY_FROM).toUpperCase()));
            } catch (JSONException e) {
                continue;
            }
        }
    }

    final JSONArray spacings = json.optJSONArray(JSON_KEY_SPACINGS);
    if (spacings != null) {
        final int spacingCount = spacings.length();
        for (int i = 0; i < spacingCount; i++) {
            try {
                final JSONObject spacing = spacings.getJSONObject(i);
                spec.addSpacing(spacing.getInt(JSON_KEY_OFFSET), spacing.getInt(JSON_KEY_SIZE),
                        From.valueOf(spacing.getString(JSON_KEY_FROM).toUpperCase()));
            } catch (JSONException e) {
                continue;
            }
        }
    }

    return spec;
}