关于android:如何在画布上绘制文本?

How to draw text on canvas?

我正在尝试为Android开发一个简单的饼图类。 目前,它可以绘制标签和值的地图并绘制饼图。 我还没有为饼图添加图例,这是我需要将文本放置在屏幕角落小矩形附近的位置。 任何帮助表示赞赏,因为我是Android开发人员的新手。


您将必须使用Canvas类的drawText方法。

1
2
3
4
5
Paint paint = new Paint();
canvas.drawPaint(paint);
paint.setColor(Color.BLACK);
paint.setTextSize(16);
canvas.drawText("My Text", x, y, paint);

这是有关的相关文档:

http://developer.android.com/reference/android/graphics/Canvas.html#drawText(java.lang.String,float,float,android.graphics.Paint)


这里曾经有另一个答案被删除,因为它只是一个链接。原始链接在这里。代码基本相同,但是我取出了非文本绘图部分,并按比例放大了尺寸,以便在现代屏幕密度下更好地工作。

这仅显示了您可以使用文本绘图进行的一些操作。

enter image description here

这是更新的代码:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
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


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

1
2
3
4
5
6
7
8
9
10
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中这样做会损害性能。这是一个更好的示例,在绘制自己的文本的自定义视图的上下文中显示它们。