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

今天没有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

最新文章

  1. sscanf_强大的数据读取-转换
  2. Public key for mysql....rpm is not installed
  3. PHP工作笔记:数组转字符串与字符串转数组
  4. 搭建TFS 2015 Build Agent环境(三)
  5. vector使用篇之erase
  6. Java开发中经典的小实例-(100能被3整除的数打印出来)
  7. 8. js中json格式解析
  8. --hdu 1231 最大连续子序列(动态规划)
  9. Fresco 源码分析(一) DraweeView-DraweeHierarchy-DraweeController(MVC) DraweeView的分析
  10. Celery - Best Practices
  11. 【SVN】win7 搭建SVN服务器
  12. ngrok逆向代理服务器搭建微信公众号本地开发环境
  13. Angular2案例rebirth开源
  14. C# Monads的实现(二)
  15. JSP知识点大纲图
  16. _0_web_基础
  17. BZOJ_2242_[SDOI2011]计算器_快速幂+扩展GCD+BSGS
  18. 6.04-news_xpath3
  19. 一、Composer
  20. RE:从零开始的莫比乌斯反演

热门文章

  1. ZOJ 2562 HDU 4228 反素数
  2. Android学习笔记(三)
  3. Linux经常使用命令(十六) - whereis
  4. OpenCASCADE Make Face With Holes
  5. occActiveX - ActiveX with OpenCASCADE
  6. 自建的IPV6管道
  7. 转 C#:使用MD5对用户密码加密与解密
  8. 【习题 8-1 UVA - 1149】Bin Packing
  9. Mblog 部署手册
  10. 洛谷——P1311 选择客栈