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 ?
A:
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 = p.measureText(text);
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);
}
Andy Zhang
2010-07-19 04:46:48
He asked for help on changing the back colour. I think your assumption ("background color is already yellow") is not good in this case.
Andreas_D
2010-07-19 06:16:22
Oops, yes, my mistake. I'll change my response.
Andy Zhang
2010-07-19 09:27:57
A:
gc.setBackground(...)
gc.fillRectangle(...)
gc.setForeground(...)
gc.drawText(...)
Daniel
2010-07-19 06:22:23