android - What is the best way to colour a part of the letter? -


i want achieve this:

enter image description here

the first thing comes mind draw text on canvas 2 times , cover first text shape. maybe there better solution.

one approach using porterduffxfermode compositing blue rectangle on text. extend textview , override ondraw() draw rectangle after text has been drawn, , proper mode (i believe xor 1 want) should achieve desired effect. this:

public class progresstextview extends textview {      private static final float max_progress = ...;      private paint mpaint;      public progresstextview(context context) {         super(context);         initpaint();     }      /* other constructor omitted, same pattern in */      private void initpaint() {         mpaint = new paint();         mpaint.setcolor(...);         mpaint.setxfermode(new porterduffxfermode(mode.xor));         // note: may need following line if hardware accel available         setlayertype(layer_type_software, null);     }      @override     public void ondraw(canvas canvas) {         super.ondraw(canvas);         drawprogress(canvas);     }      private void drawprogress(canvas canvas) {         int w = getwidth() - getpaddingleft() - getpaddingright();         int h = getheight() - getpaddingtop() - getpaddingbottom();         float progress = getprogress();         float rectw = w * (progress / max_progress);          int savecount = canvas.save();         canvas.translate(getpaddingleft(), getpaddingtop());         canvas.drawrect(0, 0, rectw, h, mpaint);         canvas.restoretocount(savecount);     }      private float getprogress() {         // todo     } } 

more info on porter/duff compositing: http://ssp.impulsetrain.com/porterduff.html


Comments