Image Saturation Animation

Is it possible to animate an image saturation (e.g. png) over time? For example, from shades of gray to full color. Plus, if I can use Interpolator.

I have seen EffectFactory and ColorMatrix classes, but I cannot combine them with animation / transition.

eg. applying the grayscale saturation on the Drawable drawable :

 ColorMatrix matrix = new ColorMatrix(); matrix.setSaturation(0); ColorMatrixColorFilter filter = new ColorMatrixColorFilter(matrix); drawable.setColorFilter(filter); 

and for full saturation later:

 matrix.setSaturation(1); 

For anyone interested in my complete solution based on Simon's answer:

 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(); 
+6
source share
2 answers

This can be achieved using 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(); 
+9
source

You need to animate it yourself!

It shouldn't be that hard.

See the ColorMatrix example in the API daemon folder.

http://alvinalexander.com/java/jwarehouse/android-examples/platforms/android-2/samples/ApiDemos/src/com/example/android/apis/graphics/ColorMatrixSample.java.shtml

You can extend the Drawable and increase the value in the onDraw method, and then set the filter accordingly.

You get the animation by calling invalidate() every few ms in onDraw . Remember to slow the iteration by turning the thread into sleep mode. Thread.sleep(40); for 25 fps

0
source

Source: https://habr.com/ru/post/974230/


All Articles