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

/**
 * Returns a density independent pixel value rounded to the nearest integer
 * for the desired dimension.//from  w w  w .j  a va2s.  co m
 *
 * @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

public static float getRawSize(Context context, int unit, float size) {
    Resources r;//w ww . j av  a2 s.  c om

    if (context == null)
        r = Resources.getSystem();
    else
        r = context.getResources();

    return TypedValue.applyDimension(unit, size, r.getDisplayMetrics());
}

From source file:Main.java

/**
 * @param dp Desired size in dp (density-independent pixels)
 * @param v View/*from   ww w  .  j  av a2s  .  com*/
 * @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.
 *//*from   www.ja v  a  2 s . co 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/* w ww  . j av  a 2 s . 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.
 * //  w  ww.  j av  a 2  s.  co 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./* w w w.j a v  a2 s.co 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

/**
 * Recursive binary search to find the best size for the text.
 *///from  w  ww  . ja v  a  2s . com
private static float getAutofitTextSize(CharSequence text, TextPaint paint, float targetWidth, int maxLines,
        float low, float high, float precision, DisplayMetrics displayMetrics) {
    float mid = (low + high) / 2.0f;
    int lineCount = 1;
    StaticLayout layout = null;

    paint.setTextSize(TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_PX, mid, displayMetrics));

    if (maxLines != 1) {
        layout = new StaticLayout(text, paint, (int) targetWidth, Layout.Alignment.ALIGN_NORMAL, 1.0f, 0.0f,
                true);
        lineCount = layout.getLineCount();
    }

    if (SPEW)
        Log.d(TAG, "low=" + low + " high=" + high + " mid=" + mid + " target=" + targetWidth + " maxLines="
                + maxLines + " lineCount=" + lineCount);

    if (lineCount > maxLines) {
        // For the case that `text` has more newline characters than `maxLines`.
        if ((high - low) < precision) {
            return low;
        }
        return getAutofitTextSize(text, paint, targetWidth, maxLines, low, mid, precision, displayMetrics);
    } else if (lineCount < maxLines) {
        return getAutofitTextSize(text, paint, targetWidth, maxLines, mid, high, precision, displayMetrics);
    } else {
        float maxLineWidth = 0;
        if (maxLines == 1) {
            maxLineWidth = paint.measureText(text, 0, text.length());
        } else {
            for (int i = 0; i < lineCount; i++) {
                if (layout.getLineWidth(i) > maxLineWidth) {
                    maxLineWidth = layout.getLineWidth(i);
                }
            }
        }

        if ((high - low) < precision) {
            return low;
        } else if (maxLineWidth > targetWidth) {
            return getAutofitTextSize(text, paint, targetWidth, maxLines, low, mid, precision, displayMetrics);
        } else if (maxLineWidth < targetWidth) {
            return getAutofitTextSize(text, paint, targetWidth, maxLines, mid, high, precision, displayMetrics);
        } else {
            return mid;
        }
    }
}

From source file:Main.java

/**
 * Creates the Paint object for drawing the crop window border.
 * /*from w  w w.  ja  v  a2 s  .c om*/
 * @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;
}