在画布上用Android中不同长度的文本绘制弯曲文本 [英] Draw Curved Text on Canvas with different length of Text in android

查看:56
本文介绍了在画布上用Android中不同长度的文本绘制弯曲文本的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要实现在画布上的圆形路径上绘制文本的弯曲文本.

I need to implement curved text which draw text on circular path on canvas.

它确实使用绘制圆弧路径

It does draw circular path using

 canvas.drawTextOnPath(QUOTE, circle, 485, 20, tPaint);

但不适用于不同长度的文本.

but it is not working for different length of the text.

以下是我的课程,用于在canavs上绘制圆形文本.

Following is my Class to draw circular text on the canavs.

 public class CircularTextVie extends View {
        private  String QUOTE = "";
        private Path circle;
        private Paint cPaint;
        private Paint tPaint;

        public CircularTextVie(Context context) {
            super(context);
            circle = new Path();
            cPaint = new Paint(Paint.ANTI_ALIAS_FLAG);
            cPaint.setStyle(Paint.Style.STROKE);
            cPaint.setColor(Color.LTGRAY);
            cPaint.setStrokeWidth(3);
            tPaint = new Paint(Paint.ANTI_ALIAS_FLAG);
            tPaint.setStyle(Paint.Style.FILL_AND_STROKE);
            tPaint.setColor(Color.BLACK);
        }


        @Override
        protected void onDraw(Canvas canvas) {
            canvas.restore();
            int xPos =  (canvas.getWidth() /3);
            int yPos = (int) ((canvas.getHeight() / 3) - ((tPaint.descent() + tPaint.ascent()) / 3)) ;
            circle.addCircle(xPos, yPos, 150, Path.Direction.CW);


      canvas.drawTextOnPath(QUOTE, circle, 485, 20, tPaint);
            QUOTE="";

        }


    public void SetText(String text) {
       this.QUOTE = text;

    }

    public void SetTypeFace(Typeface face) {
       cPaint.setTypeface(face);
      tPaint.setTypeface(face);
    }


    public void SetColor(int color) {
        cPaint.setColor(color);
       tPaint.setColor(color);

    }

}

谢谢.

推荐答案

这可以通过基于文本宽度更改x和y位置来完成

This can be done by varying the x and y positions based on textwidth

定义变量

private Rect textBounds;
private int mTextWidth, mTextHeight,centerX,centerY;

在customview的构造函数中添加以下内容

Add the below in the constructor of customview

textBounds = new Rect();

tPaint.getTextBounds(QUOTE, 0, QUOTE.length(), textBounds);
mTextWidth = Math.round(tPaint.measureText(QUOTE.toString())); // Use measureText to calculate width
mTextHeight = textBounds.height(); // Use height from getTextBounds()

然后在onDraw中

canvas.drawCircle(centerX,centerY,150,mCirlcePaint);
circle.addCircle(centerX, centerY, 150, Path.Direction.CW); 
// Note the 0 that's y offset. textdraw at circumference of the circle. Changing y you probably need to change the radius as well i guess.
canvas.drawTextOnPath(QUOTE, circle, (centerX)-(mTextWidth / 2f), 0, tPaint);

centerX,centerY是圆的中心,即canvaswidht/2和canvasHeight/2.我画了一个圆圈供参考

centerX,centerY are the center of the circle ie canvaswidht/2 and canvasHeight/2. I drew a circle for reference

打招呼的结果

获取更大文本的结果

对于数字

对评论中的问题

涉及的数学是使用文本长度计算半圆 radius =(float)((mTextWidth)/(Math.PI)).如果文本长度大于画布,则需要减小文本大小或使用完整的圆 radius =(float)((mTextWidth)/(2 *(Math.PI))).很少有其他情况下您可以考虑正确绘制文本.

The math involved is in calculating the semi circle using text length radius = (float) ((mTextWidth) / (Math.PI)). If text length is greater than your canvas you need to reduce the text size or use the full circle radius = (float) ((mTextWidth) / (2*(Math.PI))). Few other edge case you can consider to draw the text properly.

公共类GraphicsView扩展了View {

public class GraphicsView extends View {

private String QUOTE = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
private Path circle;
private Paint mCirlcePaint;
private Paint tPaint;
private Rect textBounds;
private int mTextWidth, mTextHeight, centerX, centerY;

private float radius;

public GraphicsView(Context context) {
    super(context);
    circle = new Path();

    tPaint = new Paint(Paint.ANTI_ALIAS_FLAG);
    tPaint.setStyle(Paint.Style.FILL_AND_STROKE);
    tPaint.setColor(Color.BLACK);
    tPaint.setTextSize(100f);
    textBounds = new Rect();

    tPaint.getTextBounds(QUOTE, 0, QUOTE.length(), textBounds);
    mTextWidth = Math.round(tPaint.measureText(QUOTE.toString())); // Use measureText to calculate width
    mTextHeight = textBounds.height(); // Use height from getTextBounds()

    mCirlcePaint = new Paint();
    mCirlcePaint.setStyle(Paint.Style.FILL);
    mCirlcePaint.setColor(Color.GREEN);

    radius = (float) ((mTextWidth) / (Math.PI));

}

@Override
protected void onSizeChanged(int w, int h, int oldw, int oldh) {
    super.onSizeChanged(w, h, oldw, oldh);
    centerX = w / 2;
    centerY = h / 2;

}

@Override
protected void onDraw(Canvas canvas) {

    canvas.rotate(180, getWidth() / 2, getHeight() / 2);
    canvas.drawCircle(centerX, centerY, radius, mCirlcePaint);
    circle.addCircle(centerX, centerY, radius, Path.Direction.CW);
    canvas.drawTextOnPath(QUOTE, circle, 0, 0, tPaint);

   }

}

这篇关于在画布上用Android中不同长度的文本绘制弯曲文本的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆