好用的开源项目--RevealTextView

前言

这段时间有点小偷懒^_^,好久没有更新文章了,想想都有点罪恶感ヽ(●-`Д´-)ノ。为了弥补我的罪恶感,我准备给自己的博客新加一个系列————好用的开源项目(PS:名字有点low,大家多多包含), 这个系列将为大家带来 GitHub 上面一些好用、有趣开源项目的使用,及个人的一点小见解(我会尽量月月更的。。。)。让我们一起学习吧!一起进步吧!

本博客同步发布于 XueLong的博客

作为本系列的第一个开源项目,今天的主角是————RevealTextView
项目地址: https://github.com/ANPez/RevealTextView

项目源代码

简介

A TextView subclass to show a reveal effect
文字淡入效果的TextView。
运行环境:Android 4.0+, API 14+

使用方式:
Gradle依赖:

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

布局文件中:

<com.antonionicolaspina.revealtextview.RevealTextView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    app:rtv_duration="2000"
    android:text="Hello Reveal Text!"/>

效果图
这里写图片描述

实现思路

自定义TextView,利用多线程控制动画的时间,主要是借助SpannableStringBuilder来实现文字的闪烁效果的。
首先在初始化时,调用开始动画的操作

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) {
    if (TextUtils.isEmpty(text)) {
        return;
    }
    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();
}

接下来就是在run方法中启动动画。

@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.setInterpolator(new LinearInterpolator());
    animator.addUpdateListener(this);
    if (isLoop) {
        animator.setRepeatMode(ValueAnimator.REVERSE);
        animator.setRepeatCount(ValueAnimator.INFINITE);
    }
    animator.start();
}

最后就是关于动画的更新。

@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);
  }

写在最后

讲解可能不是很清楚,还存在着诸多漏洞,请多包涵。

如果你在参考过程中遇到问题,可以在我的联系方式中给我提问。

后面会继续介绍,Android的相关知识,欢迎继续关注我博客的更新。

项目源代码

参考资源

转载请注明:XueLong的博客 » 好用的开源项目–RevealTextView

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值