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

The following examples show how to use android.text.Layout#getLineDescent() . 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: RecipientEditTextView.java    From talk-android with MIT License 6 votes vote down vote up
private MoreImageSpan createMoreSpan(int count) {
    String moreText = String.format(mMoreItem.getText().toString(), count);
    TextPaint morePaint = new TextPaint(getPaint());
    morePaint.setTextSize(mMoreItem.getTextSize());
    morePaint.setColor(mMoreItem.getCurrentTextColor());
    int width = (int) morePaint.measureText(moreText) + mMoreItem.getPaddingLeft()
            + mMoreItem.getPaddingRight();
    int height = getLineHeight();
    Bitmap drawable = Bitmap.createBitmap(width, height, Bitmap.Config.ARGB_8888);
    Canvas canvas = new Canvas(drawable);
    int adjustedHeight = height;
    Layout layout = getLayout();
    if (layout != null) {
        adjustedHeight -= layout.getLineDescent(0);
    }
    canvas.drawText(moreText, 0, moreText.length(), 0, adjustedHeight, morePaint);

    Drawable result = new BitmapDrawable(getResources(), drawable);
    result.setBounds(0, 0, width, height);
    return new MoreImageSpan(result);
}
 
Example 2
Source File: TextChipsEditView.java    From talk-android with MIT License 6 votes vote down vote up
private MoreImageSpan createMoreSpan(int count) {
    String moreText = String.format(mMoreItem.getText().toString(), count);
    TextPaint morePaint = new TextPaint(getPaint());
    morePaint.setTextSize(mMoreItem.getTextSize());
    morePaint.setColor(mMoreItem.getCurrentTextColor());
    int width = (int) morePaint.measureText(moreText) + mMoreItem.getPaddingLeft()
            + mMoreItem.getPaddingRight();
    int height = getLineHeight();
    Bitmap drawable = Bitmap.createBitmap(width, height, Bitmap.Config.ARGB_8888);
    Canvas canvas = new Canvas(drawable);
    int adjustedHeight = height;
    Layout layout = getLayout();
    if (layout != null) {
        adjustedHeight -= layout.getLineDescent(0);
    }
    canvas.drawText(moreText, 0, moreText.length(), 0, adjustedHeight, morePaint);

    Drawable result = new BitmapDrawable(getResources(), drawable);
    result.setBounds(0, 0, width, height);
    return new MoreImageSpan(result);
}
 
Example 3
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 4
Source File: RecipientEditTextView.java    From ChipsLibrary with Apache License 2.0 6 votes vote down vote up
private MoreImageSpan createMoreSpan(final int count)
{
final String moreText=String.format(mMoreItem.getText().toString(),count);
final TextPaint morePaint=new TextPaint(getPaint());
morePaint.setTextSize(mMoreItem.getTextSize());
morePaint.setColor(mMoreItem.getCurrentTextColor());
final int width=(int)morePaint.measureText(moreText)+mMoreItem.getPaddingLeft()+mMoreItem.getPaddingRight();
final int height=getLineHeight();
final Bitmap drawable=Bitmap.createBitmap(width,height,Bitmap.Config.ARGB_8888);
final Canvas canvas=new Canvas(drawable);
int adjustedHeight=height;
final Layout layout=getLayout();
if(layout!=null)
  adjustedHeight-=layout.getLineDescent(0);
canvas.drawText(moreText,0,moreText.length(),0,adjustedHeight,morePaint);
final Drawable result=new BitmapDrawable(getResources(),drawable);
result.setBounds(0,0,width,height);
return new MoreImageSpan(result);
}
 
Example 5
Source File: ColorsText.java    From JsDroidEditor with Mozilla Public License 2.0 4 votes vote down vote up
private void drawText(Canvas canvas) {
    Map<Integer, String> lineNumbles = getLineNumbers();
    Layout layout = getLayout();
    int selectLine = getSelectLine();
    int range[] = new int[4];
    {// 计算需要绘制的行号所需要的范围

        int clipLeft = 0;
        int clipTop = (getScrollView().getScrollY() == 0) ? 0
                : getExtendedPaddingTop() + getScrollView().getScrollY()
                - getScrollView().getPaddingTop();
        int clipRight = getWidth();
        int clipBottom = clipTop + getScrollView().getHeight();
        Rect rect = new Rect(clipLeft, clipTop, clipRight, clipBottom);
        getLineRangeForDraw(rect, range);
    }
    int firstLine = range[0];
    int lastLine = range[1];
    if (firstLine < 0) {
        return;
    }
    int previousLineBottom = layout.getLineTop(firstLine);
    int previousLineEnd = layout.getLineStart(firstLine);
    int lineCount = getLineCount();
    Paint paint = getPaint();


    for (int lineNum = firstLine; lineNum <= lastLine
            && lineNum < lineCount; lineNum++) {
        int start = previousLineEnd;

        previousLineEnd = layout.getLineStart(lineNum + 1);

        int end = layout.getLineVisibleEnd(lineNum);
        int ltop = previousLineBottom;
        int lbottom = layout.getLineTop(lineNum + 1);
        previousLineBottom = lbottom;
        int lbaseline = lbottom - layout.getLineDescent(lineNum);
        int left = getPaddingLeft();
        // 绘制选择行背景
        if (lineNum == selectLine) {
            paint.setColor(lineNumberBackgroundColor);
            paint.setStyle(Paint.Style.STROKE);
            paint.setStrokeWidth(mLineNumberBgStrokeWidth);
            canvas.drawRect(getPaddingLeft() - mLineNumberBgStrokeWidth, ltop, getRight() - getPaddingRight() + mLineNumberBgStrokeWidth, lbottom, paint);
            paint.setStyle(Paint.Style.FILL);
        }
        // 绘制行号
        String lineNumberText = lineNumbles.get(lineNum);
        if (lineNumberText != null) {
            paint.setColor(lineNumberColor);
            canvas.drawText(lineNumberText, 0, lineNumberText.length(), mNumberPadding,
                    lbaseline, paint);
        }
        int textLength = getText().length();
        // 绘制文字
        if (start < textLength) {
            //计算需要绘制的文字位置
            //获取改行所有文字宽度
            float[] widths = new float[end - start + 1];
            paint.getTextWidths(getText(), start, end, widths);
            //计算获取看到的文字第一个位置,和对应的偏移x
            float firstNeedDrawPos[] = getLineFirstCharPosForDraw(widths);
            int firstPos = (int) firstNeedDrawPos[0];
            float offsetX = firstNeedDrawPos[1];
            int maxOffX = getViewScrollX() + getVisibleWidth();
            // 文字着色
            for (int i = start + firstPos; i < end && i < textLength; ) {
                if (offsetX > maxOffX) {
                    break;
                }
                int color = getCodeColor(i);
                {
                    float fontWidths = widths[i - start];
                    int fontCount = 1;
                    for (int j = i + 1; j < end && j < textLength; j++) {
                        if (color == getCodeColor(j)) {
                            fontCount++;
                            fontWidths += widths[j - start];
                        } else {
                            break;
                        }
                    }
                    if (color == 0) {
                        color = defaultTextColor;
                    }

                    paint.setColor(color);
                    canvas.drawText(getText(), i, i + fontCount, left + offsetX, lbaseline, paint);
                    i += fontCount;
                    offsetX += fontWidths;

                }
            }
        }
    }

}