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

public static Bitmap getBitmapForDensity(Resources res, int displayDpi, int resId) {
    try {//from  w  ww  . j  a va2s  . co m
        TypedValue value = new TypedValue();
        res.getValueForDensity(resId, displayDpi, value, true);
        AssetFileDescriptor fd = res.getAssets().openNonAssetFd(value.assetCookie, value.string.toString());
        Options opt = new Options();
        opt.inTargetDensity = displayDpi;
        Bitmap bitmap = BitmapFactory.decodeResourceStream(res, value, fd.createInputStream(), null, opt);
        bitmap.setDensity(res.getDisplayMetrics().densityDpi);
        fd.close();
        return bitmap;
    } catch (Exception e) {
        return BitmapFactory.decodeResource(res, resId);
    }
}

From source file:com.lingganhezi.ui.widget.LinePageIndicator.java

/**
 * dppx/*from w ww.jav a2s  . c  o  m*/
 * 
 * @param res
 *            android.content.res.Resources
 * @param dp
 * @return
 */
public static int dpToPx(Resources res, int dp) {
    return (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, dp, res.getDisplayMetrics());
}

From source file:org.chromium.chrome.browser.notifications.NotificationUIManager.java

/**
 * Creates the rounded icon generator to use for notifications based on the dimensions
 * and resolution of the device we're running on.
 *
 * @param appContext The application context to retrieve resources from.
 * @return The newly created rounded icon generator.
 *///w  ww . ja v  a 2  s.  c o m
@VisibleForTesting
public static RoundedIconGenerator createRoundedIconGenerator(Context appContext) {
    Resources res = appContext.getResources();
    float density = res.getDisplayMetrics().density;

    int widthPx = res.getDimensionPixelSize(android.R.dimen.notification_large_icon_width);
    int heightPx = res.getDimensionPixelSize(android.R.dimen.notification_large_icon_height);

    return new RoundedIconGenerator(widthPx, heightPx, Math.min(widthPx, heightPx) / 2,
            NOTIFICATION_ICON_BG_COLOR, NOTIFICATION_TEXT_SIZE_DP * density);
}

From source file:com.bilibili.magicasakura.utils.ThemeUtils.java

public static Resources updateNightMode(Resources resource, boolean on) {
    DisplayMetrics dm = resource.getDisplayMetrics();
    Configuration config = resource.getConfiguration();
    final int uiModeNightMaskOrigin = config.uiMode &= ~Configuration.UI_MODE_TYPE_MASK;
    final int uiModeNightMaskNew = on ? Configuration.UI_MODE_NIGHT_YES : Configuration.UI_MODE_NIGHT_NO;
    if (uiModeNightMaskOrigin != uiModeNightMaskNew) {
        config.uiMode &= ~Configuration.UI_MODE_NIGHT_MASK;
        config.uiMode |= uiModeNightMaskNew;
        resource.updateConfiguration(config, dm);
    }/*from  w w w  . j ava  2 s . co  m*/
    return resource;
}

From source file:Main.java

public static void setLocale(String lang, Resources res) {
    Locale myLocale;/*from  w  w  w  . j a  va  2  s .c  om*/
    if (lang.equalsIgnoreCase("zh-rTW")) {
        myLocale = Locale.TRADITIONAL_CHINESE;
    } else if (lang.equalsIgnoreCase("zh-rCN") || lang.equalsIgnoreCase("zh")) {
        myLocale = Locale.SIMPLIFIED_CHINESE;
    } else if (lang.equalsIgnoreCase("pt-rBR") || lang.equalsIgnoreCase("pt")) {
        myLocale = new Locale("pt", "BR");
    } else if (lang.equalsIgnoreCase("pt-rPT")) {
        myLocale = new Locale("pt", "PT");
    } else {
        myLocale = new Locale(lang);
    }
    DisplayMetrics dm = res.getDisplayMetrics();
    Configuration conf = res.getConfiguration();
    conf.locale = myLocale;
    res.updateConfiguration(conf, dm);
}

From source file:tr.com.turkcellteknoloji.turkcellupdater.Utilities.java

static float convertPixelsToDp(float px, Context context) {
    Resources resources = context.getResources();
    DisplayMetrics metrics = resources.getDisplayMetrics();
    float dp = px / (metrics.densityDpi / 160f);
    return dp;/*  w  ww .  j  a va 2 s  .  c om*/
}

From source file:com.aero.control.helpers.Android.CirclePageIndicator.java

private static int dpToPx(float dp, Resources resources) {
    float px = TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, dp, resources.getDisplayMetrics());
    return (int) px;
}

From source file:Main.java

/**
 * Set the background of a {@link android.view.View} to the specified color, with a darker version of the
 * color as a border./* w ww. j  a v  a2s . c  o  m*/
 */
public static void setViewBackground(View view, int color) {
    Resources r = view.getResources();

    Drawable currentDrawable = view.getBackground();
    GradientDrawable backgroundDrawable;
    if (currentDrawable != null && currentDrawable instanceof GradientDrawable) {
        // Reuse drawable
        backgroundDrawable = (GradientDrawable) currentDrawable;
    } else {
        backgroundDrawable = new GradientDrawable();
    }

    int darkenedColor = darkenColor(color);

    backgroundDrawable.setColor(color);
    backgroundDrawable.setStroke(
            (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, 1, r.getDisplayMetrics()),
            darkenedColor);

    view.setBackgroundDrawable(backgroundDrawable);
}

From source file:tr.com.turkcellteknoloji.turkcellupdater.Utilities.java

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

From source file:com.desno365.mods.DesnoUtils.java

public static int convertDpToPixel(int dp, Context context) {
    Resources resources = context.getResources();
    DisplayMetrics metrics = resources.getDisplayMetrics();
    int px;//from   w w w  . j  a v a  2  s .  c  om
    px = (int) (dp * metrics.density);
    return px;
}