Medir el ancho del texto a dibujar sobre Lienzo (Android)


¿Existe un método que devuelva el ancho ( en píxeles ) de un texto que se dibujará en un lienzo de Android utilizando el método DrawText() de acuerdo con la pintura utilizada para dibujarlo?

Author: Benjamin, 2010-07-15

6 answers

 194
Author: Marc Bernstein,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2010-07-15 16:02:40
Paint paint = new Paint();
Rect bounds = new Rect();

int text_height = 0;
int text_width = 0;

paint.setTypeface(Typeface.DEFAULT);// your preference here
paint.setTextSize(25);// have this the same as your text size

String text = "Some random text";

paint.getTextBounds(text, 0, text.length(), bounds);

text_height =  bounds.height();
text_width =  bounds.width();
 23
Author: hamid reza zavareh,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2014-11-17 15:13:20

Respuesta suplementaria

Existe una ligera diferencia entre el ancho devuelto por Paint.measureText y Paint.getTextBounds. measureText devuelve un ancho que incluye el valor advanceX del glifo rellenando el principio y el final de la cadena. El ancho Rect devuelto por getTextBounds no tiene este relleno porque los límites son el Rect que envuelve firmemente el texto.

fuente

 7
Author: Suragch,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2017-05-23 11:54:46

Bien lo he hecho de manera diferente:

String finalVal ="Hiren Patel";

Paint paint = new Paint();
paint.setTextSize(40);
Typeface typeface = Typeface.createFromAsset(getAssets(), "Helvetica.ttf");
paint.setTypeface(typeface);
paint.setColor(Color.BLACK);
paint.setStyle(Paint.Style.FILL);

Rect result = new Rect();
paint.getTextBounds(finalVal, 0, finalVal.length(), result);

Log.i("Text dimensions", "Width: "+result.width()+"-Height: "+result.height());

Espero que esto te ayude.

 1
Author: Hiren Patel,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2016-12-09 11:03:30

Usé los métodos measureText () y getTextPath ()+computeBounds() y construí un Excel con todos los atributos de texto para fuente de tamaño fijo que se pueden encontrar en https://github.com/ArminJo/android-blue-display/blob/master/TextWidth.xlsx . Allí encontrará también fórmulas simples para otros atributos de texto como ascend, etc.

La aplicación así como la función drawFontTest() para generar los valores sin procesar utilizados en excel también están disponibles en este repo.

 0
Author: Armin J.,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2015-09-20 09:23:56

Puede usar "textPaint.getTextSize ()" para obtener el ancho del texto

 -1
Author: Ashish John,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2016-03-16 14:35:41