android 中 canvas 文本的椭圆大小

ellipsize for the canvas text in android

我正在尝试做什么:我正在尝试在文本的 canavs 中的 7 个字符后设置 ...

如何实现?

private void drawText(Canvas canvas, float tmpAngle, float sweepAngle, String mStr) {
        float cx = (mRadius) / 2 + mPadding;
        float cy = (mRadius) / 2 + mPadding;
        float radius = mRadius / 2 + mPadding;
        float x = cx - radius + (mPadding * 2);
        float y = cy;
        float textWidth = radius - (mPadding * 10);
        TextPaint textPaint = new TextPaint();
        textPaint.set(this.mTextPaint);
        textPaint.setColor(Color.WHITE);
        Typeface plain = Typeface.createFromAsset(getContext().getAssets(), "fonts/AvenirLTStd-Heavy.otf");
        Typeface bold = Typeface.create(plain, Typeface.BOLD);
        textPaint.setTypeface(bold);

        float angle = tmpAngle + sweepAngle / 2;

        canvas.save();
        canvas.rotate(180+(angle), cx, cy); // +180 for start from right
        canvas.drawText(mStr, x, y, textPaint);
        canvas.restore();
    }

你可以这样做,

TextPaint textPaint = new TextPaint();
// textPaint attributes
CharSequence ellipsizedText = TextUtils.ellipsize("Your text", textPaint, width, 
TextUtils.TruncateAt.END);
canvas.drawText(ellipsizedText, 0, ellipsizedText.length(), x0, y0, textPaint);

但在您的情况下,您需要在 7 个字符后使用它,最好只检查文本长度并在文本后附加 ...