Skip to main content
added 58 characters in body
Source Link
WarrenFaith
  • 57.7k
  • 25
  • 139
  • 152

Based on steelbytes' response, the updated code would look something like:

void drawHelloRectangle(Canvas c, int topLeftX, int topLeftY, int width, int height) { Paint mPaint = new Paint(); // height of 'Hello World'; height0.7 looks good int fontHeight = (int)(height0.7);

void drawHelloRectangle(Canvas c, int topLeftX, int topLeftY, int width, int height) { Paint mPaint = new Paint(); // height of 'Hello World'; height*0.7 looks good int fontHeight = (int)(height*0.7); mPaint.setColor(COLOR_RED);  mPaint.setStyle(Style.FILL);  c.drawRect( topLeftX, topLeftY, topLeftX+width, topLeftY+height, mPaint);  mPaint.setTextSize(fontHeight);  mPaint.setColor(COLOR_BLACK);  mPaint.setTextAlign(Align.CENTER);  String textToDraw = new String("Hello World");  Rect bounds = new Rect();  mPaint.getTextBounds(textToDraw, 0, textToDraw.length(), bounds);  c.drawText(textToDraw, topLeftX+width/2, topLeftY+height/2+(bounds.bottom-bounds.top)/2, mPaint); } 

}

Based on steelbytes' response, the updated code would look something like:

void drawHelloRectangle(Canvas c, int topLeftX, int topLeftY, int width, int height) { Paint mPaint = new Paint(); // height of 'Hello World'; height0.7 looks good int fontHeight = (int)(height0.7);

mPaint.setColor(COLOR_RED); mPaint.setStyle(Style.FILL); c.drawRect( topLeftX, topLeftY, topLeftX+width, topLeftY+height, mPaint); mPaint.setTextSize(fontHeight); mPaint.setColor(COLOR_BLACK); mPaint.setTextAlign(Align.CENTER); String textToDraw = new String("Hello World"); Rect bounds = new Rect(); mPaint.getTextBounds(textToDraw, 0, textToDraw.length(), bounds); c.drawText(textToDraw, topLeftX+width/2, topLeftY+height/2+(bounds.bottom-bounds.top)/2, mPaint); 

}

Based on steelbytes' response, the updated code would look something like:

void drawHelloRectangle(Canvas c, int topLeftX, int topLeftY, int width, int height) { Paint mPaint = new Paint(); // height of 'Hello World'; height*0.7 looks good int fontHeight = (int)(height*0.7); mPaint.setColor(COLOR_RED);  mPaint.setStyle(Style.FILL);  c.drawRect( topLeftX, topLeftY, topLeftX+width, topLeftY+height, mPaint);  mPaint.setTextSize(fontHeight);  mPaint.setColor(COLOR_BLACK);  mPaint.setTextAlign(Align.CENTER);  String textToDraw = new String("Hello World");  Rect bounds = new Rect();  mPaint.getTextBounds(textToDraw, 0, textToDraw.length(), bounds);  c.drawText(textToDraw, topLeftX+width/2, topLeftY+height/2+(bounds.bottom-bounds.top)/2, mPaint); } 
Source Link

Based on steelbytes' response, the updated code would look something like:

void drawHelloRectangle(Canvas c, int topLeftX, int topLeftY, int width, int height) { Paint mPaint = new Paint(); // height of 'Hello World'; height0.7 looks good int fontHeight = (int)(height0.7);

mPaint.setColor(COLOR_RED); mPaint.setStyle(Style.FILL); c.drawRect( topLeftX, topLeftY, topLeftX+width, topLeftY+height, mPaint); mPaint.setTextSize(fontHeight); mPaint.setColor(COLOR_BLACK); mPaint.setTextAlign(Align.CENTER); String textToDraw = new String("Hello World"); Rect bounds = new Rect(); mPaint.getTextBounds(textToDraw, 0, textToDraw.length(), bounds); c.drawText(textToDraw, topLeftX+width/2, topLeftY+height/2+(bounds.bottom-bounds.top)/2, mPaint); 

}