Android : Paint.getTextBounds returns width much greater than the width of the screen -


i have a textview width fill_parent under root layout means has width of screen 480.
trying measure bounds of text in text view. did :

rect rect = new rect(); textview.getpaint().gettextbounds(text,0,text.length()-1,rect); log.v(tag,"width : height ="+rect.width()+" : "+width.height()); 

but prints 10278 : 79. why printing width 10278 when scren width 480? if assuming single line , calculating point of having gettextbounds.

the documentation states int end "1 past last char in string measure." http://developer.android.com/reference/android/graphics/paint.html#gettextbounds(java.lang.string, int, int, android.graphics.rect)

you should have:

textview.getpaint().gettextbounds(text, 0, text.length(), rect); 

this applies canvas.drawtext().

the textview displaying text "wrapping" text. method gettextbounds not aware of textview @ all.

in fact, if build own custom view displaying text, gettextbounds useful achieving own text wrapping. following code terrible job of presentation, iterates on string, drawing sections of canvas, wrapping text.

    private void drawtext(canvas canvas) {          string text = "nunc eleifend erat eu nulla varius iaculis. quisque feugiat justo sit amet" +                 " neque interdum tempor. curabitur faucibus facilisis tristique. donec posuere" +                 " viverra magna, eu accumsan sapien congue id. cras fringilla justo ut lacus" +                 " molestie, et venenatis orci egestas. vivamus pretium, nisl quis cursus cursus," +                 " dolor eros porta neque, sit amet viverra ante orci non elit. donec odio neque," +                 " volutpat sit amet dui sit amet, fringilla tempus sapien. donec sed mollis justo." +                 " in dignissim tincidunt neque, eu luctus justo egestas nec. donec commodo ut arcu" +                 " vel placerat. donec ullamcorper justo eget justo commodo hendrerit. quisque" +                 " commodo imperdiet posuere. aenean vehicula dui.";          paint paint = new paint();         paint.settextsize(30);         paint.setantialias(true);          int padding = 20;          int availwidth = getwidth() - 2 * padding;         int availheight = getheight() - 2 * padding;          rect bounds = new rect();         int currenttextbottom = 0;         int firstcharinline = 0;         int lastcharinline = 0;          outerloop: while (currenttextbottom < availheight) {              while (character.iswhitespace(text.charat(firstcharinline))) {                 lastcharinline = ++firstcharinline;             }              (int = firstcharinline + 1; < text.length(); i++) {                 paint.gettextbounds(text, firstcharinline, i, bounds);                  if (bounds.width() <= availwidth) {                     if (character.iswhitespace(text.charat(i))) {                         lastcharinline = i;                     }                 } else {                     currenttextbottom += bounds.height();                     if (firstcharinline == lastcharinline) {                         lastcharinline = i;                     }                     canvas.drawtext(text, firstcharinline, lastcharinline, padding, currenttextbottom + padding, paint);                     firstcharinline = lastcharinline++;                     break;                 }                 if (i == text.length() - 1) {                     currenttextbottom += bounds.height();                     lastcharinline = text.length();                     canvas.drawtext(text, firstcharinline, lastcharinline, padding, currenttextbottom + padding, paint);                     break outerloop;                 }             }         }     } } 

edit - side note, i've noticed difference between bounds.width() , paint.measuretext()! totally unrelated above code, having issues paint.align.center glitching , not working expected. removed text alignment , tried:

canvas.drawtext(         text,         viewwidth / 2f - bounds.width() / 2f,         viewheight / 2f + bounds.height() / 2f,         paint); 

but doesn't align horizontally properly. when did instead:

float textwidth = paint.measuretext(text); canvas.drawtext(         text,         viewwidth / 2f - textwidth / 2,         viewheight / 2f + bounds.height() / 2f,         paint); 

it aligned satisfaction :)


Comments

Popular posts from this blog

php - Admin SDK -- get information about the group -

dns - How To Use Custom Nameserver On Free Cloudflare? -

Python Error - TypeError: input expected at most 1 arguments, got 3 -