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 float dp2px(Resources resources, float dp) {
    float v = TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, dp, resources.getDisplayMetrics());
    return (int) (v + 0.5f);
}

From source file:Main.java

/**
 * This method converts device specific pixels to density independent
 * pixels./*from ww w  .j  a  va2 s .co  m*/
 * 
 * @param px
 *            A value in px (pixels) unit. Which we need to convert into db
 * @param context
 *            Context to get resources and device specific display metrics
 * @return A float value to represent dp equivalent to px value
 */
public static float convertPixelsToDp(float px, Context context) {
    Resources resources = context.getResources();
    DisplayMetrics metrics = resources.getDisplayMetrics();
    float dp = px / (metrics.densityDpi / 160f);
    return dp;
}

From source file:Main.java

public static int pixelsToDp(Context c, int px) {

    Resources resources = c.getResources();
    DisplayMetrics metrics = resources.getDisplayMetrics();
    float dp = px / (metrics.densityDpi / 160f);

    return (int) dp;
}

From source file:Main.java

public static int getSpToPx(Resources res, int dip) {
    int pixel = (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_SP, dip, res.getDisplayMetrics());
    return pixel;
}

From source file:Main.java

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

From source file:Main.java

public static int dpToPixels(Context c, int dp) {

    Resources resources = c.getResources();
    DisplayMetrics metrics = resources.getDisplayMetrics();
    float px = dp * (metrics.densityDpi / 160f);

    return (int) px;
}

From source file:Main.java

/**
 * Does conversion from dp to px.//from w  w w.jav  a2  s  .co  m
 * @param resources
 * @param dp
 * @return float equivalent of pixels
 */
public static float dpToPx(Resources resources, int dp) {
    return TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, dp, resources.getDisplayMetrics());
}

From source file:Main.java

public static int getDipToPx(Resources res, int dip) {
    int pixel = (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, dip, res.getDisplayMetrics());
    return pixel;
}

From source file:Main.java

public static BitmapDrawable getCachedIcon(Resources res, String packageName, int resId, Options opts) {
    int displayDpi = res.getDisplayMetrics().densityDpi;
    Bitmap bitmap = BitmapFactory.decodeFile(getCacheFilePath(packageName, resId), opts);
    if (bitmap == null) {
        return null;
    }/*www .j  a va 2  s.  c o m*/
    bitmap.setDensity(displayDpi);
    return new BitmapDrawable(res, bitmap);
}

From source file:Main.java

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