× {{alert.msg}} Never ask again
Get notified about new tutorials RECEIVE NEW TUTORIALS

Android ImageView blur animation

Xaver Kapeller
Mar 13, 2015
<p>Blur effects are always difficult on Android. Essentially you have to decide between looks and performance. The better the blur looks the longer it takes, and if the blurring itself is not instantaneous then you cannot really animate the blur. </p> <p>You original blurring algorithm produces really good results, but because of that it is also very slow making a blur animation impossible. Just to demonstrate what it would take to effectively blur this image I have created a simple blur animation by scaling the bitmap down:</p> <pre><code>public class BlurAnimation extends Animation { private final ImageView imageView; private final Bitmap bitmap; private final float startValue; private final float stopValue; private final float difValue; private BlurAnimation(ImageView imageView, Bitmap bitmap, int startValue, int stopValue) { this.imageView = imageView; this.bitmap = bitmap; this.startValue = startValue; this.stopValue = stopValue; this.difValue = stopValue - startValue; } @Override protected void applyTransformation(float interpolatedTime, Transformation t) { super.applyTransformation(interpolatedTime, t); int current = (int)(this.difValue * interpolatedTime + this.startValue + 0.5f); Bitmap blurred = quickBlur(this.bitmap, current); this.imageView.setImageBitmap(blurred); } public Bitmap quickBlur(Bitmap bitmap, int factor) { if(factor &lt;= 0) { return Bitmap.createBitmap(1, 1, Bitmap.Config.ARGB_8888); } return Bitmap.createScaledBitmap(bitmap, bitmap.getWidth() / factor, bitmap.getHeight() / factor, true); } } </code></pre> <p>This works quite well (even though there is still some lag), but the results cannot be compared with your blur algorithm, it just looks terrible:</p> <p><img src="http://i.stack.imgur.com/eAC0R.gif" alt="enter image description here"></p> <p>So you see, it is very difficult to combine performance and good looks when it comes to blurring an image, but there are some options first and foremost <code>RenderScript</code>. <code>RenderScript</code> is very fast and has a built in gaussian blur filter. I have never used it, but from what I hear it might be the solution to your problem.</p> <p>You can also try to load already scaled down versions of an image, this would produce the same effect as in the gif above, but would be even faster. The drawback is that using this in an <code>Animation</code> is again problematic, but if you just need a blurred image and you don't really care about quality then you should go for this option.</p> <p>You can find more information about <code>RenderScript</code> and other fast blur options <a href="http://stackoverflow.com/a/23119957/2310866"><strong>in this answer</strong></a></p> <p>This tip was originally posted on <a href="http://stackoverflow.com/questions/24826057/Android%20ImageView%20blur%20animation/24862910">Stack Overflow</a>.</p>
comments powered by Disqus