Java Code Examples for android.graphics.Paint.breakText()

The following are Jave code examples for showing how to use breakText() of the android.graphics.Paint class. You can vote up the examples you like. Your votes will be used in our system to get more good examples.
+ Save this method
Example 1
Project: OpenHub   File: EllipsizeLineSpan.java   View Source Code Vote up 6 votes
@Override
public void draw(@NonNull Canvas canvas, CharSequence text, int start, int end,
                 float x, int top, int y, int bottom, @NonNull Paint paint) {
    float textWidth = paint.measureText(text, start, end);

    if (x + (int) Math.ceil(textWidth) < mClipRect.right) {
        //text fits
        canvas.drawText(text, start, end, x, y, paint);
    } else {
        float ellipsisWidth = paint.measureText("\u2026");
        // move 'end' to the ellipsis point
        end = start + paint.breakText(text, start, end, true,
                mClipRect.right - x - ellipsisWidth, null);
        canvas.drawText(text, start, end, x, y, paint);
        canvas.drawText("\u2026", x + paint.measureText(text, start, end), y, paint);
    }
}
 
Example 2
Project: FriendBook   File: StringAdapter.java   View Source Code Vote up 4 votes
public static SparseArray<ArrayList<String>> loadPages(String source, Paint textPaint, int visibleHeight, int visibleWidth, int intervalSize, int paragraphSize) {
    SparseArray<ArrayList<String>> pageArray = new SparseArray<>();
    List<String> lines = new ArrayList<>();
    if (source != null && source.length() > 0) {
        String[] split = source.split("\n");
        //剩余高度
        int rHeight = visibleHeight + intervalSize + paragraphSize;
        for (String paragraph : split) {
            boolean hasContent=false;
            //如果只有换行符,那么就不执行
            if (StringUtils.isBlank(paragraph)) continue;
            //重置段落
            paragraph = StringUtils.halfToFull("  " + paragraph + "\n");
            paragraph = StringUtils.trimBeforeReplace(paragraph, "  ");
            while (paragraph.length() > 0) {


                //测量一行占用的字节数
                int count = textPaint.breakText(paragraph, true, visibleWidth, null);
                String subStr = paragraph.substring(0, count);
                String trim = subStr.trim();
                if (trim.length()>0&&!trim.equals("\n") && !trim.equals("\r\n")&&!StringUtils.isBlank(trim)) {
                    //重置剩余距离
                    rHeight -= (textPaint.getTextSize() + intervalSize);

                    //达到行数要求,创建Page
                    if (rHeight < 0) {
                        //创建Page
                        pageArray.put(pageArray.size(), new ArrayList<>(lines));
                        //重置Lines
                        lines.clear();
                        rHeight = visibleHeight;
                        continue;
                    }
                    //将一行字节,存储到lines中
                    lines.add(subStr);
                    hasContent=true;
                }


                //裁剪
                paragraph = paragraph.substring(count);
            }

            if (lines.size() > 0&&hasContent) {
                rHeight -= paragraphSize;
            }
        }

        if (lines.size() != 0) {
            pageArray.put(pageArray.size(), new ArrayList<>(lines));
            //重置Lines
            lines.clear();

        }
    }
    return pageArray;
}
 
Example 3
Project: buildAPKsApps   File: AbstractChart.java   View Source Code Vote up 4 votes
/**
 * Draws the chart legend.
 * 
 * @param canvas the canvas to paint to
 * @param renderer the series renderer
 * @param titles the titles to go to the legend
 * @param left the left X value of the area to draw to
 * @param right the right X value of the area to draw to
 * @param y the y value of the area to draw to
 * @param width the width of the area to draw to
 * @param height the height of the area to draw to
 * @param legendSize the legend size
 * @param paint the paint to be used for drawing
 * @param calculate if only calculating the legend size
 * 
 * @return the legend height
 */
protected int drawLegend(Canvas canvas, DefaultRenderer renderer, String[] titles, int left,
    int right, int y, int width, int height, int legendSize, Paint paint, boolean calculate) {
  float size = 32;
  if (renderer.isShowLegend()) {
    float currentX = left;
    float currentY = y + height - legendSize + size;
    paint.setTextAlign(Align.LEFT);
    paint.setTextSize(renderer.getLegendTextSize());
    int sLength = Math.min(titles.length, renderer.getSeriesRendererCount());
    for (int i = 0; i < sLength; i++) {
      SimpleSeriesRenderer r = renderer.getSeriesRendererAt(i);
      final float lineSize = getLegendShapeWidth(i);
      if (r.isShowLegendItem()) {
        String text = titles[i];
        if (titles.length == renderer.getSeriesRendererCount()) {
          paint.setColor(r.getColor());
        } else {
          paint.setColor(Color.LTGRAY);
        }
        float[] widths = new float[text.length()];
        paint.getTextWidths(text, widths);
        float sum = 0;
        for (float value : widths) {
          sum += value;
        }
        float extraSize = lineSize + 10 + sum;
        float currentWidth = currentX + extraSize;

        if (i > 0 && getExceed(currentWidth, renderer, right, width)) {
          currentX = left;
          currentY += renderer.getLegendTextSize();
          size += renderer.getLegendTextSize();
          currentWidth = currentX + extraSize;
        }
        if (getExceed(currentWidth, renderer, right, width)) {
          float maxWidth = right - currentX - lineSize - 10;
          if (isVertical(renderer)) {
            maxWidth = width - currentX - lineSize - 10;
          }
          int nr = paint.breakText(text, true, maxWidth, widths);
          text = text.substring(0, nr) + "...";
        }
        if (!calculate) {
          drawLegendShape(canvas, r, currentX, currentY, i, paint);
          drawString(canvas, text, currentX + lineSize + 5, currentY + 5, paint);
        }
        currentX += extraSize;
      }
    }
  }
  return Math.round(size + renderer.getLegendTextSize());
}
 
Example 4
Project: asaf-project   File: MonospacedTextWrappingUtils.java   View Source Code Vote up 2 votes
/**
 * Recursive method, takes a list of words and concatenates them in order and with spaces in between each word, up to a maximum line width of widthAvailable,
 * further lines are added to linesSoFar until all the wordsStillToWrap have been processed.
 *
 * @param linesSoFar
 * @param wordsStillToWrap
 * @param widthAvailable
 * @param paint            if null we will wrap based on number of charactersAvailable
 * @param leftToRight
 */
private static void extractWrappedLines(List<String> linesSoFar, List<String> wordsStillToWrap, float widthAvailable, Paint paint, int charactersAvailable, boolean leftToRight) {

    if (wordsStillToWrap.size() > 0) {

        //start by assuming all the words still to wrap will wrap into one line
        String nextLineAttempt = flattenWordsAddingSpaces(wordsStillToWrap);

        int charactersThatFit;

        if (paint == null) {
            charactersThatFit = nextLineAttempt.length() < charactersAvailable ? nextLineAttempt.length() : charactersAvailable;
        } else {
            charactersThatFit = paint.breakText(nextLineAttempt, leftToRight, widthAvailable, null);
        }


        if (charactersThatFit == nextLineAttempt.length()) { //we're done

            linesSoFar.add(nextLineAttempt);
            wordsStillToWrap.clear();

        } else { //just get the next line, then pass the remaining words to the method again recursively

            List<String> wordsForNextLine = new ArrayList<String>();

            int charactersUsed = -1;//to take into account that there is no space before the first word in a line

            while (wordsStillToWrap.size() > 0 && charactersUsed + 1 + wordsStillToWrap.get(0).length() <= charactersThatFit) {

                wordsForNextLine.add(wordsStillToWrap.get(0));
                charactersUsed += (1 + wordsStillToWrap.get(0).length());
                wordsStillToWrap.remove(0);
            }

            if (charactersUsed < 1) {//the next word must be very large so we need to take what we can off the front of it

                String massiveWord = wordsStillToWrap.get(0);

                if (charactersThatFit == 0) {//just in case
                    charactersThatFit = 1;
                }

                wordsForNextLine.add(massiveWord.substring(0, charactersThatFit));
                wordsStillToWrap.remove(0);

                if (massiveWord.length() > charactersThatFit) {
                    wordsStillToWrap.add(0, massiveWord.substring(charactersThatFit, massiveWord.length()));
                }
            }

            linesSoFar.add(flattenWordsAddingSpaces(wordsForNextLine));

            extractWrappedLines(linesSoFar, wordsStillToWrap, widthAvailable, paint, charactersAvailable, leftToRight);

        }
    }
}