Example usage for android.util TypedValue COMPLEX_UNIT_IN

List of usage examples for android.util TypedValue COMPLEX_UNIT_IN

Introduction

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

Prototype

int COMPLEX_UNIT_IN

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

Click Source Link

Document

#TYPE_DIMENSION complex unit: Value is in inches.

Usage

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 av  a2s.com*/
    return 0;
}

From source file:com.hp.mss.printsdksample.fragment.TabFragmentPrintLayout.java

private void createUserSelectedImageJobData() {
    Bitmap userPickedBitmap;/*from  w w  w . ja va 2 s . co m*/

    try {
        userPickedBitmap = MediaStore.Images.Media.getBitmap(getActivity().getContentResolver(), userPickedUri);
        int width = userPickedBitmap.getWidth();
        int height = userPickedBitmap.getHeight();

        // if user picked bitmap is too big, just reduce the size, so it will not chock the print plugin
        if (width * height > 5000) {
            width = width / 2;
            height = height / 2;
            userPickedBitmap = Bitmap.createScaledBitmap(userPickedBitmap, width, height, true);
        }

        DisplayMetrics mDisplayMetric = getActivity().getResources().getDisplayMetrics();
        float widthInches = TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_IN, width, mDisplayMetric);
        float heightInches = TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_IN, height, mDisplayMetric);

        ImageAsset imageAsset = new ImageAsset(getActivity(), userPickedBitmap,
                ImageAsset.MeasurementUnits.INCHES, widthInches, heightInches);

        PrintItem printItem4x6 = new ImagePrintItem(PrintAttributes.MediaSize.NA_INDEX_4X6, margins, scaleType,
                imageAsset);
        PrintItem printItem85x11 = new ImagePrintItem(PrintAttributes.MediaSize.NA_LETTER, margins, scaleType,
                imageAsset);
        PrintItem printItem5x7 = new ImagePrintItem(mediaSize5x7, margins, scaleType, imageAsset);

        printJobData = new PrintJobData(getActivity(), printItem4x6);
        printJobData.addPrintItem(printItem85x11);
        printJobData.addPrintItem(printItem5x7);
    } catch (IOException e) {
        e.printStackTrace();
    }

}

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;/*  w  ww.  j a v  a 2  s. c o  m*/

    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  .  j a v  a 2 s . c  om*/

    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: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// ww w . j a v a  2 s .  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;
}