Example usage for android.util TypedValue COMPLEX_UNIT_DIP

List of usage examples for android.util TypedValue COMPLEX_UNIT_DIP

Introduction

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

Prototype

int COMPLEX_UNIT_DIP

To view the source code for android.util TypedValue COMPLEX_UNIT_DIP.

Click Source Link

Document

#TYPE_DIMENSION complex unit: Value is Device Independent Pixels.

Usage

From source file:Main.java

/**
 * /*from w  ww .  j  a  v a2s. c o  m*/
 * @param context
 * @param dip
 * @return
 */
public static float dipToPixels(Context context, int dip) {
    Resources r = context.getResources();
    float px = TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, dip, r.getDisplayMetrics());
    return px;
}

From source file:Main.java

public static int dp2px(Context context, float dpVal) {
    return (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, dpVal,
            context.getResources().getDisplayMetrics());
}

From source file:Main.java

/**
 * Converts density pixels to pixels/*w  w w .  java 2s. co m*/
 *
 * @param context context for getResources()
 * @param dp      desired density pixels
 * @return converted dp to pixels
 */
static float dpToPx(Context context, final int dp) {
    return TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, dp,
            context.getResources().getDisplayMetrics());
}

From source file:Main.java

/**
 * Converts a value from device independent pixels (dp) to pixels.
 * @param context context.//w w w  .  j a v a 2s  . c  o  m
 * @param dpValue the value to convert.
 * @return the given value converted to pixels.
 */
public static float toPixels(Context context, float dpValue) {
    return TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, dpValue,
            context.getResources().getDisplayMetrics());
}

From source file:Main.java

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

From source file:Main.java

/**
 * Converts density pixels into pixels./*  w ww.j  ava 2  s . co m*/
 *
 * @param context the application context.
 * @param densityPixels the amount of DPs to convert.
 * @return the amount in pixels.
 */
public static int getPixels(Context context, int densityPixels) {
    return (int) Math.ceil(TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, densityPixels,
            context.getResources().getDisplayMetrics()));
}

From source file:Main.java

/**
 * This method converts dp to pixels//from www .  j  a  v a 2s.c o m
 *
 * @param dp
 *         Oiringla dp
 * @param context
 *         App context
 *
 * @return Dp value in pixels
 */
public static int dp2Pixels(int dp, Context context) {
    Resources r = context.getResources();
    return (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, dp, r.getDisplayMetrics());
}

From source file:Main.java

/**
 * Converts a given dip-value into a pixel-value
 *
 * @param dip     - the dip value to convert
 * @param context - the application context
 * @return the converted pixel value//from   w  ww .  ja va 2 s.co m
 */
public static int dipToPx(int dip, Context context) {

    Resources r = context.getResources();
    return (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, dip, r.getDisplayMetrics());
}

From source file:Main.java

/**
 * Converts pixels to density-independent pixels (dip).
 *
 * @param context/*from  w w w. j av a2s  . c om*/
 * @param pixels
 * @return the converted number of pixels based on the display metrics
 */
public static int pixelsToDip(Context context, int pixels) {
    return (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, pixels,
            context.getResources().getDisplayMetrics());
}

From source file:Main.java

/**
 * Converts density independent pixel to real screen pixel. 160 dip = 1 inch
 * ~ 2.5 cm/* w w  w  . jav  a 2s  . co m*/
 * 
 * @param dipValue
 *            dip
 * @return pixel
 */
public static int getPixels(Resources r, double dipValue) {
    int dValue = (int) dipValue;
    int px = (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, dValue, r.getDisplayMetrics());
    return px;
}