How to use "TextLayoutView" of Instagram in improving TextView Rendering on Android -
to improve textview rendering in instagram, engineers in instagram provide hack in here,they use custom view(textlayoutview) cache text.layout, in post, don't give demo or tell how use it, if want use hack, how do?
this simple implementation:
textlayoutview:
public class textlayoutview extends view { private layout mlayout; public textlayoutview(context context) { this(context, null); } public textlayoutview(context context, attributeset attrs) { this(context, attrs, 0); } public textlayoutview(context context, attributeset attrs, int defstyleattr) { super(context, attrs, defstyleattr); setfocusable(true); } @override protected void ondraw(canvas canvas) { long t1=system.currenttimemillis(); super.ondraw(canvas); canvas.save(); if (mlayout != null) { canvas.translate(getpaddingleft(), getpaddingtop()); mlayout.draw(canvas); } canvas.restore(); long t2=system.currenttimemillis(); log.i("test", "ondraw::"+(t2-t1)); } @override protected void onmeasure(int widthmeasurespec, int heightmeasurespec) { long t1=system.currenttimemillis(); super.onmeasure(widthmeasurespec, heightmeasurespec); if (mlayout != null) { setmeasureddimension( getpaddingleft() + getpaddingright() + mlayout.getwidth(), getpaddingtop() + getpaddingbottom() + mlayout.getheight()); } long t2=system.currenttimemillis(); log.i("test", "onmeasure::"+(t2-t1)); } public void settextlayout(layout layout) { mlayout = layout; requestlayout(); }} you can use this:
@override public view getview(int position, view convertview, viewgroup parent) { ...... viewholder.textview.settextlayout(getlayout(mlist.get(position))); return convertview; } private final map<string, layout> mlayoutmap = new concurrenthashmap<string, layout>(); private layout getlayout(string str) { layout layout = mlayoutmap.get(str); if (layout == null) { textpaint textpaint = new textpaint(); textpaint.settextsize(20); layout = new staticlayout(str, textpaint, width, alignment.align_center, 1.0f, 0.0f, true); mlayoutmap.put(str, layout); } return layout; }
Comments
Post a Comment