2

I'm writing an application that has a custom text layout engine, and for this to work I must measure lots (thousands) of Strings using the .getTextBounds() method on the Paint class.

It does work. But on some devices, this measurement is extremely slow.

For example, measuring 10,000 5-6 chars long words

  • Samsung Galaxy S2 phone (or any other kind of device from the Samsung Galaxy series): 1.5 seconds
  • HTC Desire HD: ~90 seconds.
  • Asus Transformer TF300: ~30 seconds
  • Etc.

I've done method profiling and all I can see that the native Skia implementation of the measurement method takes much longer on some devices.

I'm doing nothing fancy during measurement, my code looks something like this:

Paint paint = new Paint();
Rect bounds = new Rect();
paint.setTextSize(22.0f);

for (int i = 0; i < input.length; i++) {
    paint.getTextBounds(input[i], 0, input[i].length(), bounds);
}

Is there something to look out for when measuring text dimensions? Did the manufacturers screw up something related to the font cache implementation in the ROM? Is there something I can do about this?

Zsombor Erdődy-Nagy
  • 16,864
  • 16
  • 76
  • 101
  • Why are you measuring them for? – Sipka Nov 28 '13 at 15:57
  • For our ebook rendering engine. – Zsombor Erdődy-Nagy Nov 28 '13 at 16:09
  • If you are displaying for example a sentence, and you want to know when it exceeds the width of the screen then you can use `Paing.breakText()`. It will return the characters you can display in a line. If you are appending words together, then you might want to use a StringBuilder, appending the words, and measuring the complete sentence. If you are displaying lines, I dont think you need the height of the text, (use `Paint.measureText()`) simply display every line `textSize` below the other. If you are not using a fix width typeface, then I guess this is the fastest way – Sipka Nov 28 '13 at 16:17

0 Answers0