将文本值放在所需位置

时间:2011-04-11 13:01:49

标签: android graphics

我画了一个图形对象,比如矩形。我想在矩形的每个角落写一些文字。怎么做到这一点?

private static class SimpleView extends View {
    private ShapeDrawable mDrawable = new ShapeDrawable();

    public SimpleView(Context context) {
        super(context);
        setFocusable(true);
        this.mDrawable = new ShapeDrawable(new RectShape());
        this.mDrawable.getPaint().setColor(0xFF0F00FF);


}

             @Override
        protected void onDraw(Canvas canvas) {
            int x1 = 50;
            int y1 = 150;
            int width = 400;
            int height = 50;
            this.mDrawable.setBounds(x1, y1, x1 + width, y1 + height);
            this.mDrawable.draw(canvas);

            int x = 0;
            int y = 0;
            Paint paint = new Paint();
            paint.setStyle(Paint.Style.FILL);

2 个答案:

答案 0 :(得分:1)

如果您使用的是Canvas,则只需使用drawText()方法。

drawText(String text, int start, int end, float x, float y, Paint paint) 

来源:http://developer.android.com/reference/android/graphics/Canvas.html

答案 1 :(得分:0)

canvas.drawText与角落的坐标一起使用,并将Paint设置为适当的对齐方式。即你在每个角落都绘制了文字,右边的角落有paint.align = RIGHT,左边角有paint.align = LEFT。这样,文本被绘制到广场的一侧。