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

public static float convertDpToPixels(int dp) {
    DisplayMetrics metrics = Resources.getSystem().getDisplayMetrics();
    return TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, dp, metrics);
}

From source file:Main.java

/**
 * Convert Dp to Pixel// www  . j a va  2 s.  c  o  m
 */
@SuppressWarnings("unused")
public static int dpToPx(float dp, Resources resources) {
    float px = TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, dp, resources.getDisplayMetrics());
    return (int) px;
}

From source file:Main.java

public static float applyDimension(int unit, float value, DisplayMetrics metrics) {
    switch (unit) {
    case TypedValue.COMPLEX_UNIT_PX:
        return value;
    case TypedValue.COMPLEX_UNIT_DIP:
        return value * metrics.density;
    case TypedValue.COMPLEX_UNIT_SP:
        return value * metrics.scaledDensity;
    case TypedValue.COMPLEX_UNIT_PT:
        return value * metrics.xdpi * (1.0f / 72);
    case TypedValue.COMPLEX_UNIT_IN:
        return value * metrics.xdpi;
    case TypedValue.COMPLEX_UNIT_MM:
        return value * metrics.xdpi * (1.0f / 25.4f);
    }/*  ww  w . j  a va2 s.c  om*/
    return 0;
}

From source file:Main.java

public static float dipToPixels(float dipValue) {
    DisplayMetrics metrics = Resources.getSystem().getDisplayMetrics();
    return TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, dipValue, metrics);
}

From source file:Main.java

public static float pixelsToDips(float pixelValue) {
    DisplayMetrics metrics = Resources.getSystem().getDisplayMetrics();
    float dip = TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, 1, metrics);
    return pixelValue / dip;
}

From source file:Main.java

/**
 * This method converts dp unit to equivalent pixels, depending on device density.
 *
 * @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
 *//*from  www.  ja  v  a 2s . c o m*/
//    public static float convertDpToPixel(float dp, Context context){
//        Resources resources = context.getResources();
//        DisplayMetrics metrics = resources.getDisplayMetrics();
//        float px = dp * (metrics.densityDpi / 160f);
//        return px;
//    }
public static int getPixelsFromDP(int dp, Context context) {
    Resources r = context.getResources();
    return (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, dp, r.getDisplayMetrics());
}

From source file:Main.java

public static Bitmap getRoundedCornerBitmap(Bitmap bitmap, int color, int cornerDips, int borderDips,
        Context context, boolean recycleOrig) {
    Bitmap output = Bitmap.createBitmap(bitmap.getWidth(), bitmap.getHeight(), Bitmap.Config.ARGB_8888);
    Canvas canvas = new Canvas(output);

    final int borderSizePx = (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, (float) borderDips,
            context.getResources().getDisplayMetrics());
    final int cornerSizePx = (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, (float) cornerDips,
            context.getResources().getDisplayMetrics());
    final Paint paint = new Paint();
    final Rect rect = new Rect(0, 0, bitmap.getWidth(), bitmap.getHeight());
    final RectF rectF = new RectF(rect);

    // prepare canvas for transfer
    paint.setAntiAlias(true);//  w w  w. ja  v  a  2  s .  c om
    paint.setColor(0xFFFFFFFF);
    paint.setStyle(Paint.Style.FILL);
    canvas.drawARGB(0, 0, 0, 0);
    canvas.drawRoundRect(rectF, cornerSizePx, cornerSizePx, paint);

    // draw bitmap
    paint.setXfermode(new PorterDuffXfermode(PorterDuff.Mode.SRC_IN));
    canvas.drawBitmap(bitmap, rect, rect, paint);

    // draw border
    paint.setColor(color);
    paint.setStyle(Paint.Style.STROKE);
    paint.setStrokeWidth((float) borderSizePx);
    canvas.drawRoundRect(rectF, cornerSizePx, cornerSizePx, paint);

    if (recycleOrig && bitmap != null && !bitmap.isRecycled())
        bitmap.recycle();

    return output;
}

From source file:Main.java

public static int pixelsToDpi(Resources res, int pixels) {
    return (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, (float) pixels,
            res.getDisplayMetrics());/*from   www. j  av  a2  s .c om*/
}

From source file:Main.java

/**
 * Convert a dp float value to pixels/*ww  w.j  ava 2s.  c o m*/
 * @param context
 * @param dp
 * @return the responsive pixels
 */
public static int dp2px(Context context, float dp) {
    float px = TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, dp,
            context.getResources().getDisplayMetrics());
    return Math.round(px);
}

From source file:Main.java

public static Bitmap downSampleBitmap(Uri uri, Activity act, Boolean needRotate) {
    DisplayMetrics displaymetrics = new DisplayMetrics();
    act.getWindowManager().getDefaultDisplay().getMetrics(displaymetrics);
    Resources r = act.getResources();
    int px = (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, 50, r.getDisplayMetrics()); // 50: magic num
    int targetWidth = displaymetrics.heightPixels;
    int targetHeight = displaymetrics.widthPixels - px;

    Bitmap resizedBitmap = decodeSampledBitmap(uri, targetWidth, targetHeight, act);
    Bitmap returnBitmap = null;// w ww .j a v a2s .  c  o m
    ExifInterface exif;
    try {
        float degree = 0;
        exif = new ExifInterface(uri.toString());
        int orient = exif.getAttributeInt(ExifInterface.TAG_ORIENTATION, ExifInterface.ORIENTATION_NORMAL);
        if (resizedBitmap != null && needRotate) {
            degree = getDegree(orient);
            if (degree != 0) {
                returnBitmap = createRotatedBitmap(resizedBitmap, degree);
            }
            returnBitmap = returnBitmap == null ? resizedBitmap : returnBitmap;
        }
    } catch (IOException e) {
        Log.v(TAG, "not found file at downsample");
        e.printStackTrace();
    }
    return returnBitmap;
}