如何在画布上绘制文本?

37

我正在尝试为安卓开发一个简单的饼图类。目前,它可以接受标签和数值映射,并绘制饼图。我还需要添加饼状图例,其中需要在屏幕角落附近的小矩形上放置文本。因为我是新手,所以需要任何帮助。

3个回答

55

2
canvas.drawPaint(paint); 在这里似乎是多余的。您还可能需要抗锯齿 (paint.setAntiAlias(true);) 和绘制样式 (paint.setStyle(Paint.Style.FILL);)。 - Chris Knight

15

这里曾经有另一个答案,但因为它只是一个链接而被删除。原始链接在这里。代码基本相同,但我去掉了非文本绘制部分,并将大小放大以更好地适应现代屏幕密度。

这只是展示了一些你可以用文本绘制做的事情。

enter image description here

以下是更新后的代码:

public class MainActivity extends AppCompatActivity {

    DemoView demoview;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        demoview = new DemoView(this);
        setContentView(demoview);
    }

    private class DemoView extends View {
        public DemoView(Context context){
            super(context);
        }

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

            // custom drawing code here
            // remember: y increases from top to bottom
            // x increases from left to right
            int x = 0;
            int y = 0;
            Paint paint = new Paint();
            paint.setStyle(Paint.Style.FILL);

            canvas.save();
            canvas.translate(100, 200);

            // make the entire canvas white
            canvas.drawColor(Color.WHITE);

            // draw some text using STROKE style
            paint.setStyle(Paint.Style.STROKE);
            paint.setStrokeWidth(1);
            paint.setColor(Color.MAGENTA);
            paint.setTextSize(100);
            canvas.drawText("Style.STROKE", 0, 0, paint);

            canvas.translate(0, 200);

            // draw some text using FILL style
            paint.setStyle(Paint.Style.FILL);
            //turn antialiasing on
            paint.setAntiAlias(true);
            //paint.setTextSize(30);
            canvas.drawText("Style.FILL", 0, 0, paint);

            canvas.translate(0, 200);

            // draw some rotated text
            // get text width and height
            // set desired drawing location
            x = 75;
            y = 185;
            paint.setColor(Color.GRAY);
            //paint.setTextSize(25);
            String str2rotate = "Rotated!";

            // draw bounding rect before rotating text
            Rect rect = new Rect();
            paint.getTextBounds(str2rotate, 0, str2rotate.length(), rect);
            canvas.translate(x, y);
            paint.setStyle(Paint.Style.FILL);
            // draw unrotated text
            canvas.drawText("!Rotated", 0, 0, paint);
            paint.setStyle(Paint.Style.STROKE);
            canvas.drawRect(rect, paint);
            // undo the translate
            canvas.translate(-x, -y);

            // rotate the canvas on center of the text to draw
            canvas.rotate(-45, x + rect.exactCenterX(),
                    y + rect.exactCenterY());
            // draw the rotated text
            paint.setStyle(Paint.Style.FILL);
            canvas.drawText(str2rotate, x, y, paint);

            //undo the translation and rotation
            canvas.restore();
        }
    }
}

我想以后还要尝试沿路径绘制文字

此外,还可以看看这里提供的完整答案,其中包含以下图片。

enter image description here


2

在画布上绘制文本的另一种(可以说更好的)方法是使用 StaticLayout。这种方式可以处理需要换行的多行文本。

String text = "This is some text.";

TextPaint textPaint = new TextPaint();
textPaint.setAntiAlias(true);
textPaint.setTextSize(16 * getResources().getDisplayMetrics().density);
textPaint.setColor(0xFF000000);

int width = (int) textPaint.measureText(text);
StaticLayout staticLayout = new StaticLayout(text, textPaint, (int) width, Layout.Alignment.ALIGN_NORMAL, 1.0f, 0, false);
staticLayout.draw(canvas);

为了说明,这里在使用TextPaintStaticLayout之前进行了实例化。但是,在onDraw中这样做会影响性能。这里有一个更好的示例,展示了它们在自定义视图上下文中绘制其自己的文本。


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接