2

EDIT: The problem came from the emulator, the error did not appear on a real device :(

I'm trying to draw some text in a custom view and must there for measure it but the value of the Paint.getTextBounds() returns a height which is about 30% higher then the actual text which gives everything a quirky look.

I found this: Android Paint: .measureText() vs .getTextBounds() and tried to add the solution code to my own onDraw and saw that i the same measuring error as in my code. Here is a picture of the result: enter image description here

Compare with: enter image description here The image is copied from Android Paint: .measureText() vs .getTextBounds()

Note the spacing above the text in the first picture. Any Ideas what might be causing this? Or are there alternative ways to measure height of a drawn string?

Here is the onDraw method:

@Override 
public void onDraw(Canvas canvas){
//      canvas.drawColor(color_Z1);
//      r.set(0, 0, (int)(width*progress), height);
//      paint.setColor(color_Z2);
////        canvas.drawRect(r, paint);
//      textPaint.getTextBounds(text, 0, text.length(), r);
//      canvas.drawRect(r, paint);
//      canvas.drawText(text, 0, r.height(), textPaint);

    final String s = "Hello. I'm some text!";

     Paint p = new Paint();
     Rect bounds = new Rect();
     p.setTextSize(60);

     p.getTextBounds(s, 0, s.length(), bounds);
     float mt = p.measureText(s);
     int bw = bounds.width();

     Log.i("LCG", String.format(
          "measureText %f, getTextBounds %d (%s)",
          mt,
          bw, bounds.toShortString())
      );
     bounds.offset(0, -bounds.top);
     p.setStyle(Style.STROKE);
     canvas.drawColor(0xff000080);
     p.setColor(0xffff0000);
     canvas.drawRect(bounds, p);
     p.setColor(0xff00ff00);
     canvas.drawText(s, 0, bounds.bottom, p);
}
Community
  • 1
  • 1
SverkerSbrg
  • 503
  • 1
  • 9
  • 23
  • Are you trying to draw the text along the top? Why not just draw at top coordinates, and offset the draw height by text height. Without modifying the bounds. – IAmGroot Oct 17 '13 at 11:52
  • Yes this code should align it along top the problem is that getTextBounds returns the red rectangle whos height is bigger hte the texts, so i do not have the real height of hte text... – SverkerSbrg Oct 17 '13 at 12:25
  • I think it has to do with the font. It's different between your app (Roboto) and the original SO question (Droid Sans, I assume). Try the sample with text without any special characters. E.g., "abcdefghijklmnopqrstuvwxyz". – Delyan Oct 17 '13 at 12:57
  • "abcdefghijklmnopqrstuvwxyz" gives a simlilar difference as "Hello. I'm some text". And the text i really want to print is "30%" so no funky characters there... – SverkerSbrg Oct 17 '13 at 13:02
  • I've noticed this too, no matter the font getTextBounds is reporting a height plus 30% of actual height. Does anyone know why? – Dave Thomas Oct 28 '13 at 22:27
  • Sorry I didn't read all comments below as they were not expanded. The truth is it is eclipse after all! Good to know you when looking at that visual representation that eclipse renders. The text height is 1.3 too big! – Dave Thomas Oct 28 '13 at 23:12

1 Answers1

0

i didnot test your code but i dont see any problems with Paint.getTextBounds():

public class TextBoundsTest extends View {
    private Paint paint;
    private Rect bounds;

    public TextBoundsTest(Context context) {
        super(context);
        paint = new Paint(Paint.ANTI_ALIAS_FLAG);
        paint.setTextSize(32);
        bounds = new Rect();
    }

    @Override
    protected void onDraw(Canvas canvas) {
        String text = "this is my text";
        paint.getTextBounds(text, 0, text.length(), bounds);
        Log.d(TAG, "onDraw " + bounds);

        int x = (getWidth() - bounds.width()) / 2;
        int y = 70;

        paint.setColor(0xff008800);
        bounds.offset(x, y);
        canvas.drawRect(bounds, paint);

        paint.setColor(0xffeeeeee);
        canvas.drawText(text, x, y, paint);
    }
}

add this in Activity.onCreate:

TextBoundsTest view = new TextBoundsTest(this);
setContentView(view);

the result is: enter image description here

pskink
  • 23,874
  • 6
  • 66
  • 77
  • The problem is that the getTextBounds method returns the red rectangle, compare with the rectangle in the solution i linked to, they're different. So i'm missing a correct measurement of the texts height. – SverkerSbrg Oct 17 '13 at 12:27
  • i dont understand you, run my code and see that a green rectangle perfectly matches the given text – pskink Oct 17 '13 at 12:30
  • did you change anything ? the image you posted doesnt look like my result – pskink Oct 17 '13 at 12:45
  • TextBoundsTest: https://www.dropbox.com/s/we1dqct4tiu5wz5/TextBoundsTest.java XML: https://www.dropbox.com/s/9c18vvihb5t5vrt/test3.xml This difference is what puzzels me, when i imported the code from the solution i referensed above it came out differently too... – SverkerSbrg Oct 17 '13 at 13:00
  • Nvm i'm an idiot! The problem is faulty drawing by eclipse... it appears correctly on my real device :( – SverkerSbrg Oct 17 '13 at 13:08
  • so todays lesson is: dont always trust eclipse – pskink Oct 17 '13 at 13:21
  • Naturally. Trust IntelliJ IDEA. ;) – Delyan Oct 17 '13 at 13:40