Java Code Examples for android.text.StaticLayout#getDesiredWidth()

The following examples show how to use android.text.StaticLayout#getDesiredWidth() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: PageLoader.java    From a with GNU General Public License v3.0 6 votes vote down vote up
private void drawScaledText(Canvas canvas, String line, float lineWidth, TextPaint paint, float top) {
    float x = mMarginLeft;

    if (isFirstLineOfParagraph(line)) {
        canvas.drawText(indent, x, top, paint);
        float bw = StaticLayout.getDesiredWidth(indent, paint);
        x += bw;
        line = line.substring(readBookControl.getIndent());
    }
    int gapCount = line.length() - 1;
    int i = 0;
    float d = ((mDisplayWidth - (mMarginLeft + mMarginRight)) - lineWidth) / gapCount;
    for (; i < line.length(); i++) {
        String c = String.valueOf(line.charAt(i));
        float cw = StaticLayout.getDesiredWidth(c, paint);
        canvas.drawText(c, x, top, paint);
        x += cw + d;
    }

}
 
Example 2
Source File: PageLoader.java    From HaoReader with GNU General Public License v3.0 6 votes vote down vote up
private void drawScaledText(Canvas canvas, String line, float lineWidth, TextPaint paint, float top) {
    float x = mMarginLeft;
    if (isFirstLineOfParagraph(line)) {
        String blanks = StringUtils.halfToFull("  ");
        canvas.drawText(blanks, x, top, paint);
        float bw = StaticLayout.getDesiredWidth(blanks, paint);
        x += bw;
        line = line.substring(2);
    }

    final int lineLength = line.length();
    final int gapCount = lineLength - 1;
    final float d = ((mDisplayWidth - (mMarginLeft + mMarginRight)) - lineWidth) / gapCount;
    for (int i = 0; i < lineLength; i++) {
        String c = String.valueOf(line.charAt(i));
        float cw = StaticLayout.getDesiredWidth(c, paint);
        canvas.drawText(c, x, top, paint);
        x += cw + d;
    }
}
 
Example 3
Source File: JustifyTextView.java    From BigApp_Discuz_Android with Apache License 2.0 6 votes vote down vote up
@Override
protected void onDraw(Canvas canvas) {
    TextPaint paint = getPaint();
    paint.setColor(getCurrentTextColor());
    paint.drawableState = getDrawableState();
    mViewWidth = getMeasuredWidth();
    String text = (String) getText();
    mLineY = 0;
    mLineY += getTextSize();
    Layout layout = getLayout();
    for (int i = 0; i < layout.getLineCount(); i++) {
        int lineStart = layout.getLineStart(i);
        int lineEnd = layout.getLineEnd(i);
        String line = text.substring(lineStart, lineEnd);

        float width = StaticLayout.getDesiredWidth(text, lineStart, lineEnd, getPaint());
        if (needScale(line)) {
            drawScaledText(canvas, lineStart, line, width);
        } else {
            canvas.drawText(line, 0, mLineY, paint);
        }

        mLineY += getLineHeight();
    }
}
 
Example 4
Source File: JustifyTextView.java    From BigApp_Discuz_Android with Apache License 2.0 6 votes vote down vote up
private void drawScaledText(Canvas canvas, int lineStart, String line, float lineWidth) {
    float x = 0;
    if (isFirstLineOfParagraph(lineStart, line)) {
        String blanks = "  ";
        canvas.drawText(blanks, x, mLineY, getPaint());
        float bw = StaticLayout.getDesiredWidth(blanks, getPaint());
        x += bw;

        line = line.substring(3);
    }

    float d = (mViewWidth - lineWidth) / line.length() - 1;
    for (int i = 0; i < line.length(); i++) {
        String c = String.valueOf(line.charAt(i));
        float cw = StaticLayout.getDesiredWidth(c, getPaint());
        canvas.drawText(c, x, mLineY, getPaint());
        x += cw + d;
    }
}
 
Example 5
Source File: PageLoader.java    From a with GNU General Public License v3.0 5 votes vote down vote up
private void drawScaledText(Canvas canvas, String line, float lineWidth, TextPaint paint, float top,int y,List<TxtLine> txtLists) {
    float x = mMarginLeft;

    if (isFirstLineOfParagraph(line)) {
        canvas.drawText(indent, x, top, paint);
        float bw = StaticLayout.getDesiredWidth(indent, paint);
        x += bw;
        line = line.substring(readBookControl.getIndent());
    }
    int gapCount = line.length() - 1;
    int i = 0;

    TxtLine txtList = new TxtLine();//每一行pzl
    txtList.CharsData = new ArrayList<TxtChar>();//pzl

    float d = ((mDisplayWidth - (mMarginLeft + mMarginRight)) - lineWidth) / gapCount;
    for (; i < line.length(); i++) {
        String c = String.valueOf(line.charAt(i));
        float cw = StaticLayout.getDesiredWidth(c, paint);
        canvas.drawText(c, x, top, paint);
        //pzl
        TxtChar txtChar = new TxtChar();
        txtChar.chardata = line.charAt(i);
        if(i==0) txtChar.charWidth = cw + d/2;
        if(i==gapCount) txtChar.charWidth = cw + d/2;
        txtChar.charWidth = cw + d;
        ;//字宽
        //txtChar.Index = y;//每页每个字的位置
        txtList.CharsData.add(txtChar);
        //pzl
        x += cw + d;
    }
    txtLists.set(y, txtList);//pzl

}
 
Example 6
Source File: PageLoader.java    From MyBookshelf with GNU General Public License v3.0 5 votes vote down vote up
private void drawScaledText(Canvas canvas, String line, float lineWidth, TextPaint paint, float top, int y, List<TxtLine> txtLists) {
    float x = mMarginLeft;

    if (isFirstLineOfParagraph(line)) {
        canvas.drawText(indent, x, top, paint);
        float bw = StaticLayout.getDesiredWidth(indent, paint);
        x += bw;
        line = line.substring(readBookControl.getIndent());
    }
    int gapCount = line.length() - 1;
    int i = 0;

    TxtLine txtList = new TxtLine();//每一行pzl
    txtList.setCharsData(new ArrayList<>());//pzl

    float d = ((mDisplayWidth - (mMarginLeft + mMarginRight)) - lineWidth) / gapCount;
    for (; i < line.length(); i++) {
        String c = String.valueOf(line.charAt(i));
        float cw = StaticLayout.getDesiredWidth(c, paint);
        canvas.drawText(c, x, top, paint);
        //pzl
        TxtChar txtChar = new TxtChar();
        txtChar.setChardata(line.charAt(i));
        if (i == 0) txtChar.setCharWidth(cw + d / 2);
        if (i == gapCount) txtChar.setCharWidth(cw + d / 2);
        txtChar.setCharWidth(cw + d);
        ;//字宽
        //txtChar.Index = y;//每页每个字的位置
        txtList.getCharsData().add(txtChar);
        //pzl
        x += cw + d;
    }
    if (txtLists != null) {
        txtLists.set(y, txtList);//pzl
    }
}
 
Example 7
Source File: RecyclerWheelPicker.java    From RecyclerWheelPicker with Apache License 2.0 5 votes vote down vote up
private void drawUnitText(Canvas c) {
    if (null != mUnitText && !TextUtils.isEmpty(mUnitText)) {
        mUnitTextPaint.setColor(mUnitColor);
        mUnitTextPaint.setTextSize(mUnitSize);
        float startX = getWidth() - getPaddingRight() - StaticLayout.getDesiredWidth(mUnitText, 0, mUnitText.length(), mUnitTextPaint);
        Paint.FontMetrics fontMetrics = mUnitTextPaint.getFontMetrics();
        float startY = getVerticalSpace() / 2 + mUnitSize / 2 - fontMetrics.descent / 2;
        c.drawText(mUnitText, startX, startY, mUnitTextPaint);
    }
}
 
Example 8
Source File: ContentTextViewJustified.java    From HeaderDialog with MIT License 5 votes vote down vote up
private void drawScaledText(final String line, final Canvas canvas, final float y, final float desiredLineWidth, final TextPaint paint) {
    String[] words = line.split(" ");

    float lineWidthWithoutSpaces = StaticLayout.getDesiredWidth(line.replace(" ", ""), getPaint());
    float spacing = (desiredLineWidth - lineWidthWithoutSpaces) / (words.length - 1);

    float x = 0;
    for (String word : words) {
        float cw = StaticLayout.getDesiredWidth(word, paint);
        canvas.drawText(word, x, y, paint);
        x += cw + spacing;
    }
}
 
Example 9
Source File: SelectableTextView.java    From AdvancedTextView with Apache License 2.0 5 votes vote down vote up
/**
 * 重绘文字,两端对齐
 *
 * @param canvas
 */
private void drawTextWithJustify(Canvas canvas) {
    // 文字画笔
    TextPaint textPaint = getPaint();
    textPaint.setColor(getCurrentTextColor());
    textPaint.drawableState = getDrawableState();

    String text_str = getText().toString();
    // 当前所在行的Y向偏移
    int currentLineOffsetY = getPaddingTop();
    currentLineOffsetY += getTextSize();

    Layout layout = getLayout();

    //循环每一行,绘制文字
    for (int i = 0; i < layout.getLineCount(); i++) {
        int lineStart = layout.getLineStart(i);
        int lineEnd = layout.getLineEnd(i);
        //获取到TextView每行中的内容
        String line_str = text_str.substring(lineStart, lineEnd);
        // 获取每行字符串的宽度(不包括字符间距)
        float desiredWidth = StaticLayout.getDesiredWidth(text_str, lineStart, lineEnd, getPaint());

        if (isLineNeedJustify(line_str)) {
            //最后一行不需要重绘
            if (i == layout.getLineCount() - 1) {
                canvas.drawText(line_str, getPaddingLeft(), currentLineOffsetY, textPaint);
            } else {
                drawJustifyTextForLine(canvas, line_str, desiredWidth, currentLineOffsetY);
            }
        } else {
            canvas.drawText(line_str, getPaddingLeft(), currentLineOffsetY, textPaint);
        }
        //更新行Y向偏移
        currentLineOffsetY += getLineHeight();
    }
}
 
Example 10
Source File: SelectableTextView.java    From AdvancedTextView with Apache License 2.0 5 votes vote down vote up
/**
 * 计算字符距离控件左侧的位移
 *
 * @param line       字符所在行
 * @param charOffset 字符偏移量
 */
private float calculatorCharPositionToLeft(int line, int charOffset) {

    String text_str = getText().toString();


    Layout layout = getLayout();
    int lineStart = layout.getLineStart(line);
    int lineEnd = layout.getLineEnd(line);

    String line_str = text_str.substring(lineStart, lineEnd);

    if (line_str.equals("\n"))
        return getPaddingLeft();
    // 最左侧
    if (lineStart == charOffset)
        return getPaddingLeft();
    // 最右侧
    if (charOffset == lineEnd - 1)
        return mViewTextWidth + getPaddingLeft();

    float desiredWidth = StaticLayout.getDesiredWidth(text_str, lineStart, lineEnd, getPaint());

    // 中间位置
    // 计算相邻字符之间需要填充的宽度
    // (TextView内容的实际宽度 - 该行字符串的宽度)/(字符个数-1)
    float insert_blank = (mViewTextWidth - desiredWidth) / (line_str.length() - 1);
    // 计算当前字符左侧所有字符的宽度
    float allLeftCharWidth = StaticLayout.getDesiredWidth(text_str.substring(lineStart, charOffset), getPaint());

    // 相邻字符之间需要填充的宽度 + 当前字符左侧所有字符的宽度
    return insert_blank * (charOffset - lineStart) + allLeftCharWidth + getPaddingLeft();

}
 
Example 11
Source File: JustifyTextView.java    From AlignTextView with Apache License 2.0 5 votes vote down vote up
@Override
protected void onDraw(Canvas canvas) {
	TextPaint paint = getPaint();
	paint.setColor(getCurrentTextColor());
	paint.drawableState = getDrawableState();
	mViewWidth = getMeasuredWidth();
	String text = getText().toString();
	mLineY = 0;
	mLineY += getTextSize();
	Layout layout = getLayout();

	// layout.getLayout()在4.4.3出现NullPointerException
	if (layout == null) {
		return;
	}

	Paint.FontMetrics fm = paint.getFontMetrics();

	int textHeight = (int) (Math.ceil(fm.descent - fm.ascent));
	textHeight = (int) (textHeight * layout.getSpacingMultiplier() + layout
			.getSpacingAdd());

	for (int i = 0; i < layout.getLineCount(); i++) {
		int lineStart = layout.getLineStart(i);
		int lineEnd = layout.getLineEnd(i);
		float width = StaticLayout.getDesiredWidth(text, lineStart,
				lineEnd, getPaint());
		String line = text.substring(lineStart, lineEnd);
		if (needScale(line)) {
			drawScaledText(canvas, lineStart, line, width);
		} else {
			canvas.drawText(line, 0, mLineY, paint);
		}
		mLineY += textHeight;
	}
}
 
Example 12
Source File: JustifyTextView.java    From DoraemonKit with Apache License 2.0 4 votes vote down vote up
@Override
protected void onDraw(Canvas canvas) {
    TextPaint paint = getPaint();
    paint.setColor(getCurrentTextColor());
    paint.drawableState = getDrawableState();
    mViewWidth = getMeasuredWidth();
    String text = getText().toString();
    mLineY = 0;
    mLineY += getTextSize();
    Layout layout = getLayout();

    // layout.getLayout()在4.4.3出现NullPointerException
    if (layout == null) {
        return;
    }

    Paint.FontMetrics fm = paint.getFontMetrics();

    int textHeight = (int) (Math.ceil(fm.descent - fm.ascent));
    textHeight = (int) (textHeight * layout.getSpacingMultiplier() + layout
            .getSpacingAdd());
    //解决了最后一行文字间距过大的问题
    for (int i = 0; i < layout.getLineCount(); i++) {
        int lineStart = layout.getLineStart(i);
        int lineEnd = layout.getLineEnd(i);
        float width = StaticLayout.getDesiredWidth(text, lineStart,
                lineEnd, getPaint());
        String line = text.substring(lineStart, lineEnd);

        if (i < layout.getLineCount() - 1) {
            if (needScale(line)) {
                drawScaledText(canvas, lineStart, line, width);
            } else {
                canvas.drawText(line, 0, mLineY, paint);
            }
        } else {
            canvas.drawText(line, 0, mLineY, paint);
        }
        mLineY += textHeight;
    }
}
 
Example 13
Source File: RecyclerWheelPicker.java    From RecyclerWheelPicker with Apache License 2.0 4 votes vote down vote up
@Override
    public boolean drawChild(Canvas canvas, View child, long drawingTime) {
        // resize the text size if text can not be shown completely
        if (child instanceof TextView) {
            String data = ((TextView) child).getText().toString();
            if (((TextView) child).getTextSize() == mTextSize) {
                float finalTextSize = mTextSize;
                float dataStringW = StaticLayout.getDesiredWidth(data, 0, data.length(), ((TextView) child).getPaint());
                if (getHorizontalSpace() > 0 && dataStringW * 1.1f > getHorizontalSpace()) {
                    finalTextSize = getHorizontalSpace() / dataStringW / 1.1f * mTextSize;
                }
                ((TextView) child).setTextSize(TypedValue.COMPLEX_UNIT_PX, finalTextSize);
            }
        }

        // scale child X Y
        int centerY = getVerticalSpace() / 2;
        int childCenterY = child.getTop() + child.getHeight() / 2;
        float factor = (centerY - childCenterY) * 1f / centerY;
        float alphaFactor = 1 - 0.7f * Math.abs(factor);
        child.setAlpha(alphaFactor * alphaFactor * alphaFactor);
        float scaleFactor = 1 - 0.3f * Math.abs(factor);
        child.setScaleX(scaleFactor);
        child.setScaleY(scaleFactor);

        // rotateX calculate rotate radius
        float rotateRadius = 2.0f * centerY / (float) Math.PI;
        float rad = (centerY - childCenterY) * 1f / rotateRadius;
        float offsetZ = rotateRadius * (1 - (float) Math.cos(rad));
        float rotateDeg = rad * 180 / (float) Math.PI;
        // offset Y for item rotate
        float offsetY = centerY - childCenterY - rotateRadius * (float) Math.sin(rad) * 1.3f;
        child.setTranslationY(offsetY);

        // for most of all devices, the childview would be rotated around X-axis perfectly by calling
        // view's setRotationX(). But, this would not work on huawei devices. so for the compatibility
        // here we resolve this compatibility-bug through another way by using Camera;

//  ViewCompat.setZ(child, -offsetZ);
//  child.setRotationX(rotateDeg);

        canvas.save();
        mCamera.save();
        mCamera.translate(0, 0, offsetZ);
        mCamera.rotateX(rotateDeg);
        mCamera.getMatrix(mMatrix);
        mCamera.restore();
        mMatrix.preTranslate(-child.getWidth() / 2, -childCenterY);
        mMatrix.postTranslate(child.getWidth() / 2, childCenterY);
        canvas.concat(mMatrix);
        boolean result = super.drawChild(canvas, child, drawingTime);
        canvas.restore();
        return result;

//        // parent centerY ,item centerY
//        int centerY = (getHeight() - getPaddingTop() - getPaddingBottom()) / 2;
//        int childCenterY = child.getTop() + child.getHeight() / 2;
//        // alpha
//        float factor = (centerY - childCenterY) * 1f / centerY;
//        float currentFactor = 1 - 0.7f * Math.abs(factor);
//        child.setAlpha(currentFactor * currentFactor * currentFactor);
//
//        // rotate radius
//        float rotateRadius = 2.5f * centerY / (float) Math.PI;
//        // deg
//        float rad = (centerY - childCenterY) * 1f / rotateRadius;
//        float rotateDeg = rad * 180 / (float) Math.PI;
//        // for camera
//        float offsetZ = rotateRadius * (1 - (float) Math.cos(rad));
//        canvas.save();
//        // offset Y for item rotate
//        float offsetY = centerY - childCenterY - rotateRadius * (float) Math.sin(rad);
//        canvas.translate(0, offsetY);
//        mCamera.save();
//        mCamera.translate(0, 0, offsetZ);
//        mCamera.rotateX(rotateDeg);
//        mCamera.getMatrix(mMatrix);
//        mCamera.restore();
//        mMatrix.preTranslate(-child.getWidth() / 2, -childCenterY);
//        mMatrix.postTranslate(child.getWidth() / 2, childCenterY);
//        canvas.concat(mMatrix);
//        super.drawChild(canvas, child, drawingTime);
//        canvas.restore();
//        return true;
    }
 
Example 14
Source File: JustifyTextView.java    From A-week-to-develop-android-app-plan with Apache License 2.0 4 votes vote down vote up
@Override
protected void onDraw(Canvas canvas) {
    TextPaint paint = getPaint();
    paint.setColor(getCurrentTextColor());
    // 返回绘制状态的资源ID数组表示视图的当前状态
    paint.drawableState = getDrawableState();
    // 对View上的内容进行测量后得到的View内容占据的宽度
    // 前提是你必须在父布局的onLayout()方法或者此View的onDraw()方法里调用measure(0,0);
    // 否则你得到的结果和getWidth()得到的结果一样。
    mViewWidth = getMeasuredWidth();
    // 获取文本
    String text = getText().toString();
    mLineY = 0;
    mLineY += getTextSize();
    // 获取用于显示当前文本的布局
    Layout layout = getLayout();

    if (layout == null) {
        return;
    }

    Paint.FontMetrics fm = paint.getFontMetrics();

    int textHeight = (int) (Math.ceil(fm.descent - fm.ascent));
    textHeight = (int) (textHeight * layout.getSpacingMultiplier() + layout.getSpacingAdd());

    for (int i = 0; i < layout.getLineCount(); i++) {
        // 返回文本中的指定行开头的偏移
        int lineStart = layout.getLineStart(i);
        // 返回文本中的指定行最后一个字符的偏移
        int lineEnd = layout.getLineEnd(i);
        float width = StaticLayout.getDesiredWidth(text, lineStart, lineEnd, getPaint());
        String line = text.substring(lineStart, lineEnd);

        if (line.equals("")) {
            break;
        }

        if (i < layout.getLineCount() - 1) {
            if (needScale(line)) {
                drawScaledText(canvas, lineStart, line, width);
            } else {
                canvas.drawText(line, 0, mLineY, paint);
            }
        } else {
            canvas.drawText(line, 0, mLineY, paint);
        }

        // 增加行高
        mLineY += textHeight;
    }
}