Android 如何在画布上绘制文本?

Android 如何在画布上绘制文本?,android,canvas,drawable,Android,Canvas,Drawable,我正在尝试为android开发一个简单的饼图类。现在,它可以绘制标签和值的地图并绘制饼图。我还没有为饼图添加图例,我需要在屏幕角落的小矩形附近放置文本。感谢您的帮助,因为我是Android开发人员。您必须使用Canvas类的drawText方法 Paint paint = new Paint(); canvas.drawPaint(paint); paint.setColor(Color.BLACK); paint.setTextSize(16); canvas.drawText("My

我正在尝试为android开发一个简单的饼图类。现在,它可以绘制标签和值的地图并绘制饼图。我还没有为饼图添加图例,我需要在屏幕角落的小矩形附近放置文本。感谢您的帮助,因为我是Android开发人员。

您必须使用Canvas类的drawText方法

Paint paint = new Paint(); 
canvas.drawPaint(paint); 
paint.setColor(Color.BLACK); 
paint.setTextSize(16); 
canvas.drawText("My Text", x, y, paint); 
以下是关于它的相关文档:

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

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

为了便于说明,
TextPaint
StaticLayout
在这里使用之前就被实例化了。不过,在onDraw中这样做会影响性能。在绘制自己文本的自定义视图的上下文中显示它们。

这里曾经有另一个答案被删除,因为它只是一个链接。原来的链接是。代码基本上是一样的,但我去掉了非文本的绘图部分,并放大了尺寸,以便更好地适应现代屏幕密度

这只是展示了使用文本绘图可以做的一些事情

以下是更新的代码:

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();
        }
    }
}
我以后想尝试的其他东西是

另请参见给出下图的

帆布。drawPaint(油漆)在这里似乎是多余的。您可能还需要抗锯齿(
paint.setAntiAlias(true);
)和绘制样式(
paint.setStyle(paint.style.FILL);