zoukankan      html  css  js  c++  java
  • Android-利用LinearGradient实现文字一闪一闪

    效果如下图所示:
    这里写图片描述

    具体实现方式如下:

    1,自定义一个LinearGradientTextView 继承 TextView ,重写onSizeChanged和onDraw。:

    public class LinearGradientTextView extends TextView {
        private int mViewWidth;
        private Paint mPaint;
        private LinearGradient mLinearGradient;
        private Matrix mMatrix;
        private int mTranslate;
    
        public LinearGradientTextView(Context context) {
            super(context);
        }
    
        public LinearGradientTextView(Context context, AttributeSet attrs) {
            super(context, attrs);
        }
    
        public LinearGradientTextView(Context context, AttributeSet attrs,
                int defStyleAttr) {
            super(context, attrs, defStyleAttr);
        }
    
        /**
         * LinearGradient有两个构造函数: public LinearGradient(floatx0,float y0, float x1, float y1, int[] colors, float[] positions,Shader.TileMode tile) 
         * 参数:float x0: 渐变起始点x坐标 
         * float y0:渐变起始点y坐标
         *  float x1:渐变结束点x坐标 
         *  float y1:渐变结束点y坐标
         * int[] colors:颜色 的int 数组 
         * float[] positions: 相对位置的颜色数组,可为null,若为null,可为null,颜色沿渐变线均匀分布 
         * Shader.TileMode tile: 渲染器平铺模式
         */
    
        @Override
        protected void onSizeChanged(int w, int h, int oldw, int oldh) {
            // TODO Auto-generated method stub
            super.onSizeChanged(w, h, oldw, oldh);
            if (mViewWidth == 0) {
                mViewWidth = getMeasuredWidth();
            }
            if (mViewWidth > 0) {
                mPaint = getPaint();
                mLinearGradient = new LinearGradient(
                        0,
                        0,
                        mViewWidth,
                        0,
                        new int[] { Color.BLUE, Color.BLACK, Color.RED, Color.YELLOW },
                        null, Shader.TileMode.MIRROR);
                mPaint.setShader(mLinearGradient);
                mMatrix = new Matrix();
            }
    
        }
    
        @Override
        protected void onDraw(Canvas canvas) {
            // TODO Auto-generated method stub
            super.onDraw(canvas);
            if (mMatrix != null) {
                mTranslate += mViewWidth / 5;
                if (mTranslate > 2 * mViewWidth) {
                    mTranslate = -mViewWidth;
                }
                mMatrix.setTranslate(mTranslate, 0);
                mLinearGradient.setLocalMatrix(mMatrix);
                postInvalidateDelayed(100);
            }
        }
    
    }
    

    注:这段代码主要是分两步:一个是在onSizeChanged()即大小发生改变的时候,另外一个是onDraw()主要是用来做动画的效果的。

    2,XML文件 引用到这个自定义的LinearGradientTextView :

     <com.summer.valueanimatordemo.LinearGradientTextView
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:layout_below="@+id/tv"
            android:layout_gravity="center"
            android:gravity="center"
            android:text="测试文字一闪一闪"
            android:textSize="30sp" />
  • 相关阅读:
    MyEclipse配置DataBase Explorer
    Eclipse 如何设置注释的模板
    游戏开发技术
    static_cast 与reinterpret_cast
    一个人的成功取决于晚上的8点至10点经典语录必读
    发送消息给线程
    转载ofstream和ifstream详细用法
    Effective STL笔记
    Making your C++ code robust
    TGA文件
  • 原文地址:https://www.cnblogs.com/Free-Thinker/p/9253774.html
Copyright © 2011-2022 走看看