java - How to correctly draw text in an extended class for TextView? -


i'm working on extending textview, adding outline around text. far, problem i've been having inability position "outline" correctly behind text. if code extended class 1 portrayed below, label looks this:

note: in above screenshot, set fill color white, , stroke color black.

what doing wrong?

public class outlinedtextview extends textview {     /* ===========================================================      * constants      * =========================================================== */     private static final float outline_proportion = 0.1f;      /* ===========================================================      * members      * =========================================================== */     private final paint mstrokepaint = new paint();     private int moutlinecolor = color.transparent;      /* ===========================================================      * constructors      * =========================================================== */     public outlinedtextview(context context) {         super(context);         this.setuppaint();     }     public outlinedtextview(context context, attributeset attrs) {         super(context, attrs);         this.setuppaint();         this.setupattributes(context, attrs);     }     public outlinedtextview(context context, attributeset attrs, int defstyle) {         super(context, attrs, defstyle);         this.setuppaint();         this.setupattributes(context, attrs);     }      /* ===========================================================      * overrides      * =========================================================== */     @override     protected void ondraw(canvas canvas) {         // text print         final float textsize = super.gettextsize();         final string text = super.gettext().tostring();          // setup stroke         mstrokepaint.setcolor(moutlinecolor);         mstrokepaint.setstrokewidth(textsize * outline_proportion);         mstrokepaint.settextsize(textsize);         mstrokepaint.setflags(super.getpaintflags());         mstrokepaint.settypeface(super.gettypeface());          // figure out drawing coordinates         //mstrokepaint.gettextbounds(text, 0, text.length(), mtextbounds);          // draw         canvas.drawtext(text,                 super.getwidth() * 0.5f, super.getbottom() * 0.5f,                 mstrokepaint);         super.ondraw(canvas);     }      /* ===========================================================      * private/protected methods      * =========================================================== */     private final void setuppaint() {         mstrokepaint.setantialias(true);         mstrokepaint.setstyle(paint.style.stroke);         mstrokepaint.settextalign(paint.align.center);     }     private final void setupattributes(context context, attributeset attrs) {         final typedarray array = context.obtainstyledattributes(attrs,                 r.styleable.outlinedtextview);         moutlinecolor = array.getcolor(                 r.styleable.outlinedtextview_outlinecolor, 0x00000000);         array.recycle();           // force text label centered         super.setgravity(gravity.center_horizontal);     } } 

bah, stupid of me. needed change-up commented-out line:

super.getpaint().gettextbounds(text, 0, text.length(), mtextbounds); 

in addition, rendering text, need average view's height , text's height:

// draw canvas.drawtext(text,     super.getwidth() * 0.5f, (super.getheight() + mtextbounds.height()) * 0.5f,     mstrokepaint); 

the entire code reads follows:

public class outlinedtextview extends textview {     /* ===========================================================      * constants      * =========================================================== */     private static final float outline_proportion = 0.1f;      /* ===========================================================      * members      * =========================================================== */     private final paint mstrokepaint = new paint();     private final rect mtextbounds = new rect();     private int moutlinecolor = color.transparent;      /* ===========================================================      * constructors      * =========================================================== */     public outlinedtextview(context context) {         super(context);         this.setuppaint();     }     public outlinedtextview(context context, attributeset attrs) {         super(context, attrs);         this.setuppaint();         this.setupattributes(context, attrs);     }     public outlinedtextview(context context, attributeset attrs, int defstyle) {         super(context, attrs, defstyle);         this.setuppaint();         this.setupattributes(context, attrs);     }      /* ===========================================================      * overrides      * =========================================================== */     @override     protected void ondraw(canvas canvas) {         // text print         final float textsize = super.gettextsize();         final string text = super.gettext().tostring();          // setup stroke         mstrokepaint.setcolor(moutlinecolor);         mstrokepaint.setstrokewidth(textsize * outline_proportion);         mstrokepaint.settextsize(textsize);         mstrokepaint.setflags(super.getpaintflags());         mstrokepaint.settypeface(super.gettypeface());          // figure out drawing coordinates         super.getpaint().gettextbounds(text, 0, text.length(), mtextbounds);          // draw         canvas.drawtext(text,                 super.getwidth() * 0.5f, (super.getheight() + mtextbounds.height()) * 0.5f,                 mstrokepaint);         super.ondraw(canvas);     }      /* ===========================================================      * private/protected methods      * =========================================================== */     private final void setuppaint() {         mstrokepaint.setantialias(true);         mstrokepaint.setstyle(paint.style.stroke);         mstrokepaint.settextalign(paint.align.center);     }     private final void setupattributes(context context, attributeset attrs) {         final typedarray array = context.obtainstyledattributes(attrs,                 r.styleable.outlinedtextview);         moutlinecolor = array.getcolor(                 r.styleable.outlinedtextview_outlinecolor, 0x00000000);         array.recycle();           // force text label centered         super.setgravity(gravity.center_horizontal);     } } 

Comments

Popular posts from this blog

Add email recipient to all new Trac tickets -

400 Bad Request on Apache/PHP AddHandler wrapper -

php - Change action and image src url's with jQuery -