Flashing Text and text progress bar Control

Source: Internet
Author: User
Tags drawtext

Flashing Text and text progress bar Control

I have seen that a progress bar is filled with text and curious. I want to try it myself, but it is still rough. There is no surging effect such as water flow at the end of the progress bar.


Both controls inherit TextView, so that you can easily set the font size and other attributes.


1. Text control of GradientTextView gradient color

Mainly used in the LinearGradient class for gradient color,

New LinearGradient (0, 0, width, height, colorArrays [colorIndex], gradientSpread, Shader. TileMode. MIRROR );

The parameters correspond to the following,

Parameter 1, parameter 2: the starting position of the gradient.

Parameter 3, parameter 4: gradient end point.

Parameter 5: gradient color array

Parameter 6: gradient distribution array (must be the same as the array length in parameter 5)

Parameter 7: coloring er Mode


In this example, the color gradient array colorArrays is regularly replaced 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, defStyleAttr);    }    @Override    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {        super.onMeasure(widthMeasureSpec, 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);        canvas.drawText(getText().toString(), 0, height, paint);        handler.sendEmptyMessageDelayed(COLORINVALIDATE,500);    }}



2, GradientTextViewProgress
In onDraw () drawText, note that in drawText, canvas. drawText (getText (). toString (), 0, baseline, paint );
Parameter 3 is the baseline height.
First, draw the background text with the background paint brush, then draw the progress with another paint brush, and call canvas. clipRect to crop. Refresh the control every time you set the 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, defStyleAttr);        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(widthMeasureSpec, 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();    }}




 

Related Article

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.