Android: In version 4.4, Paint.measureText () does not support backward compatibility. Any workarounds?

It is very important for my application to be able to accurately measure strings; I used Paint.measureText() for this. Unfortunately, in 4.4 this method was changed to no longer return an exact value, instead it returns a rounded value. Does anyone know of another method that I can use to accurately measure text or any other suggestions?

Android source:

 Android 17 return w*mInvCompatScaling; Android 18 return (float) Math.ceil(w*mInvCompatScaling); 
+6
source share
2 answers

Returning to thinking seems to be the only way to return old functions. Look at your own custom method as follows and call it.

 Paint paint = new Paint(); // configure paint here Method m = paint.getClass().getDeclaredMethod("native_measureText", String.class, int.class); float size = m.invoke(paint, text, 0x2); // text - text to measure // 0x2 - default for Right-To-Left text 

Although this does not look very clean, this should work for you.

0
source

You must install:

 m.setAccessible(true); 

before referring to it so as not to get an IllegalAccessException .

0
source

All Articles