3

我正在使用以下代码制作一个微调器。

@Override
    public void onDraw(Canvas canvas)
    {
        super.onDraw(canvas);

        for (int i = 0; i < segmentColors.length; i ++)
        {
            drawDataSet(canvas, i);
        }

        canvas.drawCircle(getWidth()/2, getHeight()/2, getWidth() - getWidth()/2 - dpToPx(5), mTransparentCirclePaint);
    }


protected void drawDataSet(Canvas canvas, int position)
    {
        int color = segmentColors[position];
        float mainArcAngle = ((float) 360/ (float)segmentColors.length);

        Paint paint = new Paint();
        paint.setStyle(Paint.Style.FILL);

        if ( position%2 == 0)
            paint.setColor(color);
        else
            paint.setColor(Color.parseColor("#009f2f"));

        RectF rect = new RectF(0, 0, getWidth(), getWidth());
        canvas.drawArc(rect, (mainArcAngle * position), mainArcAngle, true, paint);

        TextPaint mTextPaint = new TextPaint(Paint.ANTI_ALIAS_FLAG);
        mTextPaint.setColor(Color.BLACK);
        mTextPaint.setAntiAlias(true);
        mTextPaint.setTypeface(Typeface.MONOSPACE);
        mTextPaint.setTextAlign(Paint.Align.CENTER);
        mTextPaint.setTextSize(18f);

        Path path = new Path();
        path.addArc(rect, (mainArcAngle * position), mainArcAngle);

        //PathMeasure pm = new PathMeasure(path, false);
        //DynamicLayout layout = new DynamicLayout(values[position], values[position], mTextPaint, (int) pm.getLength(), Layout.Alignment.ALIGN_CENTER, 1, 0, true);
        //canvas.save();
        //layout.draw(canvas);
        //canvas.restore();
        canvas.drawTextOnPath(values[position], path, 0 , getWidth() / 6 , mTextPaint );
    }

由于间距很小,在弧内写入的文本有点压缩。如何使它成为多行或增加字母间距?

4

1 回答 1

7

从 API 21 开始,您可以使用Paint#setLetterSpacing(float)

为文本设置油漆的字母间距。默认值为 0。该值以“EM”为单位。轻微膨胀的典型值将在 0.05 左右。负值会使文本变紧。

您可能还会看到它是如何TextView#setLetterSpacing()实现的。

于 2017-05-29T13:38:00.850 回答