Java Code Examples for android.text.Layout#getSpacingAdd()

The following examples show how to use android.text.Layout#getSpacingAdd() . 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: LayoutMeasureUtil.java    From TextLayoutBuilder with Apache License 2.0 6 votes vote down vote up
/**
 * Prior to version 20, If the Layout specifies extra space between lines (either by spacingmult
 * or spacingadd) the StaticLayout would erroneously add this space after the last line as well.
 * This bug was fixed in version 20. This method calculates the extra space and reduces the height
 * by that amount.
 *
 * @param layout The layout.
 * @return The height of the layout.
 */
public static int getHeight(Layout layout) {
  if (layout == null) {
    return 0;
  }

  int extra = 0;
  if (Build.VERSION.SDK_INT < Build.VERSION_CODES.KITKAT_WATCH
      && layout instanceof StaticLayout) {
    int line = Math.max(0, layout.getLineCount() - 1);
    int above = layout.getLineAscent(line);
    int below = layout.getLineDescent(line);
    float originalSize = (below - above - layout.getSpacingAdd()) / layout.getSpacingMultiplier();
    float ex = below - above - originalSize;
    if (ex >= 0) {
      extra = (int) (ex + 0.5);
    } else {
      extra = -(int) (-ex + 0.5);
    }
  }
  return layout.getHeight() - extra;
}
 
Example 2
Source File: CodeTextView.java    From Markwon with Apache License 2.0 5 votes vote down vote up
private static float getLineBottomWithoutSpacing(@NonNull Layout layout, int line) {
    final float value = layout.getLineBottom(line);

    final boolean isLastLineSpacingNotAdded = Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT;
    final boolean isLastLine = line == (layout.getLineCount() - 1);

    final float lineBottomWithoutSpacing;

    final float lineSpacingExtra = layout.getSpacingAdd();
    final float lineSpacingMultiplier = layout.getSpacingMultiplier();

    final boolean hasLineSpacing = Float.compare(lineSpacingExtra, .0F) != 0
            || Float.compare(lineSpacingMultiplier, 1F) != 0;

    if (!hasLineSpacing || isLastLine && isLastLineSpacingNotAdded) {
        lineBottomWithoutSpacing = value;
    } else {
        final float extra;
        if (Float.compare(lineSpacingMultiplier, 1F) != 0) {
            final float lineHeight = getLineHeight(layout, line);
            extra = lineHeight - (lineHeight - lineSpacingExtra) / lineSpacingMultiplier;
        } else {
            extra = lineSpacingExtra;
        }
        lineBottomWithoutSpacing = value - extra;
    }

    return lineBottomWithoutSpacing;
}
 
Example 3
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 4
Source File: BulletSpan.java    From Telegram-FOSS with GNU General Public License v2.0 5 votes vote down vote up
@Override
public void drawLeadingMargin(Canvas canvas, Paint paint, int x, int dir,
                              int top, int baseline, int bottom,
                              CharSequence text, int start, int end,
                              boolean first, Layout layout) {
    if (((Spanned) text).getSpanStart(this) == start) {
        Paint.Style style = paint.getStyle();
        int oldcolor = 0;

        if (mWantColor) {
            oldcolor = paint.getColor();
            paint.setColor(mColor);
        }

        paint.setStyle(Paint.Style.FILL);

        if (layout != null) {
            // "bottom" position might include extra space as a result of line spacing
            // configuration. Subtract extra space in order to show bullet in the vertical
            // center of characters.
            final int line = layout.getLineForOffset(start);
            int spacing = line != layout.getLineCount() - 1 ? (int) layout.getSpacingAdd() : 0;
            bottom = bottom - spacing;
        }

        final float yPosition = (top + bottom) / 2f;
        final float xPosition = x + dir * mBulletRadius;

        canvas.drawCircle(xPosition, yPosition, mBulletRadius, paint);

        if (mWantColor) {
            paint.setColor(oldcolor);
        }

        paint.setStyle(style);
    }
}
 
Example 5
Source File: BulletSpan.java    From Telegram with GNU General Public License v2.0 5 votes vote down vote up
@Override
public void drawLeadingMargin(Canvas canvas, Paint paint, int x, int dir,
                              int top, int baseline, int bottom,
                              CharSequence text, int start, int end,
                              boolean first, Layout layout) {
    if (((Spanned) text).getSpanStart(this) == start) {
        Paint.Style style = paint.getStyle();
        int oldcolor = 0;

        if (mWantColor) {
            oldcolor = paint.getColor();
            paint.setColor(mColor);
        }

        paint.setStyle(Paint.Style.FILL);

        if (layout != null) {
            // "bottom" position might include extra space as a result of line spacing
            // configuration. Subtract extra space in order to show bullet in the vertical
            // center of characters.
            final int line = layout.getLineForOffset(start);
            int spacing = line != layout.getLineCount() - 1 ? (int) layout.getSpacingAdd() : 0;
            bottom = bottom - spacing;
        }

        final float yPosition = (top + bottom) / 2f;
        final float xPosition = x + dir * mBulletRadius;

        canvas.drawCircle(xPosition, yPosition, mBulletRadius, paint);

        if (mWantColor) {
            paint.setColor(oldcolor);
        }

        paint.setStyle(style);
    }
}
 
Example 6
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 7
Source File: LayoutUtils.java    From Markwon with Apache License 2.0 4 votes vote down vote up
public static int getLineBottomWithoutPaddingAndSpacing(
        @NonNull Layout layout,
        int line
) {

    final int bottom = layout.getLineBottom(line);
    final boolean lastLineSpacingNotAdded = Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT;
    final boolean isSpanLastLine = line == (layout.getLineCount() - 1);

    final int lineBottom;
    final float lineSpacingExtra = layout.getSpacingAdd();
    final float lineSpacingMultiplier = layout.getSpacingMultiplier();

    // simplified check
    final boolean hasLineSpacing = lineSpacingExtra != DEFAULT_EXTRA
            || lineSpacingMultiplier != DEFAULT_MULTIPLIER;

    if (!hasLineSpacing
            || (isSpanLastLine && lastLineSpacingNotAdded)) {
        lineBottom = bottom;
    } else {
        final float extra;
        if (Float.compare(DEFAULT_MULTIPLIER, lineSpacingMultiplier) != 0) {
            final int lineHeight = getLineHeight(layout, line);
            extra = lineHeight -
                    ((lineHeight - lineSpacingExtra) / lineSpacingMultiplier);
        } else {
            extra = lineSpacingExtra;
        }
        lineBottom = (int) (bottom - extra + .5F);
    }

    // check if it is the last line that span is occupying **and** that this line is the last
    //  one in TextView
    if (isSpanLastLine
            && (line == layout.getLineCount() - 1)) {
        return lineBottom - layout.getBottomPadding();
    }

    return lineBottom;
}
 
Example 8
Source File: AlignTextView.java    From SprintNBA 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();
    width = getMeasuredWidth();
    String text = getText().toString();

    Paint.FontMetrics fm = paint.getFontMetrics();
    // 计算行高
    Layout layout = getLayout();

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

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

    float firstHeight = getTextSize();

    int gravity = getGravity();
    if ((gravity & 0x1000) == 0) { // 是否垂直居中
        firstHeight = firstHeight + (textHeight - firstHeight) / 2;
    }

    int paddingLeft = getPaddingLeft();
    int paddingRight = getPaddingRight();
    width = width - paddingLeft - paddingRight;

    lines.clear();
    tailLines.clear();

    // 文本含有换行符时,分割单独处理
    String[] items = text.split("\\n");
    for (String item : items) {
        calc(paint, item);
    }

    for (int i = 0; i < lines.size(); i++) {
        float drawY = i * textHeight + firstHeight;
        String line = lines.get(i);
        // 绘画起始x坐标
        float drawSpacingX = paddingLeft;
        float gap = (width - paint.measureText(line));
        float interval = gap / (line.length() - 1);

        // 绘制最后一行
        if (tailLines.contains(i)) {
            interval = 0;
            if (align == Align.ALIGN_CENTER)
                drawSpacingX += gap / 2;
            else if (align == Align.ALIGN_RIGHT)
                drawSpacingX += gap;
        }

        for (int j = 0; j < line.length(); j++) {
            float drawX = paint.measureText(line.substring(0, j))
                    + interval * j;
            canvas.drawText(line.substring(j, j + 1), drawX + drawSpacingX,
                    drawY, paint);
        }
    }
}
 
Example 9
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;
    }
}