将字母间距设置为drawTextOnPath

时间:2017-05-29 13:15:47

标签: android android-canvas android-custom-view android-paint

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

@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 );
    }

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

1 个答案:

答案 0 :(得分:0)

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

  

设置文本的油漆字母间距。默认值为0.该值位于&#39; EM&#39;单位。轻微膨胀的典型值约为0.05。负值会收紧文字。

您也可以see TextView#setLetterSpacing()如何实施。