是否可以随时间动画图像的饱和度(例如png)?例如,从灰度到全彩.如果我可以使用插值器.
我已经看过EffectFactory和ColorMatrix类,但是我无法将它们与动画/过渡相结合.
例如.在Drawable drawable上应用灰度饱和度:
ColorMatrix matrix = new ColorMatrix(); matrix.setSaturation(0); ColorMatrixColorFilter filter = new ColorMatrixColorFilter(matrix); drawable.setColorFilter(filter);
并在以后完全饱和:
matrix.setSaturation(1);
对于任何感兴趣的人,我的完整解决方案基于Simon的答案:
final ColorMatrix matrix = new ColorMatrix(); final Drawable drawable = ... ; ValueAnimator animation = ValueAnimator.ofFloat(0f,1f); animation.setDuration(1000); // animation.setInterpolator(); animation.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() { @Override public void onAnimationUpdate(ValueAnimator animation) { matrix.setSaturation(animation.getAnimatedFraction()); ColorMatrixColorFilter filter = new ColorMatrixColorFilter(matrix); drawable.setColorFilter(filter); } }); animation.start();
解决方法
使用ValueAnimator肯定可以实现:
ValueAnimator animation = ValueAnimator.ofFloat(0f,1f); animation.setDuration(1000); animation.addUpdateListener(new AnimatorUpdateListener() { @Override public void onAnimationUpdate(ValueAnimator animation) { matrix.setSaturation(animation.getAnimatedFraction()); } }); animation.start();