开发者

canvas.drawText question

开发者 https://www.devze.com 2023-01-08 08:34 出处:网络
i am drawing text in my custom view in android using canvas.drawtext. i need to change back color, and want text right aligned. for example i want to print the text i开发者_运维知识库n a 10, 10, 100,

i am drawing text in my custom view in android using canvas.drawtext. i need to change back color, and want text right aligned. for example i want to print the text i开发者_运维知识库n a 10, 10, 100, 20 rectangle of color yellow and text color red and right aligned. how can i do that ?


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


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

精彩评论

暂无评论...
验证码 换一张
取 消