Example usage for android.util TypedValue COMPLEX_UNIT_MM

List of usage examples for android.util TypedValue COMPLEX_UNIT_MM

Introduction

In this page you can find the example usage for android.util TypedValue COMPLEX_UNIT_MM.

Prototype

int COMPLEX_UNIT_MM

To view the source code for android.util TypedValue COMPLEX_UNIT_MM.

Click Source Link

Document

#TYPE_DIMENSION complex unit: Value is in millimeters.

Usage

From source file:Main.java

public static int mm2px(Context context, int mm) {
    return (int) (TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_MM, mm,
            context.getResources().getDisplayMetrics()) + 0.5f);
}

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);
    }/*from   ww w  .j a  v a 2  s .  com*/
    return 0;
}

From source file:org.appcelerator.titanium.util.TiUIHelper.java

public static int getSizeUnits(final String size) {
    int units = TypedValue.COMPLEX_UNIT_PX;
    String unitString = null;/*from   www . j  av a 2 s . com*/

    if (size != null) {
        Matcher m = SIZED_VALUE.matcher(size.trim());
        if (m.matches()) {
            if (m.groupCount() == 2) {
                unitString = m.group(2);
            }
        }
    }

    if (unitString == null) {
        unitString = TiApplication.getInstance().getDefaultUnit();
    }

    if (TiDimension.UNIT_PX.equals(unitString) || TiDimension.UNIT_SYSTEM.equals(unitString)) {
        units = TypedValue.COMPLEX_UNIT_PX;
    } else if (TiDimension.UNIT_PT.equals(unitString)) {
        units = TypedValue.COMPLEX_UNIT_PT;
    } else if (TiDimension.UNIT_DP.equals(unitString) || TiDimension.UNIT_DIP.equals(unitString)) {
        units = TypedValue.COMPLEX_UNIT_DIP;
    } else if (TiDimension.UNIT_SP.equals(unitString) || TiDimension.UNIT_SIP.equals(unitString)) {
        units = TypedValue.COMPLEX_UNIT_SP;
    } else if (TiDimension.UNIT_MM.equals(unitString)) {
        units = TypedValue.COMPLEX_UNIT_MM;
    } else if (TiDimension.UNIT_CM.equals(unitString)) {
        units = TiDimension.COMPLEX_UNIT_CM;
    } else if (TiDimension.UNIT_IN.equals(unitString)) {
        units = TypedValue.COMPLEX_UNIT_IN;
    } else {
        if (unitString != null) {
            Log.w(TAG, "Unknown unit: " + unitString, Log.DEBUG_MODE);
        }
    }

    return units;
}

From source file:org.appcelerator.titanium.util.TiUIHelper.java

public static void getSizeAndUnits(final String size, final float[] result) {
    int units = TypedValue.COMPLEX_UNIT_PX;
    float value = 15.0f;
    String unitString = null;/*  w  w w . ja va  2s. c o  m*/

    if (size != null) {
        Matcher m = SIZED_VALUE.matcher(size.trim());
        if (m.matches()) {
            if (m.groupCount() == 2) {
                unitString = m.group(2);
                value = Float.parseFloat(m.group(1));
            }
        }
    }

    if (unitString == null) {
        unitString = TiApplication.getInstance().getDefaultUnit();
    }

    if (TiDimension.UNIT_PX.equals(unitString) || TiDimension.UNIT_SYSTEM.equals(unitString)) {
        units = TypedValue.COMPLEX_UNIT_PX;
    } else if (TiDimension.UNIT_PT.equals(unitString)) {
        units = TypedValue.COMPLEX_UNIT_PT;
    } else if (TiDimension.UNIT_DP.equals(unitString) || TiDimension.UNIT_DIP.equals(unitString)) {
        units = TypedValue.COMPLEX_UNIT_DIP;
    } else if (TiDimension.UNIT_SP.equals(unitString) || TiDimension.UNIT_SIP.equals(unitString)) {
        units = TypedValue.COMPLEX_UNIT_SP;
    } else if (TiDimension.UNIT_MM.equals(unitString)) {
        units = TypedValue.COMPLEX_UNIT_MM;
    } else if (TiDimension.UNIT_CM.equals(unitString)) {
        units = TypedValue.COMPLEX_UNIT_MM;
        value *= 10;
    } else if (TiDimension.UNIT_IN.equals(unitString)) {
        units = TypedValue.COMPLEX_UNIT_IN;
    } else {
        if (unitString != null) {
            Log.w(TAG, "Unknown unit: " + unitString, Log.DEBUG_MODE);
        }
    }
    result[0] = units;
    result[1] = value;
}

From source file:com.nextgis.mobile.fragment.MapFragment.java

protected void drawScaleRuler() {
    int px = (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_MM, 10,
            getResources().getDisplayMetrics());
    int notch = (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_MM, 1,
            getResources().getDisplayMetrics());
    Bitmap ruler = Bitmap.createBitmap(px, px, Bitmap.Config.ARGB_8888);
    Canvas canvas = new Canvas(ruler);
    Paint paint = new Paint(Paint.ANTI_ALIAS_FLAG);
    paint.setColor(ContextCompat.getColor(getActivity(), R.color.primary_dark));
    paint.setStrokeWidth(4);//  ww  w  . ja va 2s.  c o  m
    paint.setStyle(Paint.Style.STROKE);
    canvas.drawLine(0, px, px, px, paint);
    canvas.drawLine(0, px, 0, 0, paint);
    canvas.drawLine(0, 0, notch, 0, paint);
    canvas.drawLine(px, px, px, px - notch, paint);
    mScaleRuler.setImageBitmap(ruler);
}

From source file:self.philbrown.droidQuery.$.java

/**
 * Interprets the CSS-style String and sets the value
 * @param view the view that will change.
 * @param key the name of the attribute/*from ww w .j  a  v a2s. c o  m*/
 * @param _value the end animation value
 * @return the computed value
 */
public Number getAnimationValue(View view, String key, String _value) {
    Number value = null;

    boolean negativeValue = false;
    if (_value.startsWith("-")) {
        negativeValue = true;
        _value = _value.substring(1);
    }

    String[] split = (_value).split("(?<=\\D)(?=\\d)|(?<=\\d)(?=\\D)");
    if (negativeValue)
        split[0] = String.format(Locale.US, "-%s", split[0]);
    if (split.length == 1) {
        if (split[0].contains(".")) {
            value = Float.valueOf(split[0]);
        } else {
            value = Integer.valueOf(split[0]);
        }
    } else {
        if (split.length > 2) {
            Log.w("droidQuery", "parsererror for key " + key);
            return null;
        }
        if (split[1].equalsIgnoreCase("px")) {
            //this is the default. Just determine if float or int
            if (split[0].contains(".")) {
                value = Float.valueOf(split[0]);
            } else {
                value = Integer.valueOf(split[0]);
            }
        } else if (split[1].equalsIgnoreCase("dip") || split[1].equalsIgnoreCase("dp")) {
            float px = TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, Float.parseFloat(split[0]),
                    context.getResources().getDisplayMetrics());
            if (split[0].contains(".")) {
                value = px;
            } else {
                value = (int) px;
            }
        } else if (split[1].equalsIgnoreCase("in")) {
            float px = TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_IN, Float.parseFloat(split[0]),
                    context.getResources().getDisplayMetrics());
            if (split[0].contains(".")) {
                value = px;
            } else {
                value = (int) px;
            }
        } else if (split[1].equalsIgnoreCase("mm")) {
            float px = TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_MM, Float.parseFloat(split[0]),
                    context.getResources().getDisplayMetrics());
            if (split[0].contains(".")) {
                value = px;
            } else {
                value = (int) px;
            }
        } else if (split[1].equalsIgnoreCase("pt")) {
            float px = TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_PT, Float.parseFloat(split[0]),
                    context.getResources().getDisplayMetrics());
            if (split[0].contains(".")) {
                value = px;
            } else {
                value = (int) px;
            }
        } else if (split[1].equalsIgnoreCase("sp")) {
            float px = TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_SP, Float.parseFloat(split[0]),
                    context.getResources().getDisplayMetrics());
            if (split[0].contains(".")) {
                value = px;
            } else {
                value = (int) px;
            }
        } else if (split[1].equals("%")) {
            ViewParent parent = view.getParent();
            float pixels = 0;
            if (parent == null || !(parent instanceof View)) {
                pixels = context.getResources().getDisplayMetrics().widthPixels;
                //use best guess for width or height dpi
                if (split[0].equalsIgnoreCase("y") || split[0].equalsIgnoreCase("top")
                        || split[0].equalsIgnoreCase("bottom")) {
                    pixels = context.getResources().getDisplayMetrics().heightPixels;
                }
            } else {
                pixels = ((View) parent).getWidth();
                if (split[0].equalsIgnoreCase("y") || split[0].equalsIgnoreCase("top")
                        || split[0].equalsIgnoreCase("bottom")) {
                    pixels = ((View) parent).getHeight();
                }
            }
            float percent = 0;
            if (pixels != 0)
                percent = Float.valueOf(split[0]) / 100 * pixels;
            if (split[0].contains(".")) {
                value = percent;
            } else {
                value = (int) percent;
            }
        } else {
            Log.w("droidQuery", "invalid units for Object with key " + key);
            return null;
        }
    }
    return value;
}