Example usage for android.text Layout getDesiredWidth

List of usage examples for android.text Layout getDesiredWidth

Introduction

In this page you can find the example usage for android.text Layout getDesiredWidth.

Prototype

public static float getDesiredWidth(CharSequence source, TextPaint paint) 

Source Link

Document

Return how wide a layout must be in order to display the specified text with one line per paragraph.

Usage

From source file:Main.java

public static float getDesiredWidth(String str, TextPaint paint) {
    float strWidth = Layout.getDesiredWidth(str, paint);
    return strWidth;
}

From source file:com.example.view.wheel.WheelView.java

/**
 * Calculates control width and creates text layouts
 * @param widthSize the input layout width
 * @param mode the layout mode//w ww.j ava  2s  .  c o  m
 * @return the calculated control width
 */
private int calculateLayoutWidth(int widthSize, int mode) {
    initResourcesIfNecessary();

    int width = widthSize;
    int maxLength = getMaxTextLength();
    if (maxLength > 0) {
        float textWidth = (float) Math.ceil(Layout.getDesiredWidth("0", itemsPaint));
        itemsWidth = (int) (maxLength * textWidth);
    } else {
        itemsWidth = 0;
    }
    itemsWidth += ADDITIONAL_ITEMS_SPACE; // make it some more

    labelWidth = 0;
    if (label != null && label.length() > 0) {
        labelWidth = (int) Math.ceil(Layout.getDesiredWidth(label, valuePaint));
    }

    boolean recalculate = false;
    if (mode == MeasureSpec.EXACTLY) {
        width = widthSize;
        recalculate = true;
    } else {
        width = itemsWidth + labelWidth + 2 * PADDING;
        if (labelWidth > 0) {
            width += LABEL_OFFSET;
        }

        // Check against our minimum width
        width = Math.max(width, getSuggestedMinimumWidth());

        if (mode == MeasureSpec.AT_MOST && widthSize < width) {
            width = widthSize;
            recalculate = true;
        }
    }

    if (recalculate) {
        // recalculate width
        int pureWidth = width - LABEL_OFFSET - 2 * PADDING;
        if (pureWidth <= 0) {
            itemsWidth = labelWidth = 0;
        }
        if (labelWidth > 0) {
            double newWidthItems = (double) itemsWidth * pureWidth / (itemsWidth + labelWidth);
            itemsWidth = (int) newWidthItems;
            labelWidth = pureWidth - itemsWidth;
        } else {
            itemsWidth = pureWidth + LABEL_OFFSET; // no label
        }
    }

    if (itemsWidth > 0) {
        createLayouts(itemsWidth, labelWidth);
    }

    return width;
}

From source file:com.dongdong.wheel.WheelView.java

/**
 * Calculates control width and creates text layouts
 *
 * @param widthSize the input layout width
 * @param mode      the layout mode/*  w  w w .  ja v a 2 s . co  m*/
 * @return the calculated control width
 */
private int calculateLayoutWidth(int widthSize, int mode) {
    initResourcesIfNecessary();

    int width = widthSize;

    int maxLength = getMaxTextLength();
    if (maxLength > 0) {
        float textWidth = (float) Math.ceil(Layout.getDesiredWidth("0", mItemsPaint));
        mItemsWidth = (int) (maxLength * textWidth);
    } else {
        mItemsWidth = 0;
    }
    mItemsWidth += ADDITIONAL_ITEMS_SPACE; // make it some more

    mLabelWidth = 0;
    if (mLabel != null && mLabel.length() > 0) {
        mLabelWidth = (int) (float) Math.ceil(Layout.getDesiredWidth(mLabel, mValuePaint));
    }

    boolean recalculate = false;
    if (mode == MeasureSpec.EXACTLY) {
        width = widthSize;
        recalculate = true;
    } else {
        width = mItemsWidth + mLabelWidth + 2 * PADDING;
        if (mLabelWidth > 0) {
            width += LABEL_OFFSET;
        }

        // Check against our minimum width
        width = Math.max(width, getSuggestedMinimumWidth());

        if (mode == MeasureSpec.AT_MOST && widthSize < width) {
            width = widthSize;
            recalculate = true;
        }
    }

    if (recalculate) {
        // recalculate width
        int pureWidth = width - LABEL_OFFSET - 2 * PADDING;
        if (pureWidth <= 0) {
            mItemsWidth = mLabelWidth = 0;
        }
        if (mLabelWidth > 0) {
            double newWidthItems = (double) mItemsWidth * pureWidth / (mItemsWidth + mLabelWidth);
            mItemsWidth = (int) newWidthItems;
            mLabelWidth = pureWidth - mItemsWidth;
        } else {
            mItemsWidth = pureWidth + LABEL_OFFSET; // no mLabel
        }
    }

    if (mItemsWidth > 0) {
        createLayouts(mItemsWidth, mLabelWidth);
    }

    return width;
}

From source file:com.taobao.weex.dom.WXTextDomObject.java

/**
 * Get text width according to constrain of outerWidth with and forceToDesired
 * @param textPaint paint used to measure text
 * @param outerWidth the width that css-layout desired.
 * @param forceToDesired if set true, the return value will be outerWidth, no matter what the width
 *                   of text is.//from  www  . ja v a 2 s.  c o  m
 * @return if forceToDesired is false, it will be the minimum value of the width of text and
 * outerWidth in case of outerWidth is defined, in other case, it will be outer width.
 */
float getTextWidth(TextPaint textPaint, float outerWidth, boolean forceToDesired) {
    if (mText == null) {
        if (forceToDesired) {
            return outerWidth;
        }
        return 0;
    }
    float textWidth;
    if (forceToDesired) {
        textWidth = outerWidth;
    } else {
        float desiredWidth = Layout.getDesiredWidth(spanned, textPaint);
        if (CSSConstants.isUndefined(outerWidth) || desiredWidth < outerWidth) {
            textWidth = desiredWidth;
        } else {
            textWidth = outerWidth;
        }
    }
    return textWidth;
}

From source file:android.support.v7.widget.SwitchCompat.java

private Layout makeLayout(CharSequence text) {
    final CharSequence transformed = (mSwitchTransformationMethod != null)
            ? mSwitchTransformationMethod.getTransformation(text, this)
            : text;/*from   w  ww .  ja  va 2s.  co  m*/

    return new StaticLayout(transformed, mTextPaint,
            transformed != null ? (int) Math.ceil(Layout.getDesiredWidth(transformed, mTextPaint)) : 0,
            Layout.Alignment.ALIGN_NORMAL, 1.f, 0, true);
}

From source file:com.mixiaoxiao.support.widget.SmoothSwitch.java

private Layout makeLayout(CharSequence text) {
    final CharSequence transformed = (mSwitchTransformationMethod != null)
            ? mSwitchTransformationMethod.getTransformation(text, this)
            : text;/*from   w  w w .ja v  a 2s.c  o  m*/

    return new StaticLayout(transformed, mTextPaint,
            (int) Math.ceil(Layout.getDesiredWidth(transformed, mTextPaint)), Layout.Alignment.ALIGN_NORMAL,
            1.f, 0, true);
}