package com.xiong.demo1;import android.content.Context;import android.graphics.Canvas;import android.graphics.Color;import android.graphics.LinearGradient;import android.graphics.Matrix;import android.graphics.Paint;import android.graphics.Shader;import android.util.AttributeSet;import android.widget.TextView;/** * Created by xionglh on 2015/12/29. */public class CustTextView extends TextView {    private int mViewWith;    private int mTranleate;    private Paint mPaint;    private LinearGradient mLinearGradient;    private Matrix mMatrix;    public CustTextView(Context context, AttributeSet attrs) {        super(context, attrs);    }    @Override    protected void onSizeChanged(int w, int h, int oldw, int oldh) {        super.onSizeChanged(w, h, oldw, oldh);        if (mViewWith == 0) {            mViewWith = getMeasuredWidth();            if (mViewWith > 0) {                mPaint = getPaint();                /**                 * LinearGradient第一个和第二个参数表示颜色渐变的起点 第三、第四表示颜色渐变的终点 第五个参数表示渐变的颜色                 * 第六个参数可以为空,表示坐标,值为0-1 new float[] {0.25f, 0.5f, 0.75f, 1 }                 *如果这是空的,颜色均匀分布,沿梯度线                 *  第七个表示平铺方式                 * CLAMP重复最后一个颜色至最后                 * MIRROR重复着色的图像水平或垂直方向已镜像方式填充会有翻转效果                 *REPEAT重复着色的图像水平或垂直方向                 */                mLinearGradient = new LinearGradient(0, 0, mViewWith, 0,                        new int[]{Color.parseColor("#7B68EE"), Color.parseColor("#FFC0CB"), Color.parseColor("#7B68EE")},                        null, Shader.TileMode.MIRROR);                mPaint.setShader(mLinearGradient);                mMatrix = new Matrix();            }        }    }    @Override    protected void onDraw(Canvas canvas) {        super.onDraw(canvas);        if (mMatrix != null) {            mTranleate += mViewWith / 14;            if (mTranleate > 2 * mViewWith) {                mTranleate = -mViewWith;            }            mMatrix.setTranslate(mTranleate, 0);            mLinearGradient.setLocalMatrix(mMatrix);            postInvalidateDelayed(100);        }    }}

更多相关文章

  1. Android详情页结合ScrollView完成Title颜色渐变效果
  2. Android 搜索到的关键字改变颜色
  3. Android多媒体开发 Pro Android Media 第一章 Android图像编程入
  4. android 蓝牙ble app开发(二) -- 关键概念,连接参数,连接请求
  5. Android 自定义阴影Shadow颜色,大小等样式
  6. android xml中 颜色透明度(不透明度)参照表
  7. 改变Android按钮背景颜色的高效方法
  8. Android activity 参数传递

随机推荐

  1. Google Protocol Buffer
  2. Android仿IOS提示框
  3. Android(安卓)Kotlin项目集成阿里ARouter
  4. Android(安卓)OOM Adjustments
  5. Android(安卓)自动化测试框架-百度cafe
  6. Android自学——ListView
  7. android之MediaPlay播放视频
  8. Android(安卓)解屏幕锁与点亮屏幕(来电时
  9. android binder机制之三 Server Porxy
  10. 在Linux下编译Windows版本的adb和fastboo