Adnroid 闪烁动画
程序员文章站
2022-03-17 13:49:57
...
最近发现一个有趣的地图社交软件spot,主页面有一个home图片闪烁动画也是换了几种思路来完成,效果如下
![spot gif 显示效果](https://img-blog.csdnimg.cn/20190404102130154.gif)
首先完成有红色渐变白色动画并设置无限循环播放,
animator = ObjectAnimator.ofInt(textView, "backgroundColor", 0xffff0000, 0xffffffff);
animator.setEvaluator(new HsvEveluator());
animator.setDuration(500);
animator.start();
HavEveluator继承TypeEveluator 自己来写求值器,系统默认的求值器可能在渐变过程中出现其他颜色。
public class HsvEveluator implements TypeEvaluator<Integer> {
float[] startHsv = new float[3];
float[] endHsv = new float[3];
float[] outHsv = new float[3];
@Override
public Integer evaluate(float fraction, Integer startValue, Integer endValue) {
Color.colorToHSV(startValue, startHsv);
Color.colorToHSV(endValue, endHsv);
outHsv[0] = startHsv[0] + (endHsv[0] - startHsv[0]) * fraction;
outHsv[1] = startHsv[1] + (endHsv[1] - startHsv[1]) * fraction;
outHsv[2] = startHsv[2] + (endHsv[2] - startHsv[2]) * fraction;
int alpha = startValue >> 24 + (int) ((endValue >> 24 - startValue >> 24) * fraction);
return Color.HSVToColor(alpha, outHsv);
}
来看看这个动画
![](https://img-blog.csdnimg.cn/20190404103723598.gif)
如果设置的duration时间短,看不出来动画完成后突然变成初始值红色,其实也能用,再加一个相反颜色渐变更好
animator = ObjectAnimator.ofInt(textView, "backgroundColor", 0xffff0000, 0xffffffff);
animator.setEvaluator(new HsvEveluator());
animator.setDuration(500);
animator.addListener(new Animator.AnimatorListener() {
@Override
public void onAnimationStart(Animator animation) {
}
@Override
public void onAnimationEnd(Animator animation) {
nextanimator.cancel();
nextanimator.start();
}
@Override
public void onAnimationCancel(Animator animation) {
}
@Override
public void onAnimationRepeat(Animator animation) {
}
});
animator.start();
nextanimator = ObjectAnimator.ofInt(textView, "backgroundColor", 0xffffffff, 0xffff0000);
nextanimator.setEvaluator(new HsvEveluator());
nextanimator.addListener(new Animator.AnimatorListener() {
@Override
public void onAnimationStart(Animator animation) {
}
@Override
public void onAnimationEnd(Animator animation) {
animator.cancel();
animator.start();
}
@Override
public void onAnimationCancel(Animator animation) {
}
@Override
public void onAnimationRepeat(Animator animation) {
}
});
nextanimator.setDuration(500);
附加gif图
![最终效果](https://img-blog.csdnimg.cn/20190404105546567.gif)
好啦,完成拉!
}