Search code examples
androidanimationtint

android tint animation doesn't work


I have custom ImageView which should animate its content after loading from solid background color to image with setColorFilter:

private void _animate() {
    int bgColor= ContextCompat.getColor(getContext(), R.color.iconPlaceholderBg);
    int transparent=ContextCompat.getColor(getContext(),android.R.color.transparent);
    ValueAnimator bgAnim= ValueAnimator.ofObject(new ArgbEvaluator(),bgColor,transparent);
    bgAnim.setDuration(5000);
    //bgAnim.setInterpolator(new DecelerateInterpolator());
    bgAnim.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() {
        @Override
        public void onAnimationUpdate(ValueAnimator animation) {
            //Log.d("anim",animation.getAnimatedFraction()+"")
            //Image.this.clearColorFilter();
            Image.this.setColorFilter((int)animation.getAnimatedValue());
        }
    });
    bgAnim.start();
}

Animation itself runs as expexted (5 seconds long).However content appears immediately in the final position(without color transformation).I've tried different PorterDuff.Mode modes but looks like none of them do things right.
Are there any workarounds to get desired effect?


Solution

  • The problem was with my background color which has tranparency.For some reason setColorFilter works not exactly as you expect with trasparent colors.I've managed to get desired behaviour with following workaround:

    Image.this.getDrawable().setAlpha((int) (animation.getAnimatedFraction()*255));