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

/**
 * Returns a density independent pixel value rounded to the nearest integer
 * for the desired dimension.//  www.  j a va2 s. c  om
 *
 * @param pixels The pixel value to be converted
 * @return A rounded DIP value
 */
public static int convertToDIP(int pixels) {
    return Math.round(TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, pixels,
            Resources.getSystem().getDisplayMetrics()));
}

From source file:Main.java

public static int getDPI(DisplayMetrics dm, int sizeInDp) {
    return (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, sizeInDp, dm);
}

From source file:Main.java

/**
 * @param dp Desired size in dp (density-independent pixels)
 * @param v View/* w w w . j  a va  2s  .  c o  m*/
 * @return Number of corresponding density-dependent pixels for the given device
 */
static int getDP(int dp, View v) {
    return (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, dp,
            v.getResources().getDisplayMetrics());
}

From source file:Main.java

/**
 * Given a value in DIP (density per pixel) uses the display metrics of the device to return the value in pixels.
 */// ww  w.j ava2 s. c  o  m
public static int valueInDipToPixels(Context context, float valueInDip) {
    return (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, valueInDip,
            context.getResources().getDisplayMetrics());
}

From source file:Main.java

/**
 * Convert Density pixels to normal pixels
 *
 * @param context Context/*from  w ww  . j  a v a2s .c o  m*/
 * @param dp      Density pixels
 * @return Integer
 */
public static int getPixelsFromDp(Context context, int dp) {
    Resources r = context.getResources();
    return (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, dp, r.getDisplayMetrics());
}

From source file:Main.java

/**
 * Gets the default target radius (in pixels). This is the radius of the
 * circular area that can be touched in order to activate the handle.
 * //from www .j a va  2  s  .c  o  m
 * @param context the Context
 * @return the target radius (in pixels)
 */
public static float getTargetRadius(Context context) {

    final float targetRadius = TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, TARGET_RADIUS_DP,
            context.getResources().getDisplayMetrics());
    return targetRadius;
}

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./*from  w  w  w .  j  a va  2 s. 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:Main.java

/**
 * Creates the Paint object for drawing the crop window border.
 * /*from  w  w w . j  av a  2  s . c  o  m*/
 * @param context the Context
 * @return new Paint object
 */
public static Paint newBorderPaint(Context context) {

    // Set the line thickness for the crop window border.
    final float lineThicknessPx = TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP,
            DEFAULT_LINE_THICKNESS_DP, context.getResources().getDisplayMetrics());

    final Paint borderPaint = new Paint();
    borderPaint.setColor(Color.parseColor(SEMI_TRANSPARENT));
    borderPaint.setStrokeWidth(lineThicknessPx);
    borderPaint.setStyle(Paint.Style.STROKE);

    return borderPaint;
}

From source file:Main.java

/**
 * Creates the Paint object for drawing the corners of the border
 * //w  w  w. j  a  v  a 2  s .c om
 * @param context the Context
 * @return the new Paint object
 */
public static Paint newCornerPaint(Context context) {

    // Set the line thickness for the crop window border.
    final float lineThicknessPx = TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP,
            DEFAULT_CORNER_THICKNESS_DP, context.getResources().getDisplayMetrics());

    final Paint cornerPaint = new Paint();
    cornerPaint.setColor(DEFAULT_CORNER_COLOR);
    cornerPaint.setStrokeWidth(lineThicknessPx);
    cornerPaint.setStyle(Paint.Style.STROKE);

    return cornerPaint;
}

From source file:Main.java

/**
 * Convert density pixel value to pixel for accurate scaling on different
 * screen densities/*from  w w  w  . j  a  va  2  s.c o  m*/
 * @param context
 * @param dipValue
 * @return
 */
public static float dipToPixels(Context context, float dipValue) {
    DisplayMetrics metrics = context.getResources().getDisplayMetrics();
    return TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, dipValue, metrics);
}