android闪动的文字和文字进度条控件(带源码)

以前看到过有进度条是填充文字的,好奇心作祟,便想自己写一个试试,但是还是比较粗糙,进度条末端没有水流等涌动的效果。


两个控件都是继承TextView,便于设置字体大小等相关属性。


1,GradientTextView  渐变颜色的文字控件

主要用到LinearGradient类,用于渐变颜色,

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

参数对应如下,

参数1,参数2:渐变色起点位置。

参数3,参数4:渐变色终点位置。

参数5:渐变色的渐变颜色数组

参数6:渐变色的分布数组(需要和参数5中数组长度相同)

参数7:着色器模式


例子中定时替换渐变色数组colorArrays来达到文字颜色闪动的效果。


[java]  view plain  copy
  在CODE上查看代码片 派生到我的代码片
  1. import android.content.Context;  
  2. import android.graphics.Canvas;  
  3. import android.graphics.Color;  
  4. import android.graphics.LinearGradient;  
  5. import android.graphics.Paint;  
  6. import android.graphics.Shader;  
  7. import android.os.Handler;  
  8. import android.os.Message;  
  9. import android.util.AttributeSet;  
  10. import android.widget.TextView;  
  11.   
  12. /** 
  13.  * @author jayce 
  14.  * @date 2015/1/23 
  15.  */  
  16. public class GradientTextView extends TextView {  
  17.     private int[] colorArray1 = new int[]{Color.RED, Color.GREEN, Color.BLUE};  
  18.     private int[] colorArray2 = new int[]{Color.GREEN, Color.BLUE, Color.RED};  
  19.     private int[] colorArray3 = new int[]{Color.BLUE, Color.RED, Color.GREEN};  
  20.   
  21.     private int[][] colorArrays = new int[][]{colorArray1, colorArray2, colorArray3};  
  22.     private int colorIndex = 0;  
  23.     private float[] gradientSpread = new float[]{00.5f, 1.0f};  
  24.   
  25.     private static final int COLORINVALIDATE=0;  
  26.   
  27.     private int width;  
  28.     private int height;  
  29.   
  30.     private Handler handler = new Handler() {  
  31.         @Override  
  32.         public void handleMessage(Message msg) {  
  33.             super.handleMessage(msg);  
  34.   
  35.             colorIndex = (colorIndex + 1) % 3;  
  36.             invalidate();  
  37.         }  
  38.     };  
  39.   
  40.     public GradientTextView(Context context) {  
  41.         this(context, null);  
  42.     }  
  43.   
  44.     public GradientTextView(Context context, AttributeSet attrs) {  
  45.         this(context, attrs, 0);  
  46.     }  
  47.   
  48.     public GradientTextView(Context context, AttributeSet attrs, int defStyleAttr) {  
  49.         super(context, attrs, defStyleAttr);  
  50.     }  
  51.   
  52.     @Override  
  53.     protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {  
  54.         super.onMeasure(widthMeasureSpec, heightMeasureSpec);  
  55.         width = getMeasuredWidth();  
  56.         height = getMeasuredHeight();  
  57.     }  
  58.   
  59.   
  60.   
  61.     @Override  
  62.     protected void onDraw(Canvas canvas) {  
  63.         //super.onDraw(canvas);  
  64.         Paint paint = getPaint();  
  65.         paint.setAntiAlias(true);  
  66.         LinearGradient lg = new LinearGradient(00, width, height, colorArrays[colorIndex], gradientSpread, Shader.TileMode.MIRROR);  
  67.         paint.setShader(lg);  
  68.   
  69.         canvas.drawText(getText().toString(), 0, height, paint);  
  70.         handler.sendEmptyMessageDelayed(COLORINVALIDATE,500);  
  71.     }  
  72. }  



2,GradientTextViewProgress
在onDraw()中drawText,注意drawText中 canvas.drawText(getText().toString(), 0, baseline, paint);
参数3是baseline的高度。
先用背景画笔画出背景文字,然后用另外一个画笔画出进度,调用canvas.clipRect来裁剪。每次设置进度后,刷新控件。

[java]  view plain  copy
  在CODE上查看代码片 派生到我的代码片
  1. package huwei.com.gradienttextviewdemo;  
  2.   
  3. import android.content.Context;  
  4. import android.graphics.Canvas;  
  5. import android.graphics.Color;  
  6. import android.graphics.LinearGradient;  
  7. import android.graphics.Paint;  
  8. import android.graphics.Rect;  
  9. import android.graphics.Shader;  
  10. import android.util.AttributeSet;  
  11. import android.view.View;  
  12. import android.widget.TextView;  
  13.   
  14. /** 
  15.  * @author jayce 
  16.  * @date 2015/1/23 
  17.  */  
  18. public class GradientTextProgress extends TextView {  
  19.     private String mText;  
  20.     private int maxValue;  
  21.     private int curValue;  
  22.     private Paint bgPaint,paint;//<»­±Ê  
  23.     private int mWidth, mHeight, baseline;  
  24.     private int bgColorInt=Color.BLACK;  
  25.     private int proColorInt=Color.BLUE;   
  26.       
  27.     private LinearGradient lg;  //½¥±äÉ«  
  28.     private boolean hasGradient;  
  29.     private int[] color;  
  30.     private float[] position;  
  31.     private Shader.TileMode mode;  
  32.   
  33.     public GradientTextProgress(Context context) {  
  34.         this(context, null);  
  35.     }  
  36.   
  37.     public GradientTextProgress(Context context, AttributeSet attrs) {  
  38.         this(context, attrs, 0);  
  39.     }  
  40.   
  41.     public GradientTextProgress(Context context, AttributeSet attrs, int defStyleAttr) {  
  42.         super(context, attrs, defStyleAttr);  
  43.   
  44.         initPaint();  
  45.   
  46.         mText = getText().toString();  
  47.     }  
  48.   
  49.     private void initPaint() {  
  50.         bgPaint = getPaint();  
  51.         bgPaint.setAntiAlias(true);  
  52.         bgPaint.setTextSize(getTextSize());  
  53.           
  54.         paint=new Paint();  
  55.         paint.set(bgPaint);  
  56.     }  
  57.   
  58.     public int getMaxValue() {  
  59.         return maxValue;  
  60.     }  
  61.   
  62.     public void setMaxValue(int maxValue) {  
  63.         this.maxValue = maxValue;  
  64.     }  
  65.   
  66.     public String getmText() {  
  67.         return mText;  
  68.     }  
  69.   
  70.     public void setmText(String mText) {  
  71.         this.mText = mText;  
  72.         invalidate();  
  73.     }  
  74.   
  75.     public int getProgress() {  
  76.         return curValue;  
  77.     }  
  78.   
  79.     public void setLinearGradient(int color[],float position[],Shader.TileMode mode) {  
  80.         hasGradient=true;  
  81.         this.color=color;  
  82.         this.position=position;  
  83.         this.mode=mode;  
  84.     }  
  85.   
  86.     public void setProgress(int curValue) {  
  87.         this.curValue = curValue;  
  88.         invalidate();  
  89.     }  
  90.   
  91.     public int getBgColorInt() {  
  92.         return bgColorInt;  
  93.     }  
  94.   
  95.     //ÉèÖýø¶È»­±ÊÑÕÉ«£¬ÉèÖý¥±äÉ«ºó£¬¸ÃÏîʧЧ  
  96.     public int getProColorInt( ) {  
  97.         return proColorInt;  
  98.     }  
  99.   
  100.     @Override  
  101.     protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {  
  102.         super.onMeasure(widthMeasureSpec, heightMeasureSpec);  
  103.         mWidth = getMeasuredWidth();  
  104.         mHeight = getMeasuredHeight();  
  105.   
  106.         Paint.FontMetricsInt fontMetrics = paint.getFontMetricsInt();  
  107.         baseline =  (mHeight - fontMetrics.bottom + fontMetrics.top) / 2 - fontMetrics.top;  
  108.           
  109.    
  110.     }  
  111.   
  112.     @Override  
  113.     protected void onDraw(Canvas canvas) {  
  114.         if (null == mText || "".equals(mText)) return;  
  115.   
  116.   
  117.         bgPaint.setColor(bgColorInt);  
  118.         canvas.drawText(mText, 0, baseline, bgPaint); //»­±³¾°  
  119.   
  120.   
  121.         float section = (float) curValue / maxValue;  
  122.         Rect proRect = new Rect(00, (int) (section * mWidth), mHeight);  
  123.         if(!hasGradient){  
  124.             paint.setColor(proColorInt);  
  125.         }else{  
  126.             lg=new LinearGradient(0,0,(int) (section * mWidth),mHeight,color,position, mode);  
  127.             paint.setShader(lg);  
  128.         }  
  129.           
  130.         canvas.save();  
  131.         canvas.clipRect(proRect);  
  132.         canvas.drawText(mText, 0, baseline, paint);  
  133.   
  134.         canvas.restore();  
  135.     }  
  136. }  
   


      下载地址:http://download.csdn.net/detail/huweigoodboy/8393485



  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值