Flashing text and text progress bar controls

Source: Internet
Author: User
Tags drawtext

Before I saw a progress bar is filled with text, curiosity, I would like to write a try, but still relatively rough, progress bar at the end of no flow and other surging effect.


Two controls are inherited TextView, which makes it easy to set the font size and other related properties.


1,gradienttextview text control with gradient color

Mainly used in the LinearGradient class, for the gradient color,

New LinearGradient (0, 0, width, height, colorarrays[colorindex], Gradientspread, Shader.TileMode.MIRROR);

The parameters correspond to the following,

Parameter 1, Parameter 2: Gradient color start position.

Parameter 3, Parameter 4: Gradient color end position.

Parameter 5: Array of gradient colors for the gradient color

Parameter 6: Array of distributions for the gradient color (required with the same length as the array in parameter 5)

Parameter 7: Shader mode


In the example, you can replace the gradient array colorarrays to achieve the text color flashing effect.


Import Android.content.context;import Android.graphics.canvas;import Android.graphics.color;import Android.graphics.lineargradient;import Android.graphics.paint;import Android.graphics.shader;import Android.os.handler;import android.os.message;import Android.util.attributeset;import android.widget.TextView;/** * @author Jayce * @date 2015/1/23 */public class Gradienttextview extends TextView {private int[] colorArray1 = new int[    ]{color.red, Color.green, color.blue};    Private int[] ColorArray2 = new Int[]{color.green, Color.Blue, color.red};    Private int[] ColorArray3 = new Int[]{color.blue, color.red, color.green};    Private int[][] Colorarrays = new Int[][]{colorarray1, ColorArray2, colorArray3};    private int colorindex = 0;    Private float[] Gradientspread = new float[]{0, 0.5f, 1.0f};    private static final int colorinvalidate=0;    private int width;    private int height; Private Handler Handler = new Handler () {@Override public void Handlemessage (MeSsage msg) {super.handlemessage (msg);            ColorIndex = (colorindex + 1)% 3;        Invalidate ();    }    };    Public Gradienttextview (Context context) {This (context, NULL);    } public Gradienttextview (context context, AttributeSet Attrs) {This (context, attrs, 0); } public Gradienttextview (context context, AttributeSet attrs, int defstyleattr) {Super (context, Attrs, defstyl    EATTR); } @Override protected void onmeasure (int widthmeasurespec, int heightmeasurespec) {super.onmeasure (widthmeas        Urespec, Heightmeasurespec);        width = Getmeasuredwidth ();    Height = getmeasuredheight ();        } @Override protected void OnDraw (canvas canvas) {//super.ondraw (canvas);        Paint paint = getpaint ();        Paint.setantialias (TRUE); LinearGradient lg = new LinearGradient (0, 0, width, height, colorarrays[colorindex], Gradientspread,        Shader.TileMode.MIRROR);        Paint.setshader (LG); AaVas.drawtext (GetText (). toString (), 0, height, paint);    Handler.sendemptymessagedelayed (colorinvalidate,500); }}



2,gradienttextviewprogress
DrawText in OnDraw (), note drawText in Canvas.drawtext (GetText (). toString (), 0, Baseline, paint);
Parameter 3 is the height of the baseline.
First draw the background text with the background brush, then draw the progress with another brush, call Canvas.cliprect to crop. Refreshes the control after each set of progress.

Package Huwei.com.gradienttextviewdemo;import Android.content.context;import Android.graphics.canvas;import Android.graphics.color;import Android.graphics.lineargradient;import Android.graphics.paint;import Android.graphics.rect;import Android.graphics.shader;import Android.util.attributeset;import Android.view.View; Import android.widget.textview;/** * @author Jayce * @date 2015/1/23 */public class Gradienttextprogress extends TextView    {Private String MText;    private int maxValue;    private int curvalue;    Private Paint bgpaint,paint;//<»±êprivate int mwidth, mheight, Baseline;    private int bgcolorint=color.black;         private int procolorint=color.blue;  Private lineargradient LG;    ½¥±äé«private Boolean hasgradient;    Private int[] color;    Private float[] position;    private Shader.tilemode mode;    Public gradienttextprogress (Context context) {This (context, NULL);      } public gradienttextprogress (context context, AttributeSet Attrs) {  This (context, attrs, 0); } public gradienttextprogress (context context, AttributeSet attrs, int defstyleattr) {Super (context, ATTRS, Def        STYLEATTR);        Initpaint ();    MText = GetText (). toString ();        } private void Initpaint () {bgpaint = Getpaint ();        Bgpaint.setantialias (TRUE);                Bgpaint.settextsize (GetTextSize ());        Paint=new Paint ();    Paint.set (Bgpaint);    } public int Getmaxvalue () {return maxValue;    } public void Setmaxvalue (int maxValue) {this.maxvalue = MaxValue;    } public String Getmtext () {return mText;        } public void Setmtext (String mText) {this.mtext = MText;    Invalidate ();    } public int getprogress () {return curvalue;        } public void setlineargradient (int color[],float position[],shader.tilemode mode) {hasgradient=true;        This.color=color;        This.position=position;    This.mode=mode; } public void Setprogress (iNT Curvalue) {this.curvalue = Curvalue;    Invalidate ();    } public int Getbgcolorint () {return bgcolorint;    }//éèöã½ø¶è»±êñõé«£¬éèöã½¥±ä髺󣬸ãïîê§ð§public int Getprocolorint () {return procolorint; } @Override protected void onmeasure (int widthmeasurespec, int heightmeasurespec) {super.onmeasure (widthmeas        Urespec, Heightmeasurespec);        Mwidth = Getmeasuredwidth ();        Mheight = Getmeasuredheight ();        Paint.fontmetricsint FontMetrics = Paint.getfontmetricsint ();             Baseline = (Mheight-fontmetrics.bottom + fontmetrics.top)/2-fontmetrics.top; } @Override protected void OnDraw (canvas canvas) {if (null = = MText | |        ". Equals (MText)) return;        Bgpaint.setcolor (Bgcolorint); Canvas.drawtext (mText, 0, Baseline, bgpaint);        »±³¾°float section = (float) curvalue/maxvalue;        Rect prorect = new Rect (0, 0, (int) (section * mwidth), mheight); if (!hasgradienT) {Paint.setcolor (procolorint);            }else{lg=new lineargradient (0,0, (int) (section * mwidth), mheight,color,position, mode);        Paint.setshader (LG);        } canvas.save ();        Canvas.cliprect (Prorect);        Canvas.drawtext (mText, 0, Baseline, paint);    Canvas.restore (); }}




Flashing text and text progress bar controls

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.