简体   繁体   中英

Animate ImageView saturation

I am trying to animate some imageviews from color to grayscale and later from grayscale back to full.

This works fine but it doesn't animate:

        ColorMatrix matrixGrey = new ColorMatrix();
        matrixGrey.setSaturation(0);
        filterGrey = new ColorMatrixColorFilter(matrixGrey);

        ColorMatrix matrixFull = new ColorMatrix();
        matrixFull.setSaturation(1);
        filterFull = new ColorMatrixColorFilter(matrixFull);

        card1.setColorFilter(filterFull);
        card2.setColorFilter(filterGrey);

With the help of this post: Animate image's saturation i tried this to animate from full color to grayscale.

ValueAnimator animation = ValueAnimator.ofFloat(1f, 0f);
            animation.setDuration(10000);
//   animation.setInterpolator();
            animation.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() {
                @Override
                public void onAnimationUpdate(ValueAnimator animation) {
                    matrix.setSaturation(animation.getAnimatedFraction());
                    ColorMatrixColorFilter filter = new ColorMatrixColorFilter(matrix);
                    v.setColorFilter(filter);
                }
            });
            animation.start();

It still animates from grayscale to color even if i changed the ofFloat values. What am i doing wrong here? card1, card2 and v is ImageViews.

使用 animation.animatedValue 而不是 animation.animatedFraction

matrix.setSaturation(animation.animatedValue as Float)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM