歡迎來到Linux教程網
Linux教程網
Linux教程網
Linux教程網
Linux教程網 >> Linux基礎 >> 關於Linux >> 翻翻git之---閃爍動畫的TextView RevealTextView

翻翻git之---閃爍動畫的TextView RevealTextView

日期:2017/3/1 12:16:04   编辑:關於Linux

今天上一個自身閃爍,用於吸引用戶注意力的TextView * RevealTextView*

先上下效果圖:(這圖片夠大的)

這裡寫圖片描述

How to use?<喎?http://www.2cto.com/kf/ware/vc/" target="_blank" class="keylink">vcD4NCjxwPkdyYWRsZTwvcD4NCjxwcmUgY2xhc3M9"brush:java;"> dependencies { compile 'com.antonionicolaspina:revealtextview:2.0' }

Eclipse
Copy下 attires.xml 和RevealTextView.java就行了,內容不多。

這裡寫圖片描述


接下來我們來拆拆實現,先看一下自定義Styleable的東西

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

就只有持續時間和文字

接下來看具體實現

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

這邊用一個數組作為整個字符串的容器,然後把每個字符生成一個隨機數放入容器中,然後開始多線程的操作。

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

在onAnimationUpdate中對字體進行二次處理最終實現了 圖中的效果

Copyright © Linux教程網 All Rights Reserved