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

/**
 * Creates the Paint object for drawing the corners of the border
 * /*from www . ja  v a  2s. c  o m*/
 * @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 v a 2s.  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);
}

From source file:Main.java

/**
 * Use touch-icon or higher-resolution favicon and round the corners.
 * @param context    Context used to get resources.
 * @param touchIcon  Touch icon bitmap.//from   ww  w.  j av  a 2 s.c o m
 * @param canvas     Canvas that holds the touch icon.
 */
private static void drawTouchIconToCanvas(Context context, Bitmap touchIcon, Canvas canvas) {
    Rect iconBounds = new Rect(0, 0, canvas.getWidth(), canvas.getHeight());
    Rect src = new Rect(0, 0, touchIcon.getWidth(), touchIcon.getHeight());
    Paint paint = new Paint(Paint.ANTI_ALIAS_FLAG);
    paint.setFilterBitmap(true);
    canvas.drawBitmap(touchIcon, src, iconBounds, paint);
    // Convert dp to px.
    int borderRadii = (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, TOUCHICON_BORDER_RADII,
            context.getResources().getDisplayMetrics());
    Path path = new Path();
    path.setFillType(Path.FillType.INVERSE_WINDING);
    RectF rect = new RectF(iconBounds);
    rect.inset(INSET_DIMENSION_FOR_TOUCHICON, INSET_DIMENSION_FOR_TOUCHICON);
    path.addRoundRect(rect, borderRadii, borderRadii, Path.Direction.CW);
    paint.setXfermode(new PorterDuffXfermode(PorterDuff.Mode.CLEAR));
    canvas.drawPath(path, paint);
}

From source file:Main.java

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

From source file:com.andryr.guitartuner.Utils.java

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

From source file:Main.java

public static Bitmap createRoundedBitmap(Context context, Bitmap bitmap, int leftTop, int rightTop,
        int leftBottm, int rightBottom) {
    Bitmap bmp;/*from w w  w.  java 2 s. c o m*/

    bmp = Bitmap.createBitmap(bitmap.getWidth(), bitmap.getHeight(), Bitmap.Config.ARGB_8888);
    BitmapShader shader = new BitmapShader(bitmap, BitmapShader.TileMode.CLAMP, BitmapShader.TileMode.CLAMP);

    float radius = TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, leftTop,
            context.getResources().getDisplayMetrics());
    Canvas canvas = new Canvas(bmp);
    Paint paint = new Paint();
    paint.setAntiAlias(true);
    paint.setShader(shader);

    RectF rect = new RectF(0, 0, bitmap.getWidth(), bitmap.getHeight());
    canvas.drawRoundRect(rect, radius, radius, paint);
    return bmp;
}

From source file:Main.java

/**
 * This method converts dp unit to equivalent pixels, depending on device
 * density./*  www  .  j  a v a  2s  . c o m*/
 * 
 * @param dp
 *            A value in dp (density independent pixels) unit. Which we need
 *            to convert into pixels
 * @param context
 *            Context to get resources and device specific display metrics
 * @return A float value to represent px equivalent to dp depending on
 *         device density
 */
public static int convertDpToPixel(float dp, Context context) {
    // Resources resources = context.getResources();
    // DisplayMetrics metrics = resources.getDisplayMetrics();
    // float px = dp * (metrics.densityDpi / 160f);
    // return px;
    return (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, dp,
            context.getResources().getDisplayMetrics());
}

From source file:com.carlrice.reader.utils.UiUtils.java

static public int dpToPixel(int dp) {
    return (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, dp,
            Application.context().getResources().getDisplayMetrics());
}

From source file:com.roxbyte.formality.util.FormUtil.java

public static int convertToPx(int dp, Resources resources) {
    int px = (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, dp, resources.getDisplayMetrics());
    return px;//from w ww  .  j av  a2s .co  m
}

From source file:com.activiti.android.ui.utils.UIUtils.java

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