Android textview get line count before draw on canvas -
i want line count before drawing text. here in code i'm getting count(current line number). it's working not correct. in while loop i'm setting 1 word @ 1 step trying know line count comparing canvas width , temp text width.
private void draw(canvas canvas, string text, int size) { canvas.drawpaint(paint); tv = new textview(mcontext); tv.settextcolor(color.black); tv.settextsize(size); int lineheight = tv.getlineheight(); // max lines on screen int lines = canvas.getheight() / lineheight; //max_height = lines * tv.getlineheight(); string screentext = ""; pattern stuff = pattern.compile("[\\w']+|[\\s.,!?;:-]"); matcher matcher = stuff.matcher(text); list<string> matchlist = new arraylist<string>(); while (matcher.find()) { matchlist.add(matcher.group(0)); } // string wholetext[] = text.split(" "); int = 0; string word; paint.settextsize(size*3); while (count <= lines-1&&i<=matchlist.size()-1) { word = matchlist.get(i++); //wholetext[i++]; screentext += word; tempstr += word; float w = paint.measuretext(tempstr, 0, tempstr.length()); if(canvas.getwidth() <= w) { count++; tempstr=word; } if(count>lines){ break; } tv.settext(screentext); } // have enable setdrawingcacheenabled, or getdrawingcache return null tv.setdrawingcacheenabled(true); tv.measure(measurespec.makemeasurespec(canvas.getwidth(), measurespec.exactly), measurespec.makemeasurespec(canvas.getheight(), measurespec.exactly)); tv.setpadding(10,0,10,0); //tv.layout(0, 0, tv.getmeasuredwidth(), lines * tv.getlineheight()); tv.layout(0, 0, tv.getmeasuredwidth(), tv.getmeasuredheight()); canvas.drawbitmap(tv.getdrawingcache(), 0, 0, paint); //canvas.drawtext(tv.gettext().tostring(), 0, 0, paint); // disable drawing cache tv.setdrawingcacheenabled(false); }
Comments
Post a Comment