Example usage for android.text StaticLayout StaticLayout

List of usage examples for android.text StaticLayout StaticLayout

Introduction

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

Prototype

@Deprecated
public StaticLayout(CharSequence source, TextPaint paint, int width, Alignment align, float spacingmult,
        float spacingadd, boolean includepad) 

Source Link

Usage

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

/**
 * Update layout according to {@link #mText} and span
 * @param width the specified width./*from  w w  w  .  ja v  a 2s  .  c om*/
 * @param forceWidth If true, force the text width to the specified width, otherwise, text width
 *                   may equals to or be smaller than the specified width.
 * @param previousLayout the result of previous layout, could be null.
 */
private @NonNull Layout createLayout(float width, boolean forceWidth, @Nullable Layout previousLayout) {
    float textWidth;
    textWidth = getTextWidth(mTextPaint, width, forceWidth);
    Layout layout;
    if (!FloatUtil.floatsEqual(previousWidth, textWidth) || previousLayout == null) {
        boolean forceRtl = false;
        Object direction = getStyles().get(Constants.Name.DIRECTION);
        if (direction != null && "text".equals(mType)) {
            forceRtl = direction.equals(Constants.Name.RTL);
        }
        layout = StaticLayoutProxy.create(spanned, mTextPaint, (int) Math.ceil(textWidth),
                Layout.Alignment.ALIGN_NORMAL, 1, 0, false, forceRtl);
    } else {
        layout = previousLayout;
    }
    if (mNumberOfLines != UNSET && mNumberOfLines > 0 && mNumberOfLines < layout.getLineCount()) {
        int lastLineStart, lastLineEnd;
        lastLineStart = layout.getLineStart(mNumberOfLines - 1);
        lastLineEnd = layout.getLineEnd(mNumberOfLines - 1);
        if (lastLineStart < lastLineEnd) {
            SpannableStringBuilder builder = null;
            if (lastLineStart > 0) {
                builder = new SpannableStringBuilder(spanned.subSequence(0, lastLineStart));
            } else {
                builder = new SpannableStringBuilder();
            }
            Editable lastLine = new SpannableStringBuilder(spanned.subSequence(lastLineStart, lastLineEnd));
            builder.append(truncate(lastLine, mTextPaint, (int) Math.ceil(textWidth), textOverflow));
            adjustSpansRange(spanned, builder);
            spanned = builder;
            return new StaticLayout(spanned, mTextPaint, (int) Math.ceil(textWidth),
                    Layout.Alignment.ALIGN_NORMAL, 1, 0, false);
        }
    }
    return layout;
}

From source file:com.ifoer.util.NetPOSPrinter.java

public Bitmap drawBitSecond(String s) {
    if (this.company_address == null) {
        this.company_address = XmlPullParser.NO_NAMESPACE;
    }/*from w  w w.  j av a  2 s  .  c  o  m*/
    if (this.company_phone == null) {
        this.company_phone = XmlPullParser.NO_NAMESPACE;
    }
    if (this.license_plate_number == null) {
        this.license_plate_number = XmlPullParser.NO_NAMESPACE;
    }
    StringBuffer ss = new StringBuffer();
    ss.append(new StringBuilder(
            String.valueOf(this.mContext.getResources().getString(C0136R.string.print_test_time)))
                    .append(this.str).append(SpecilApiUtil.LINE_SEP).toString());
    ss.append(new StringBuilder(
            String.valueOf(this.mContext.getResources().getString(C0136R.string.print_serial_number)))
                    .append(this.serialNum).append(SpecilApiUtil.LINE_SEP).toString());
    ss.append(new StringBuilder(
            String.valueOf(this.mContext.getResources().getString(C0136R.string.print_test_company_address)))
                    .append(this.company_address).append(SpecilApiUtil.LINE_SEP).toString());
    ss.append(new StringBuilder(
            String.valueOf(this.mContext.getResources().getString(C0136R.string.print_test_company_phone)))
                    .append(this.company_phone).append(SpecilApiUtil.LINE_SEP).toString());
    ss.append(new StringBuilder(String
            .valueOf(this.mContext.getResources().getString(C0136R.string.print_test_license_plate_number)))
                    .append(this.license_plate_number).append(SpecilApiUtil.LINE_SEP).toString());
    ss.append(s);
    this.textPaint.setColor(DefaultRenderer.BACKGROUND_COLOR);
    this.textPaint.setTextSize(20.0f);
    StaticLayout layout = new StaticLayout(ss, this.textPaint, PRINT_WIDTH, Alignment.ALIGN_NORMAL, 1.0f, 0.0f,
            true);
    this.nBitmapSecond = Bitmap.createBitmap(PRINT_WIDTH, layout.getHeight(), Config.RGB_565);
    Canvas canvas = new Canvas(this.nBitmapSecond);
    canvas.drawColor(-1);
    layout.draw(canvas);
    return this.nBitmapSecond;
}

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

/**
 * Truncate the source span to the specified lines.
 * Caller of this method must ensure that the lines of text is <strong>greater than desired lines and need truncate</strong>.
 * Otherwise, unexpected behavior may happen.
 * @param source The source span.// w  w  w  .  j  a  v a2  s. com
 * @param paint the textPaint
 * @param desired specified lines.
 * @param truncateAt truncate method, null value means clipping overflow text directly, non-null value means using ellipsis strategy to clip
 * @return The spans after clipped.
 */
private @NonNull Spanned truncate(@Nullable Editable source, @NonNull TextPaint paint, int desired,
        @Nullable TextUtils.TruncateAt truncateAt) {
    Spanned ret = new SpannedString("");
    if (!TextUtils.isEmpty(source) && source.length() > 0) {
        if (truncateAt != null) {
            source.append(ELLIPSIS);
            Object[] spans = source.getSpans(0, source.length(), Object.class);
            for (Object span : spans) {
                int start = source.getSpanStart(span);
                int end = source.getSpanEnd(span);
                if (start == 0 && end == source.length() - 1) {
                    source.removeSpan(span);
                    source.setSpan(span, 0, source.length(), source.getSpanFlags(span));
                }
            }
        }

        StaticLayout layout;
        int startOffset;

        while (source.length() > 1) {
            startOffset = source.length() - 1;
            if (truncateAt != null) {
                startOffset -= 1;
            }
            source.delete(startOffset, startOffset + 1);
            layout = new StaticLayout(source, paint, desired, Layout.Alignment.ALIGN_NORMAL, 1, 0, false);
            if (layout.getLineCount() <= 1) {
                ret = source;
                break;
            }
        }
    }
    return ret;
}

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

/**
 * Creates layouts/*from   w  ww .  ja v  a  2  s . c  o m*/
 *
 * @param widthItems width of items layout
 * @param widthLabel width of mLabel layout
 */
private void createLayouts(int widthItems, int widthLabel) {
    if (mItemsLayout == null || mItemsLayout.getWidth() > widthItems) {
        mItemsLayout = new StaticLayout(buildText(isScrollingPerformed), mItemsPaint, widthItems,
                widthLabel > 0 ? Layout.Alignment.ALIGN_OPPOSITE : Layout.Alignment.ALIGN_CENTER, 1,
                ADDITIONAL_ITEM_HEIGHT, false);
    } else {
        mItemsLayout.increaseWidthTo(widthItems);
    }

    if (!isScrollingPerformed && (mValueLayout == null || mValueLayout.getWidth() > widthItems)) {
        String text = getAdapter() != null ? getAdapter().getItem(mCurrentItem) : null;
        mValueLayout = new StaticLayout(text != null ? text : "", mValuePaint, widthItems,
                widthLabel > 0 ? Layout.Alignment.ALIGN_OPPOSITE : Layout.Alignment.ALIGN_CENTER, 1,
                ADDITIONAL_ITEM_HEIGHT, false);
    } else if (isScrollingPerformed) {
        mValueLayout = null;
    } else {
        mValueLayout.increaseWidthTo(widthItems);
    }

    if (widthLabel > 0) {
        if (mLabelLayout == null || mLabelLayout.getWidth() > widthLabel) {
            mLabelLayout = new StaticLayout(mLabel, mValuePaint, widthLabel, Layout.Alignment.ALIGN_NORMAL, 1,
                    ADDITIONAL_ITEM_HEIGHT, false);
        } else {
            mLabelLayout.increaseWidthTo(widthLabel);
        }
    }
}

From source file:com.semfapp.adamdilger.semf.Take5PdfDocument.java

public void drawText(String string, float xLoc, float yLoc, float textSize, Typeface typeface) {

    if (string != null) {
        TextPaint mTextPaint = new TextPaint();
        mTextPaint.setTypeface(typeface);
        mTextPaint.setTextSize(textSize);

        StaticLayout mTextLayout = new StaticLayout(string, mTextPaint, ((int) (595 * 0.28)),
                Layout.Alignment.ALIGN_NORMAL, 1.0f, 0.0f, false);

        can.save();/*  w  ww .j a  v a2  s  . c  o m*/
        // calculate x and y position where your text will be placed

        can.translate(xLoc, yLoc);
        mTextLayout.draw(can);
        can.restore();
    }
}

From source file:com.semfapp.adamdilger.semf.Take5PdfDocument.java

public float drawRiskElement(int height, Take5RiskElement element) {
    final float singleLineHeight = 12.5f;
    float totalItemHeight;

    TextPaint textPaint = new TextPaint();
    textPaint.setTypeface(roboto);//from  ww  w.  jav a 2s.co  m
    textPaint.setTextSize(FONT11);
    textPaint.setColor(Color.BLACK);

    String oneString = "";
    String twoString = "";

    if (element.getOne() != null) {
        oneString = element.getOne();
    }
    if (element.getTwo() != null) {
        twoString = element.getTwo();
    }

    StaticLayout one = new StaticLayout(oneString, textPaint, 235, Layout.Alignment.ALIGN_NORMAL, 1.0f, 0.0f,
            false);
    StaticLayout rating = new StaticLayout(element.getRating().toString(), textPaint, 100,
            Layout.Alignment.ALIGN_NORMAL, 1.0f, 0.0f, false);
    StaticLayout two = new StaticLayout(twoString, textPaint, 250, Layout.Alignment.ALIGN_NORMAL, 1.0f, 0.0f,
            false);

    if (one.getLineCount() > two.getLineCount()) {
        totalItemHeight = singleLineHeight * one.getLineCount();
    } else {
        totalItemHeight = singleLineHeight * two.getLineCount();
    }

    can.save();
    can.translate(15, height);
    one.draw(can);
    can.restore();

    can.save();
    can.translate(320, height);
    two.draw(can);
    can.restore();

    can.save();
    can.translate(270, height);
    rating.draw(can);
    can.restore();

    return totalItemHeight;
}

From source file:io.plaidapp.core.ui.transitions.ReflowText.java

private Layout createLayout(ReflowData data, Context context, boolean enforceMaxLines) {
    TextPaint paint = new TextPaint(Paint.ANTI_ALIAS_FLAG);
    paint.setTextSize(data.textSize);/*ww  w .  j  av a 2 s . com*/
    paint.setColor(data.textColor);
    paint.setLetterSpacing(data.letterSpacing);
    if (data.fontResId != 0) {
        try {
            Typeface font = ResourcesCompat.getFont(context, data.fontResId);
            if (font != null) {
                paint.setTypeface(font);
            }
        } catch (Resources.NotFoundException nfe) {
        }
    }

    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
        StaticLayout.Builder builder = StaticLayout.Builder
                .obtain(data.text, 0, data.text.length(), paint, data.textWidth)
                .setLineSpacing(data.lineSpacingAdd, data.lineSpacingMult).setBreakStrategy(data.breakStrategy);
        if (enforceMaxLines && data.maxLines != -1) {
            builder.setMaxLines(data.maxLines);
            builder.setEllipsize(TextUtils.TruncateAt.END);
        }
        return builder.build();
    } else {
        return new StaticLayout(data.text, paint, data.textWidth, Layout.Alignment.ALIGN_NORMAL,
                data.lineSpacingMult, data.lineSpacingAdd, true);
    }
}

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 www .  ja v  a 2 s.  c o  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:uk.co.samuelwall.materialtaptargetprompt.MaterialTapTargetPrompt.java

void updateTextPositioning() {
    final float primaryTextWidth = mPaintPrimaryText.measureText(mPrimaryText);
    final float secondaryTextWidth = mSecondaryText != null ? mPaintSecondaryText.measureText(mSecondaryText)
            : 0;/*  w  w  w  . j ava  2  s.  c  om*/
    final float textWidth;
    final float maxWidth = Math.max(80,
            (mView.mClipBounds ? mView.mClipBoundsRight - mView.mClipBoundsLeft : getParentView().getWidth())
                    - (mTextPadding * 2));
    final float textWidthCalculation = Math.min(mMaxTextWidth, Math.max(primaryTextWidth, secondaryTextWidth));
    if (textWidthCalculation > maxWidth) {
        mView.mTextLeft = (mView.mClipBounds ? mView.mClipBoundsLeft : 0) + mTextPadding;
        textWidth = maxWidth;
    } else {
        if (mTextPositionRight) {
            mView.mTextLeft = (mView.mClipBounds ? mView.mClipBoundsRight : getParentView().getRight())
                    - mTextPadding - textWidthCalculation;
        } else {
            mView.mTextLeft = mTextPadding + (mView.mClipBounds ? mView.mClipBoundsLeft : 0);
        }
        textWidth = textWidthCalculation;
    }

    mView.mPrimaryTextLayout = new StaticLayout(mPrimaryText, mPaintPrimaryText, (int) textWidth,
            Layout.Alignment.ALIGN_NORMAL, 1f, 0f, false);

    mView.mPrimaryTextTop = mView.mCentreTop;
    if (mTextPositionAbove) {
        mView.mPrimaryTextTop = mView.mPrimaryTextTop - mBaseFocalRadius - mFocalToTextPadding
                - mView.mPrimaryTextLayout.getHeight();
    } else {
        mView.mPrimaryTextTop += mBaseFocalRadius + mFocalToTextPadding;
    }

    if (mSecondaryText != null) {
        mView.mSecondaryTextLayout = new StaticLayout(mSecondaryText, mPaintSecondaryText, (int) textWidth,
                Layout.Alignment.ALIGN_NORMAL, 1f, 0f, false);
        if (mTextPositionAbove) {
            mView.mPrimaryTextTop = mView.mPrimaryTextTop - mView.mTextSeparation
                    - mView.mSecondaryTextLayout.getHeight();
        }

        mView.mSecondaryTextOffsetTop = mView.mPrimaryTextLayout.getHeight() + mView.mTextSeparation;
    } else {
        mView.mSecondaryTextLayout = null;
    }

    updateBackgroundRadius();
    updateIconPosition();
}

From source file:de.dreier.mytargets.views.MaterialTapTargetPrompt.java

private void updateTextPositioning() {
    final float primaryTextWidth = mPaintPrimaryText.measureText(mPrimaryText);
    final float secondaryTextWidth = mSecondaryText != null ? mPaintSecondaryText.measureText(mSecondaryText)
            : 0;//from  w w  w  .j  av a  2 s  .co  m
    final float textWidth;
    final float maxWidth = Math.max(80,
            (mView.mClipBounds ? mView.mClipBoundsRight - mView.mClipBoundsLeft : getParentView().getWidth())
                    - (mTextPadding * 2));
    final float textWidthCalculation = Math.min(mMaxTextWidth, Math.max(primaryTextWidth, secondaryTextWidth));
    if (textWidthCalculation > maxWidth) {
        mView.mTextLeft = (mView.mClipBounds ? mView.mClipBoundsLeft : 0) + mTextPadding;
        textWidth = maxWidth;
    } else {
        if (mTextPositionRight) {
            mView.mTextLeft = (mView.mClipBounds ? mView.mClipBoundsRight : getParentView().getRight())
                    - mTextPadding - textWidthCalculation;
        } else {
            mView.mTextLeft = mTextPadding + (mView.mClipBounds ? mView.mClipBoundsLeft : 0);
        }
        textWidth = textWidthCalculation;
    }

    mView.mPrimaryTextLayout = new StaticLayout(mPrimaryText, mPaintPrimaryText, (int) textWidth,
            Layout.Alignment.ALIGN_NORMAL, 1f, 0f, false);

    mView.mPrimaryTextTop = mView.mCentreTop;
    if (mTextPositionAbove) {
        mView.mPrimaryTextTop = mView.mPrimaryTextTop - mBaseFocalRadius - mFocalToTextPadding
                - mView.mPrimaryTextLayout.getHeight();
    } else {
        mView.mPrimaryTextTop += mBaseFocalRadius + mFocalToTextPadding;
    }

    if (mSecondaryText != null) {
        mView.mSecondaryTextLayout = new StaticLayout(mSecondaryText, mPaintSecondaryText, (int) textWidth,
                Layout.Alignment.ALIGN_NORMAL, 1f, 0f, false);
        if (mTextPositionAbove) {
            mView.mPrimaryTextTop = mView.mPrimaryTextTop - mView.mTextSeparation
                    - mView.mSecondaryTextLayout.getHeight();
        }

        mView.mSecondaryTextOffsetTop = mView.mPrimaryTextLayout.getHeight() + mView.mTextSeparation;
    } else {
        mView.mSecondaryTextLayout = null;
    }

    updateBackgroundRadius();
    updateIconPosition();
}