canvas.drawText问题

时间:2010-07-19 04:32:58

标签: java android

我使用canvas.drawtext在android中的自定义视图中绘制文本。我需要改变颜色,并希望文本对齐。例如,我想打印10,10,100,20矩形的黄色和文本颜色红色和右对齐的文本。我怎么能这样做?

2 个答案:

答案 0 :(得分:2)

public void onDraw(Canvas c) {
    String text = "red right-aligned text";
    Paint paint = new Paint();
    paint.setStyle(Paint.Style.FILL_AND_STROKE);

    int rectX = 10;
    int rectY = 10;
    int rectWidth = 100;
    int rectHeight = 20;
    float textWidth = paint.measureText(text); // measureText method of Paint

    paint.setColor(Color.YELLOW);
    c.drawRect(rectX, rectY, rectX + rectWidth, rectY + rectHeight, paint);

    paint.setColor(Color.RED);
    c.drawText(text, rectX + rectWidth - textWidth, rectY, paint);
}

答案 1 :(得分:0)

gc.setBackground(...)
gc.fillRectangle(...)
gc.setForeground(...)
gc.drawText(...)