Example usage for android.util TypedValue applyDimension

List of usage examples for android.util TypedValue applyDimension

Introduction

In this page you can find the example usage for android.util TypedValue applyDimension.

Prototype

public static float applyDimension(int unit, float value, DisplayMetrics metrics) 

Source Link

Document

Converts an unpacked complex data value holding a dimension to its final floating point value.

Usage

From source file:Main.java

public static int dp(Context context, float dp) {
    Resources resources = context.getResources();
    int px = (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, dp, resources.getDisplayMetrics());
    return px;/*from   www .ja  va 2s.  co m*/
}

From source file:Main.java

public static int sp(Context context, float sp) {
    Resources resources = context.getResources();
    int px = (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_SP, sp, resources.getDisplayMetrics());
    return px;//from w ww  .j  a  v  a  2 s .c om
}

From source file:Main.java

public static int pixToDip(Context context, float pixValue) {
    Resources r = context.getResources();
    float px = TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, pixValue, r.getDisplayMetrics());

    return (int) px;
}

From source file:Main.java

static int convertToPx(int dp, Resources resources) {
    DisplayMetrics dm = resources.getDisplayMetrics();
    return (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, dp, dm);
}

From source file:Main.java

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

From source file:Main.java

public static int getSizeByGivenAbsSize(Context context, int givenAbsSize) {
    return (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, givenAbsSize,
            context.getResources().getDisplayMetrics());
}

From source file:Main.java

public static float getRawSize(int unit, float size, Context context) {
    Resources r;//from w ww  . jav a  2 s. c o  m
    r = context.getResources();
    return TypedValue.applyDimension(unit, size, r.getDisplayMetrics());
}

From source file:Main.java

/**
 * sp*ppi/160  =px//from  ww  w  .  j av  a 2 s .co m
 * 
 * @param ctx
 * @param dip
 * @return
 */
public static int spToPX(final Context ctx, float sp) {
    return (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_SP, sp,
            ctx.getResources().getDisplayMetrics());
}

From source file:Main.java

/**
 * Converts the number in pixels to the number in dips
 *//*from w ww .jav  a 2s  .com*/
public static int convertToDip(DisplayMetrics displayMetrics, int sizeInPixels) {
    return (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, sizeInPixels, displayMetrics);
}

From source file:Main.java

public static float sp2px(Context context, float sp) {
    if (context == null) {
        return sp;
    }//from w w  w.  ja  v  a  2 s. c o  m
    Resources r = context.getResources();
    float size = TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_SP, sp, r.getDisplayMetrics());
    return size;
}