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

public static TextPaint setTextSize(Context c, TextPaint paint, float size) {
    Resources r;//  w w w . j  a  va 2  s . co  m
    if (c == null) {
        r = Resources.getSystem();
    } else {
        r = c.getResources();
    }
    if (r != null) {
        paint.setTextSize(TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_SP, size, r.getDisplayMetrics()));
    }
    return paint;
}

From source file:Main.java

/**
 * This method converts dp to pixels//from   w  w w  . ja v a2s.  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

/**
 * sp to px/*  ww  w  .  ja  v a2s .  c o  m*/
 * @param context
 * @param px
 * @return
 */
public static int sp2px(Context context, int px) {
    return (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_SP, px,
            context.getResources().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 w w  .  jav  a  2 s.  c  om*/
 */
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 ww. j  a va2 s  .c  o m*/
 * @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/*from  w w w  .j av 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;
}

From source file:Main.java

public static void expand(final View v, Activity activity) {
    v.measure(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT);
    Resources r = activity.getResources();
    final float px = TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, 200, r.getDisplayMetrics());
    final int targtetHeight = (int) px;//200;//v.getMeasuredHeight();

    v.getLayoutParams().height = 0;//w  w  w. jav  a 2  s  . c o  m
    v.setVisibility(View.VISIBLE);
    Animation a = new Animation() {
        @Override
        protected void applyTransformation(float interpolatedTime, Transformation t) {
            v.getLayoutParams().height = interpolatedTime == 1 ? ((int) px)//200//ViewGroup.LayoutParams.WRAP_CONTENT
                    : (int) (targtetHeight * interpolatedTime);
            v.requestLayout();
        }

        @Override
        public boolean willChangeBounds() {
            return true;
        }
    };

    a.setDuration((int) (targtetHeight / v.getContext().getResources().getDisplayMetrics().density));
    v.startAnimation(a);
}

From source file:Main.java

/**
 * Converts the passed float value (dip) into pixels based on the resource
 * @param res the Resources to use for retrieving display metrics
 * @param value the dip value to convert
 * @return the passed dip's pixel equivalent.
 * //from   w w w.j  a  v  a 2 s. c o  m
 * @deprecated use getResources().getDimensionPixelSize(R.dimen.your_dimension_to_convert);
 */
@Deprecated
public static float convertFromDip(final Resources res, final float value) {
    return TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, value, res.getDisplayMetrics());
}

From source file:Main.java

public static Bitmap createRoundedBitmap(Context context, Bitmap bitmap, int radiusDP) {
    Bitmap bmp;/* w ww  .j  av  a2 s.  co 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, radiusDP,
            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

private static int getLineCount(CharSequence text, TextPaint paint, float size, float width,
        DisplayMetrics displayMetrics) {
    paint.setTextSize(TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_PX, size, displayMetrics));
    StaticLayout layout = new StaticLayout(text, paint, (int) width, Layout.Alignment.ALIGN_NORMAL, 1.0f, 0.0f,
            true);/*from w ww .  j av a 2s  . com*/
    return layout.getLineCount();
}