Colorfade
我正在寻找过渡动画:
3(或更多)之间的淡化。
重复
并且可以应用于RelativeLayout
的背景或LinearLayout
的免费
我试过了:
TransitionDrawable
和ColorDrawable
:
relativeLayout = (RelativeLayout)findViewById(R.id.screen1);
ColorDrawable[] color = {new ColorDrawable(Color.parseColor("#7cc57a")), new ColorDrawable(Color.parseColor("#CAC132")) , new ColorDrawable((Color.parseColor("#E79893")))};
TransitionDrawable trans = new TransitionDrawable(color);
relativeLayout.setBackgroundDrawable(trans);
trans.startTransition(7000);
但它只在两种颜色之间消失,不再重复......
有没有可以解决此问题的代码?或者我应该添加一些 whenCompleted()
- 方法吗?
答案 0 :(得分:0)
为此目的创建的示例app和 ColorFader 类(多种颜色之间的动画)可在此处获取 - https://github.com/maciejsikora/ColorFader。
针对此问题的简单解决方案,但仅限于3种颜色:
public class ColorChanger {
private int from_color;
private int to_color;
private int currentColor;
private int interval=1000;//default iterval
private ValueAnimator colorAnimation;
private int[] colors=new int[3];
public ColorChanger(int color1, int color2, int color3){
colors[0]=color1;
colors[1]=color2;
colors[2]=color3;
this.currentColor=0;
from_color=colors[0];
to_color=colors[1];
}
public ColorChanger run(final View view){
colorAnimation = ValueAnimator.ofObject(new ArgbEvaluator(), from_color, to_color);
colorAnimation.setDuration(this.interval);
colorAnimation.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() {
@Override
public void onAnimationUpdate(ValueAnimator animator) {
view.setBackgroundColor((Integer) animator.getAnimatedValue());
}
});
colorAnimation.addListener(new Animator.AnimatorListener() {
@Override
public void onAnimationStart(Animator animation) {
}
@Override
public void onAnimationEnd(Animator animation) {
from_color=colors[currentColor];
if (currentColor+1>=colors.length){
currentColor=0;
}else{
currentColor++;
}
to_color=colors[currentColor];
//run again
run(view);
}
@Override
public void onAnimationCancel(Animator animation) {
}
@Override
public void onAnimationRepeat(Animator animation) {
}
});
colorAnimation.start();
return this;
}
public void setIntervalMs( int ms){
this.interval=ms;
}
}
使用示例:
ColorChanger colorChanger = new ColorChanger(getResources().getColor(R.color.red),
getResources().getColor(R.color.blue),
getResources().getColor(R.color.green));
colorChanger.run(findViewById(R.id.content));