android - lags using canvas during scrolling -
i'm trying create outline/stroke on text in android. since there no "easy way" create stroke, try realize canvas. tried textview extending class (outlinetextview.java):
import android.content.context; import android.graphics.canvas; import android.graphics.paint; import android.graphics.typeface; import android.text.layout; import android.text.staticlayout; import android.text.textpaint; import android.util.attributeset; import android.widget.textview; public class outlinetextview extends textview { public outlinetextview(context context) { super(context); initpaint(); } public outlinetextview(context context, attributeset attrs) { super(context, attrs); initpaint(); } public outlinetextview(context context, attributeset attrs, int defstyle) { super(context, attrs, defstyle); initpaint(); } private void initpaint() { mtextpaint = new textpaint(); mtextpaint.setantialias(true); mtextpaint.settextsize(gettextsize()); mtextpaint.setcolor(mcolor); mtextpaint.setstyle(paint.style.fill); mtextpaint.settypeface(gettypeface()); mtextpaintoutline = new textpaint(); mtextpaintoutline.setantialias(true); mtextpaintoutline.settextsize(gettextsize()); mtextpaintoutline.setcolor(mbordercolor); mtextpaintoutline.setstyle(paint.style.stroke); mtextpaintoutline.settypeface(gettypeface()); mtextpaintoutline.setstrokewidth(mbordersize); } public void settext(string text) { super.settext(text); mtext = text.tostring(); requestlayout(); invalidate(); } public void settextsize(float size) { super.settextsize(size); requestlayout(); invalidate(); initpaint(); } public void settextcolor(int color) { super.settextcolor(color); mcolor = color; invalidate(); initpaint(); } public void setshadowlayer(float radius, float dx, float dy, int color) { super.setshadowlayer(radius, dx, dy, color); mbordersize = radius; mbordercolor = color; requestlayout(); invalidate(); initpaint(); } public void settypeface(typeface tf, int style) { super.settypeface(tf, style); requestlayout(); invalidate(); initpaint(); } public void settypeface(typeface tf) { super.settypeface(tf); requestlayout(); invalidate(); initpaint(); } @override protected void ondraw(canvas canvas) { layout layout = new staticlayout(gettext(), mtextpaintoutline, getwidth(), layout.alignment.align_center, mspacingmult, mspacingadd, mincludepad); layout.draw(canvas); layout = new staticlayout(gettext(), mtextpaint, getwidth(), layout.alignment.align_center, mspacingmult, mspacingadd, mincludepad); layout.draw(canvas); } @override protected void onmeasure(int widthmeasurespec, int heightmeasurespec) { layout layout = new staticlayout(gettext(), mtextpaintoutline, measurewidth(widthmeasurespec), layout.alignment.align_center, mspacingmult, mspacingadd, mincludepad); int ex = (int) (mbordersize * 2 + 1); setmeasureddimension(measurewidth(widthmeasurespec) + ex, measureheight(heightmeasurespec) * layout.getlinecount() + ex); } private int measurewidth(int measurespec) { int result = 0; int specmode = measurespec.getmode(measurespec); int specsize = measurespec.getsize(measurespec); if (specmode == measurespec.exactly) { result = specsize; } else { result = (int) mtextpaintoutline.measuretext(mtext) + getpaddingleft() + getpaddingright(); if (specmode == measurespec.at_most) { result = math.min(result, specsize); } } return result; } private int measureheight(int measurespec) { int result = 0; int specmode = measurespec.getmode(measurespec); int specsize = measurespec.getsize(measurespec); mascent = (int) mtextpaintoutline.ascent(); if (specmode == measurespec.exactly) { result = specsize; } else { result = (int) (-mascent + mtextpaintoutline.descent()) + getpaddingtop() + getpaddingbottom(); if (specmode == measurespec.at_most) { result = math.min(result, specsize); } } return result; } private textpaint mtextpaint; private textpaint mtextpaintoutline; private string mtext = ""; private int mascent = 0; private float mbordersize; private int mbordercolor; private int mcolor; private float mspacingmult = 1.0f; private float mspacingadd = 0; private boolean mincludepad = true; } i use class in mainactivity following:
outlinetextview t2b = new outlinetextview(this); t2b.setgravity(gravity.center | gravity.center_horizontal); t2b.settextsize(44); t2b.settextcolor(color.parsecolor(color.green)); t2b.setshadowlayer(10, 1, 1, color.black); typeface type2 = typeface.createfromasset(getassets(),"fonts/helveticaltstd-bold.otf"); t2b.settypeface(type2); t2b.settext(percentage + "%"); fl.addview(t2b); fl framelayout. output stroked text , looks want it. view long list of tiles , on of stroked text. text not problem. problem performance while scrolling. if scroll down or up, performance slows down , scolling not smooth . think problem way draw stroked lines. try many variation of stroke text , tried every suggested variation of stroke lines here. everytime have same problem. if draw text without stroke lines (but still using outlinetextview), scrolls smooth again, no lags, no performance slowdowns. drawing of stroke lines seems slow down system.
p.s: outlinetextview.java not own, got internet, since seems draw stroked text.
i thankful, if me.
Comments
Post a Comment