zoukankan      html  css  js  c++  java
  • android继承TextView实现文本彩色闪烁

       闲来无事,收到街边广告LED的启发,想着自己实现一个类似的彩色字体闪烁的效果。

       设计思路

       我们知道android的LinearGradient可以实现颜色渐变的背景,那么如果只有速度够快,通过在水平方向上不断的平移LinearGradient就可以渲染出文字闪烁的效果了。代码如下:

    package com.example.customview.view;
    
    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.util.Log;
    import android.view.View.MeasureSpec;
    import android.view.WindowManager;
    import android.widget.TextView;
    
    public class CustomTextView extends TextView {
        
        private final static String TAG = CirclePercentView.class.getSimpleName();
        private Paint paint1;
        private Paint paint2;
        
        private int mWidth;
        private LinearGradient gradient;
        private Matrix matrix;
        //渐变的速度
        private int deltaX;
    
        public CustomTextView(Context context) {
            super(context, null);
        }
    
        public CustomTextView(Context context, AttributeSet attrs) {
            super(context, attrs);
            initView(context, attrs);
            // TODO Auto-generated constructor stub
        }
    
        private void initView(Context context, AttributeSet attrs) {
            paint1 = new Paint();
            paint1.setColor(getResources().getColor(android.R.color.holo_blue_dark));
            paint1.setStyle(Paint.Style.FILL);
    
        }
    
        
        @Override
        protected void onSizeChanged(int w, int h, int oldw, int oldh) {
            // TODO Auto-generated method stub
            super.onSizeChanged(w, h, oldw, oldh);
            if(mWidth == 0){
                Log.e(TAG,"*********************");
                mWidth = getMeasuredWidth();
                paint2 = getPaint();
                //颜色渐变器
                gradient = new LinearGradient(0, 0, mWidth, 0, new int[]{Color.BLUE,Color.GREEN,Color.RED}, null, Shader.TileMode.CLAMP);
                paint2.setShader(gradient);
                
                matrix = new Matrix();
            }
        }
    
        @Override
        protected void onDraw(Canvas canvas) {
            super.onDraw(canvas);
            if(matrix !=null){
                deltaX+=mWidth/5;
                if(deltaX>2*mWidth){
                    deltaX = -mWidth;
                }
            }
            //关键代码通过矩阵的平移实现
            matrix.setTranslate(deltaX, 0);
            gradient.setLocalMatrix(matrix);
            postInvalidateDelayed(10);
        }
    }
    View Code

    运行效果如下:

  • 相关阅读:
    SDUT OJ 河床
    BZOJ 1500: [NOI2005]维修数列( splay )
    BZOJ 2049: [Sdoi2008]Cave 洞穴勘测( LCT )
    BZOJ 3401: [Usaco2009 Mar]Look Up 仰望( 单调栈 )
    BZOJ 1552: [Cerc2007]robotic sort( splay )
    BZOJ 1251: 序列终结者( splay )
    BZOJ 1576: [Usaco2009 Jan]安全路经Travel( 树链剖分 )
    BZOJ 3408: [Usaco2009 Oct]Heat Wave 热浪( 最短路 )
    BZOJ 3403: [Usaco2009 Open]Cow Line 直线上的牛( deque )
    BZOJ 3407: [Usaco2009 Oct]Bessie's Weight Problem 贝茜的体重问题( dp )
  • 原文地址:https://www.cnblogs.com/sharkli/p/5978637.html
Copyright © 2011-2022 走看看