1. 如何为自定义View添加动画效果
public void startAnimation() {
ValueAnimator anim = ValueAnimator.ofFloat(minValue, midValue, maxValue);
anim.setRepeatCount(ValueAnimator.INFINITE);//设置无限重复
anim.setRepeatMode(ValueAnimator.RESTART);//设置重复模式
anim.setDuration(1000);
anim.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() {
@Override
public void onAnimationUpdate(ValueAnimator animation) {
value = (Float) animation.getAnimatedValue();
postInvalidate();
}
});
anim.start();
}
即利用ValueAnimator,在动画的duration期间,产生一系列的value值,然后不断刷新来触发View的重绘,重绘过程中用到了这些value值,从而达到了动画效果
2.颜色渐变
int startColor = 0x33FFFFFF;
int endColor = 0xE6FF5800;
GradientDrawable gradient = (GradientDrawable) mButtonLayout.getBackground();
ValueAnimator colorAnim = ValueAnimator.ofObject(new ArgbEvaluator(), startColor, endColor);
// ValueAnimator colorAnim = ValueAnimator.ofArgb(startColor, endColor);
colorAnim.setDuration(3000);
colorAnim.start();
colorAnim.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() {
@Override
public void onAnimationUpdate(ValueAnimator animation) {
int color = (int) animation.getAnimatedValue();
gradient.setColor(color);
}
});
网友评论