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

The following examples show how to use android.graphics.Paint#breakText() . 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: EllipsizeLineSpan.java    From OpenHub with GNU General Public License v3.0 6 votes vote down vote up
@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
Source File: RadialMenuView.java    From talkback with Apache License 2.0 6 votes vote down vote up
private static String getEllipsizedText(Paint paint, String title, float maxWidth) {
  final float textWidth = paint.measureText(title);
  if (textWidth <= maxWidth) {
    return title;
  }

  // Find the maximum length with an ellipsis.
  final float ellipsisWidth = paint.measureText(ELLIPSIS);
  final int length = paint.breakText(title, true, (maxWidth - ellipsisWidth), null);

  // Try to land on a word break.
  // TODO: Use breaking iterator for better i18n support.
  final int space = title.lastIndexOf(' ', length);
  if (space > 0) {
    return title.substring(0, space) + ELLIPSIS;
  }

  // Otherwise, cut off characters.
  return title.substring(0, length) + ELLIPSIS;
}
 
Example 3
Source File: LVViewManagerPlugin.java    From VideoOS-Android-SDK with GNU General Public License v3.0 5 votes vote down vote up
@Override
public Varargs invoke(Varargs args) {
    final int fixIndex = VenvyLVLibBinder.fixIndex(args);
    if (args.narg() > fixIndex) {
        String text = LuaUtil.getString(args, fixIndex + 1);
        final Float width = LuaUtil.getFloat(args, fixIndex + 2);
        Float size = LuaUtil.getFloat(args, fixIndex + 3);
        float mathWidth = 0f;
        float mathHeight = 0f;
        if (!TextUtils.isEmpty(text)) {
            Paint paint = new Paint();
            if (size != null) {
                paint.setTextSize(size);
            }
            float widthMeasureSpec = paint.measureText(text);
            Rect bounds = new Rect();
            paint.getTextBounds(text, 0, text.length(), bounds);
            float hightMeasureSpec = bounds.height();
            if (width >= widthMeasureSpec) {
                mathWidth = widthMeasureSpec;
            } else {
                mathWidth = width;
            }
            int charNumInThisLine = paint.breakText(text, 0, text.length(), true, mathWidth, null);
            if (charNumInThisLine >= text.length()) {
                mathHeight = (float) (hightMeasureSpec * (Math.ceil(text.length() / charNumInThisLine)));
            } else {
                mathHeight = (float) (hightMeasureSpec * (Math.ceil(text.length() / charNumInThisLine) + 1));
            }
        }
        LuaValue[] luaValue = new LuaValue[]{LuaValue.valueOf(mathWidth), LuaValue.valueOf(mathHeight)};
        return LuaValue.varargsOf(luaValue);
    }
    return valueOf(0);
}
 
Example 4
Source File: ServiceUpdate.java    From rss with GNU General Public License v3.0 5 votes vote down vote up
private static
String fitToScreen(Resources resources, String content, int ind, float extra)
{
    // ind == 0 is the title, ind == 1 is the link.
    int size = 0 == ind ? R.dimen.item_title_size : R.dimen.item_link_size;
    int color = 0 == ind ? R.color.item_title_color : R.color.item_link_color;

    Paint paint = ViewFeedItem.configurePaint(resources, size, color);

    int chars = paint.breakText(content, true, USABLE_WIDTH_TEXT - extra, null);
    int space = content.lastIndexOf(' ', chars);

    return content.substring(0, -1 == space ? chars : space);
}
 
Example 5
Source File: CardFaceView.java    From CardView with Apache License 2.0 5 votes vote down vote up
protected void drawTextInRectF(String text, float offsetX, float offsetY, Canvas canvas, RectF r, Paint textPaint) {
	textPaint.setTextAlign(Align.CENTER);
	float width = r.width();

	int numOfChars = textPaint.breakText(text, true, width, null);
	int start = (text.length() - numOfChars) / 2;
	canvas.drawText(text, start, start + numOfChars, r.centerX() + offsetX,
			r.centerY() + offsetY, textPaint);
}
 
Example 6
Source File: StringAdapter.java    From FriendBook with GNU General Public License v3.0 4 votes vote down vote up
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 7
Source File: AbstractChart.java    From MiBandDecompiled with Apache License 2.0 4 votes vote down vote up
protected int drawLegend(Canvas canvas, DefaultRenderer defaultrenderer, String as[], int i, int j, int k, int l, 
        int i1, int j1, Paint paint, boolean flag)
{
    float f = 32F;
    if (defaultrenderer.isShowLegend())
    {
        float f1 = i;
        float f2 = f + (float)((k + i1) - j1);
        paint.setTextAlign(android.graphics.Paint.Align.LEFT);
        paint.setTextSize(defaultrenderer.getLegendTextSize());
        int k1 = Math.min(as.length, defaultrenderer.getSeriesRendererCount());
        int l1 = 0;
        while (l1 < k1) 
        {
            SimpleSeriesRenderer simpleseriesrenderer = defaultrenderer.getSeriesRendererAt(l1);
            float f3 = getLegendShapeWidth(l1);
            float f4;
            if (simpleseriesrenderer.isShowLegendItem())
            {
                String s = as[l1];
                float af[];
                float f5;
                int i2;
                if (as.length == defaultrenderer.getSeriesRendererCount())
                {
                    paint.setColor(simpleseriesrenderer.getColor());
                } else
                {
                    paint.setColor(0xffcccccc);
                }
                af = new float[s.length()];
                paint.getTextWidths(s, af);
                f5 = 0.0F;
                i2 = af.length;
                for (int j2 = 0; j2 < i2; j2++)
                {
                    f5 += af[j2];
                }

                float f6 = f5 + (10F + f3);
                float f7 = f1 + f6;
                if (l1 > 0 && getExceed(f7, defaultrenderer, j, l))
                {
                    f1 = i;
                    f2 += defaultrenderer.getLegendTextSize();
                    float f9 = f + defaultrenderer.getLegendTextSize();
                    f7 = f1 + f6;
                    f4 = f9;
                } else
                {
                    f4 = f;
                }
                if (getExceed(f7, defaultrenderer, j, l))
                {
                    float f8 = (float)j - f1 - f3 - 10F;
                    if (isVertical(defaultrenderer))
                    {
                        f8 = (float)l - f1 - f3 - 10F;
                    }
                    int k2 = paint.breakText(s, true, f8, af);
                    s = (new StringBuilder()).append(s.substring(0, k2)).append("...").toString();
                }
                if (!flag)
                {
                    drawLegendShape(canvas, simpleseriesrenderer, f1, f2, l1, paint);
                    drawString(canvas, s, 5F + (f1 + f3), f2 + 5F, paint);
                }
                f1 += f6;
            } else
            {
                f4 = f;
            }
            l1++;
            f = f4;
        }
    }
    return Math.round(f + defaultrenderer.getLegendTextSize());
}
 
Example 8
Source File: ServiceUpdate.java    From rss with GNU General Public License v3.0 4 votes vote down vote up
private static
void setDesLines(Resources resources, FeedItem feedItem, CharSequence content)
{
    Paint paint = ViewFeedItem.configurePaint(resources, R.dimen.item_description_size, R.color.item_description_color);

    List<String> lines = new ArrayList<String>(Arrays.asList(Patterns.LINE.split(content)));

    int j = 0;

    for(int x = 0; 3 > x; x++)
    {
        // Skip any empty lines.
        while(null != lines && j < lines.size() && lines.get(j).trim().isEmpty())
        {
            j++;
        }
        if(j == lines.size())
        {
            break;
        }

        String currentLine = lines.get(j).trim();

        int index = paint.breakText(currentLine, true, USABLE_WIDTH_TEXT, null);

        if(currentLine.length() == index)
        {
            feedItem.m_desLines[x] = currentLine;
        }
        else
        {
            // Break at the closest ' ' - 1 (some padding).
            int space = currentLine.lastIndexOf(' ', index - 1);

            // TODO if no space add a hyphen.
            index = -1 == space ? index : space;

            feedItem.m_desLines[x] = currentLine.substring(0, index);

            // Add the remaining to the next line.
            if(j + 1 < lines.size())
            {
                lines.set(j + 1, currentLine.substring(index) + lines.get(j + 1));
            }
            else
            {
                lines.add(currentLine.substring(index));
            }
        }

        j++;
    }
}