美文网首页
闪烁的TextView(可配置颜色)

闪烁的TextView(可配置颜色)

作者: 郑伟1 | 来源:发表于2018-09-03 15:52 被阅读0次

    排版能累死个人

    /**
      *闪烁的文字
      */
    public class FlashTextViewextends TextView {
    
      private LinearGradientmLinearGradient;
    
      private MatrixmGradientMatrix;
    
      private PaintmPaint;
    
      private int mViewWidth =0;
    
      private int mTranslate =0;
    
      private boolean mAnimating =true;
    
      private int time=0;
    
      private int color01=0xff2591FF;
    
      private int color02=0x332591FF;
    
      private int color03=0xff2591FF;
    
      private int[]colors=new int[] {color01, color02,color03};
      
    
    public FlashTextView(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 (mViewWidth ==0) {
    
                mViewWidth = getMeasuredWidth();
    
            if (mViewWidth >0) {
    
                mPaint = getPaint();
    
                mLinearGradient =new LinearGradient(-mViewWidth, 0, 0, 0,colors,
    
                      null, Shader.TileMode.CLAMP);
    
                mPaint.setShader(mLinearGradient);
    
                mGradientMatrix =new Matrix();
    
                      }
    
              }
     }
    
      public boolean ismAnimating() {
    
          return mAnimating;
    
        }
    
      public void setmAnimating(boolean mAnimating) {
    
          this.mAnimating = mAnimating;
    
        }
    
      public int getColor01() {
    
          return color01;
    
      }
    
      public void setColor01(int color01) {
    
          this.color01 = color01;
    
      }
    
      public int getColor02() {
    
          return color02;
    
      }
    
      public void setColor02(int color02) {
    
          this.color02 = color02;
    
      }
    
      public int getColor03() {
    
          return color03;
    
      }
    
      public void setColor03(int color03) {
    
          this.color03 = color03;
    
      }
    
      public int[]getColors() {
    
          return colors;
    
      }
    
       public void setColors(int[] colors) {
    
          this.colors = colors;
    
      }
    
      @Override
      protected void onDraw(Canvas canvas) {
    
          super.onDraw(canvas);
    
          if (mAnimating &&mGradientMatrix !=null) {
    
            mTranslate +=mViewWidth /25;
    
            mGradientMatrix.setTranslate(mTranslate, 0);
    
            mLinearGradient.setLocalMatrix(mGradientMatrix);
    
            if (mTranslate>=2 *mViewWidth){
    
              mTranslate=0;
    
              new Handler().postDelayed(new Runnable() {
    
                @Override
                public void run() {
                        postInvalidateDelayed(25); 
                    }
                  },5000);
    
            }else{
    
                postInvalidateDelayed(25);
    
            }
    
         }
    
      }
    
    }
    
    

    相关文章

      网友评论

          本文标题:闪烁的TextView(可配置颜色)

          本文链接:https://www.haomeiwen.com/subject/dqfiwftx.html