mynameisanthpny
mynameisanthpny

Reputation: 689

canvas.drawText question

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 in a 10, 10, 100, 20 rectangle of color yellow and text color red and right aligned. how can i do that ?

Upvotes: 0

Views: 6291

Answers (2)

Andy Zhang
Andy Zhang

Reputation: 8315

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

Upvotes: 2

Daniel
Daniel

Reputation: 28074

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

Upvotes: 0

Related Questions