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

The following examples show how to use android.graphics.Paint#getTextWidths() . 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: SpannableStringBuilder.java    From android_9.0.0_r45 with Apache License 2.0 6 votes vote down vote up
/**
 * Don't call this yourself -- exists for Paint to use internally.
 * {@hide}
 */
public int getTextWidths(int start, int end, float[] widths, Paint p) {
    checkRange("getTextWidths", start, end);

    int ret;

    if (end <= mGapStart) {
        ret = p.getTextWidths(mText, start, end - start, widths);
    } else if (start >= mGapStart) {
        ret = p.getTextWidths(mText, start + mGapLength, end - start, widths);
    } else {
        char[] buf = TextUtils.obtain(end - start);

        getChars(start, end, buf, 0);
        ret = p.getTextWidths(buf, 0, end - start, widths);
        TextUtils.recycle(buf);
    }

    return ret;
}
 
Example 2
Source File: FixedWidthTimestampSpan.java    From revolution-irc with GNU General Public License v3.0 6 votes vote down vote up
@Override
public int getSize(@NonNull Paint paint, CharSequence text, int start, int end,
                   @Nullable Paint.FontMetricsInt fm) {
    paint.getTextWidths(MEASURE_NUMBER_CHARS, mNumberWidths);
    float mw = 0.f;
    char ld = '0';
    for (int i = MEASURE_NUMBER_CHARS.length() - 1; i >= 0; --i) {
        if (mNumberWidths[i] > mw) {
            mNumberWidths[i] = mw;
            ld = MEASURE_NUMBER_CHARS.charAt(i);
        }
    }
    CharSequence s = text.subSequence(start - mPreOffset, start);
    String rs = sMatchNumbersRegex.matcher(s).replaceAll(String.valueOf(ld));
    return (int) (paint.measureText(rs) - paint.measureText(s, 0, s.length()));
}
 
Example 3
Source File: VMDimen.java    From VMLibrary with Apache License 2.0 5 votes vote down vote up
/**
 * 获取文字的宽度
 *
 * @param paint 绘制文字的画笔
 * @param str   需要计算宽度的字符串
 * @return 返回字符串宽度
 */
public static float getTextWidth(Paint paint, String str) {
    float textWidth = 0;
    if (str != null && str.length() > 0) {
        // 记录字符串中每个字符宽度的数组
        float[] widths = new float[str.length()];
        // 获取字符串中每个字符的宽度到数组
        paint.getTextWidths(str, widths);
        for (int i = 0; i < str.length(); i++) {
            textWidth += (float) Math.ceil(widths[i]);
        }
    }
    return textWidth;
}
 
Example 4
Source File: WheelView.java    From Android-PickerView with Apache License 2.0 5 votes vote down vote up
public int getTextWidth(Paint paint, String str) { //calculate text width
    int iRet = 0;
    if (str != null && str.length() > 0) {
        int len = str.length();
        float[] widths = new float[len];
        paint.getTextWidths(str, widths);
        for (int j = 0; j < len; j++) {
            iRet += (int) Math.ceil(widths[j]);
        }
    }
    return iRet;
}
 
Example 5
Source File: WheelView.java    From Android-PickerView with Apache License 2.0 5 votes vote down vote up
public int getTextWidth(Paint paint, String str) { //calculate text width
    int iRet = 0;
    if (str != null && str.length() > 0) {
        int len = str.length();
        float[] widths = new float[len];
        paint.getTextWidths(str, widths);
        for (int j = 0; j < len; j++) {
            iRet += (int) Math.ceil(widths[j]);
        }
    }
    return iRet;
}
 
Example 6
Source File: WheelView.java    From AndroidPicker with MIT License 5 votes vote down vote up
private int obtainTextWidth(Paint paint, String str) {
    int iRet = 0;
    if (str != null && str.length() > 0) {
        int len = str.length();
        float[] widths = new float[len];
        paint.getTextWidths(str, widths);
        for (int j = 0; j < len; j++) {
            iRet += (int) Math.ceil(widths[j]);
        }
    }
    return iRet;
}
 
Example 7
Source File: DisplayUtils.java    From NewXmPluginSDK with Apache License 2.0 5 votes vote down vote up
public static int getTextWidth(Paint paint, String str) {
    int iRet = 0;
    if (str != null && str.length() > 0) {
        int len = str.length();
        float[] widths = new float[len];
        paint.getTextWidths(str, widths);
        for (int j = 0; j < len; j++) {
            iRet += (int) Math.ceil(widths[j]);
        }
    }
    return iRet;
}
 
Example 8
Source File: WheelView.java    From AndroidAnimationExercise with Apache License 2.0 5 votes vote down vote up
public int getTextWidth(Paint paint, String str)
{
	int iRet = 0;
	if (str != null && str.length() > 0)
	{
		int len = str.length();
		float[] widths = new float[len];
		paint.getTextWidths(str, widths);
		for (int j = 0; j < len; j++)
		{
			iRet += (int) Math.ceil(widths[j]);
		}
	}
	return iRet;
}
 
Example 9
Source File: WheelView.java    From Android-PickerDialog with Apache License 2.0 5 votes vote down vote up
public int getTextWidth(Paint paint, String str) {
    int iRet = 0;
    if (str != null && str.length() > 0) {
        int len = str.length();
        float[] widths = new float[len];
        paint.getTextWidths(str, widths);
        for (int j = 0; j < len; j++) {
            iRet += (int) Math.ceil(widths[j]);
        }
    }
    return iRet;
}
 
Example 10
Source File: ViewUtils.java    From android with MIT License 5 votes vote down vote up
/**
 * 获取文字的宽度
 *
 * @param paint
 * @param str
 * @return
 */
public static int getTextWidth(Paint paint, String str) {
    int iRet = 0;
    if (str != null && str.length() > 0) {
        int len = str.length();
        float[] widths = new float[len];
        paint.getTextWidths(str, widths);
        for (int j = 0; j < len; j++) {
            iRet += (int) Math.ceil(widths[j]);
        }
    }
    return iRet;
}
 
Example 11
Source File: SunView.java    From OpenWeatherPlus-Android with Apache License 2.0 5 votes vote down vote up
/**
 * 精确计算文字宽度
 *
 * @param paint 画笔
 * @param str   字符串文本
 */
public static int getTextWidth(Paint paint, String str) {
    int iRet = 0;
    if (str != null && str.length() > 0) {
        int len = str.length();
        float[] widths = new float[len];
        paint.getTextWidths(str, widths);
        for (int j = 0; j < len; j++) {
            iRet += (int) Math.ceil(widths[j]);
        }
    }
    return iRet;
}
 
Example 12
Source File: RxShoppingView.java    From RxTools-master with Apache License 2.0 5 votes vote down vote up
private int getTextWidth(Paint paint, String str) {
    int iRet = 0;
    if (str != null && str.length() > 0) {
        int len = str.length();
        float[] widths = new float[len];
        paint.getTextWidths(str, widths);
        for (int j = 0; j < len; j++) {
            iRet += (int) Math.ceil(widths[j]);
        }
    }
    return iRet;
}
 
Example 13
Source File: WheelView.java    From AndroidFrame with Apache License 2.0 5 votes vote down vote up
public int getTextWidth(Paint paint, String str) {//计算文字宽度
    int iRet = 0;
    if (str != null && str.length() > 0) {
        int len = str.length();
        float[] widths = new float[len];
        paint.getTextWidths(str, widths);
        for (int j = 0; j < len; j++) {
            iRet += (int) Math.ceil(widths[j]);
        }
    }
    return iRet;
}
 
Example 14
Source File: DegreeSeekBar.java    From imsdk-android with MIT License 5 votes vote down vote up
private void init() {
    mPointColor = ContextCompat.getColor(getContext(), R.color.easy_photos_fg_primary);
    mTextColor = ContextCompat.getColor(getContext(), R.color.easy_photos_fg_primary);
    mCenterTextColor = ContextCompat.getColor(getContext(), R.color.easy_photos_fg_accent);

    mPointPaint = new Paint(Paint.ANTI_ALIAS_FLAG);
    mPointPaint.setStyle(Paint.Style.STROKE);
    mPointPaint.setColor(mPointColor);
    mPointPaint.setStrokeWidth(2);

    mTextPaint = new Paint();
    mTextPaint.setColor(mTextColor);
    mTextPaint.setStyle(Paint.Style.STROKE);
    mTextPaint.setAntiAlias(true);
    mTextPaint.setTextSize(24f);
    mTextPaint.setTextAlign(Paint.Align.LEFT);
    mTextPaint.setAlpha(100);

    mFontMetrics = mTextPaint.getFontMetricsInt();

    mTextWidths = new float[1];
    mTextPaint.getTextWidths("0", mTextWidths);

    mCirclePaint = new Paint();
    mCirclePaint.setStyle(Paint.Style.FILL);
    mCirclePaint.setAlpha(255);
    mCirclePaint.setAntiAlias(true);
}
 
Example 15
Source File: TextUtil.java    From TextPathView with MIT License 5 votes vote down vote up
public static int getTextWidth(Paint paint, String str) {
    int iRet = 0;
    if (str != null && str.length() > 0) {
        int len = str.length();
        float[] widths = new float[len];
        paint.getTextWidths(str, widths);
        for (int j = 0; j < len; j++) {
            iRet += (int) Math.ceil(widths[j]);
        }
    }
    return iRet;
}
 
Example 16
Source File: BezierSeekBar.java    From BezierSeekBar with Apache License 2.0 5 votes vote down vote up
private float getTextWidth(Paint paint, String str) {
    float iRet = 0;
    if (str != null && str.length() > 0) {
        int len = str.length();
        float[] widths = new float[len];
        paint.getTextWidths(str, widths);
        for (int j = 0; j < len; j++) {
            iRet += (float) Math.ceil(widths[j]);
        }
    }
    return iRet;
}
 
Example 17
Source File: DegreeSeekBar.java    From EasyPhotos with Apache License 2.0 5 votes vote down vote up
private void init() {
    mPointColor = ContextCompat.getColor(getContext(), R.color.easy_photos_fg_primary);
    mTextColor = ContextCompat.getColor(getContext(), R.color.easy_photos_fg_primary);
    mCenterTextColor = ContextCompat.getColor(getContext(), R.color.easy_photos_fg_accent);

    mPointPaint = new Paint(Paint.ANTI_ALIAS_FLAG);
    mPointPaint.setStyle(Paint.Style.STROKE);
    mPointPaint.setColor(mPointColor);
    mPointPaint.setStrokeWidth(2);

    mTextPaint = new Paint();
    mTextPaint.setColor(mTextColor);
    mTextPaint.setStyle(Paint.Style.STROKE);
    mTextPaint.setAntiAlias(true);
    mTextPaint.setTextSize(24f);
    mTextPaint.setTextAlign(Paint.Align.LEFT);
    mTextPaint.setAlpha(100);

    mFontMetrics = mTextPaint.getFontMetricsInt();

    mTextWidths = new float[1];
    mTextPaint.getTextWidths("0", mTextWidths);

    mCirclePaint = new Paint();
    mCirclePaint.setStyle(Paint.Style.FILL);
    mCirclePaint.setAlpha(255);
    mCirclePaint.setAntiAlias(true);
}
 
Example 18
Source File: X8SliderbarView.java    From FimiX8-RE with MIT License 5 votes vote down vote up
public int getTextWidth(Paint mPaint, String str) {
    float iSum = 0.0f;
    if (!(str == null || str.equals(""))) {
        int len = str.length();
        float[] widths = new float[len];
        mPaint.getTextWidths(str, widths);
        for (int i = 0; i < len; i++) {
            iSum = (float) (((double) iSum) + Math.ceil((double) widths[i]));
        }
    }
    return (int) iSum;
}
 
Example 19
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;

                }
            }
        }
    }

}
 
Example 20
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());
}