Centering text on a bitmap?

Tutorials concerning the OpenGL® ES cross-platform API for full-function 2D and 3D graphics on the Google-Android platform.

Centering text on a bitmap?

Postby thedeftone » Sun Jun 01, 2008 2:53 pm

Hi

is there an easy way to center a text vertically and horizontally on a bitmap?
I tried to calculate the xPosition and the yPosition of the text, but i can't figure out who the center the text vertically.

This is how i centered the text horizontally:
Syntax: [ Download ] [ Hide ]
Using java Syntax Highlighting
  1. // Here we calculate the xPos of the text with the help of getTextWidths()
  2.  
  3. // which gives us the width in px of a certain text
  4.  
  5. int textXPos = (size-paintText.getTextWidths(value, new float[1]) ) / 2;
  6.  
  7.            
  8.  
  9. int textYPos = ????? ;
  10.  
  11.            
  12.  
  13. // draw Text
  14.  
  15. canvas.drawText(value, textXPos, textYPos, paintText);
Parsed in 0.030 seconds, using GeSHi 1.0.8.4

size is the height and also the width of the canvas...

I think it could help a lot to get the height of the text in px but I don't get it...

greetz
thedeftone
Junior Developer
Junior Developer
 
Posts: 14
Joined: Wed Apr 09, 2008 7:51 pm

Top

Postby thedeftone » Sun Jun 01, 2008 3:28 pm

So the width is workin perfect now I think but i keep having troubles with the height.
I just found an older topic where a similar problem was discussed: This topic
Use Paint.ascent() and Paint.descent() to compute the height.

Can someone explain this to me? I'm not sure what ascent and descent means and how to calculate the height with this information.

greets :D
thedeftone
Junior Developer
Junior Developer
 
Posts: 14
Joined: Wed Apr 09, 2008 7:51 pm

Postby thedeftone » Tue Jun 03, 2008 8:40 am

I've been searching desperately for a solution...can anybody help me out? :cry:
Last edited by thedeftone on Thu Jul 03, 2008 8:40 pm, edited 1 time in total.
thedeftone
Junior Developer
Junior Developer
 
Posts: 14
Joined: Wed Apr 09, 2008 7:51 pm

Postby cheneysan » Thu Jul 03, 2008 12:25 pm

You could try FontMetrics top and bottom attributes. :?:
cheneysan
Freshman
Freshman
 
Posts: 2
Joined: Thu Jul 03, 2008 11:11 am

TextAlign example in the SDK

Postby cg0601 » Thu Aug 06, 2009 6:57 am

If you draw on a Canvas, you can use the text attributes of your paint:

Syntax: [ Download ] [ Hide ]
Using java Syntax Highlighting
  1.        textPaint.setTextAlign(Paint.Align.CENTER);
  2.  
  3.  
Parsed in 0.030 seconds, using GeSHi 1.0.8.4


There is a good example in the ApiDemos project in the SDK. Look for the class TextAlign.java in the com.example.android.apis.graphics package
cg0601
Junior Developer
Junior Developer
 
Posts: 16
Joined: Tue Aug 04, 2009 8:53 am

Top

Return to Android 2D/3D Graphics - OpenGL Tutorials

Who is online

Users browsing this forum: No registered users and 3 guests