zoukankan      html  css  js  c++  java
  • 翻翻git之---闪烁动画的TextView RevealTextView

    转载请注明出处:王亟亟的大牛之路

    今天没有P1啦!。 对换工作有想法的。能够找昨天的P1。哈哈 地址:http://blog.csdn.net/ddwhan0123/article/details/50728434


    今天上一个自身闪烁,用于吸引用户注意力的TextView * RevealTextView*

    先上下效果图:(这图片够大的)

    这里写图片描写叙述

    How to use?

    Gradle

    dependencies {
      compile 'com.antonionicolaspina:revealtextview:2.0'
    }

    Eclipse
    Copy下 attires.xml 和RevealTextView.java即可了,内容不多。

    这里写图片描写叙述


    接下来我们来拆拆实现。先看一下自己定义Styleable的东西

     <declare-styleable name="RevealTextView">
            <attr name="rtv_duration" format="integer" />
            <attr name="android:text" />
        </declare-styleable>

    就仅仅有持续时间和文字

    接下来看详细实现

    public final class RevealTextView extends TextView implements Runnable, ValueAnimator.AnimatorUpdateListener

    继承于TextView 实现多线程以及动画的时间,一看就知道 他没实用诸如Handler的实现,纯粹的主线程子线程的相互操作配合动画来实现闪烁效果

      protected void init(TypedArray attrs) {
        try {
          animationDuration = attrs.getInteger(R.styleable.RevealTextView_rtv_duration, animationDuration);
          text = attrs.getString(R.styleable.RevealTextView_android_text);
        } finally {
          attrs.recycle();
        }
    
        setAnimatedText(text);
      }

    接着就是初始化,然后就是调用開始动画的操作,这里是把标签传来的字送了过去。

    public void setAnimatedText(String text) {
        this.text = text;
        alphas    = new double[text.length()];
        for(int i=0; i<text.length(); i++) {
          alphas[i] = Math.random() - 1.0f;
        }
    
        setText(text);
    
        replayAnimation();
      }

    这边用一个数组作为整个字符串的容器。然后把每一个字符生成一个随机数放入容器中,然后開始多线程的操作。

    public void replayAnimation() {
        if (null != text) {
          post(this);
        }
      }

    当字符串不为空開始运行多线程操作。

    而且这2个方法是public的,给予我们外部调用的。

     @Override
      public void run() {
        final int color = getCurrentTextColor();
    
        red   = Color.red(color);
        green = Color.green(color);
        blue  = Color.blue(color);
    
        ValueAnimator animator = ValueAnimator.ofFloat(0f, 2f);
        animator.setDuration(animationDuration);
        animator.addUpdateListener(this);
        animator.start();
      }

    在run方法中运行了 我们的动画的初始化操作

      @Override
      public void onAnimationUpdate(ValueAnimator valueAnimator) {
        final float value = (float) valueAnimator.getAnimatedValue();
        SpannableStringBuilder builder = new SpannableStringBuilder(text);
        for(int i=0; i<text.length(); i++) {
          builder.setSpan(new ForegroundColorSpan(Color.argb(clamp(value + alphas[i]), red, green, blue)), i, i+1, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
        }
        setText(builder);
      }

    在onAnimationUpdate中对字体进行二次处理终于实现了 图中的效果

    作者git:https://github.com/ANPez/RevealTextView

    下载地址:https://github.com/ANPez/RevealTextView/archive/master.zip

  • 相关阅读:
    POJ数据结构专辑(含部分题解)
    第K小数 uva 10041 Vito's Family poj 2388 Who's in the Middle
    POJ 1195 Mobile phones (二维树状树组)
    python 学习体会
    ACM竞赛常用STL(二)之STLalgorithm
    计算机科学中的树
    ctf古典密码从0到
    漏洞挖掘的艺术面向源码的静态漏洞挖掘
    漏洞挖掘的艺术面向二进制的静态漏洞挖掘
    实战演示 H5 性能分析
  • 原文地址:https://www.cnblogs.com/zhchoutai/p/7111369.html
Copyright © 2011-2022 走看看