Search code examples
androidandroid-image

How to apply filter to images loaded by Android ImageLoader?


I'm using the Android-Universal-Image-Loader library to load my images and do the caching etc. I'm trying to apply a box blur filter to it, using the JH Labs library. I tried to apply to filter in the onLoadingComplete event but it doesn't seem like the bitmap there in mutable. Has anyone had experience of applying a filter to bitmaps loaded by Android ImageLoader?


Solution

  • I've finally gotten this to work. It was the right approach to apply the filter in the onLoadingComplete event. However, since the bitmap there is inmutable, the key is to pass in the ImageView and call the setImageBitmap method again to set the filtered bitmap to the ImageView:

    public void displayImageBlurred(String imageUri, final ImageView imageView) {
        this.displayImage(imageUri, imageView, true, 0, new SimpleImageLoadingListener() {
    
            @Override
            public void onLoadingComplete(Bitmap loadedImage) {
                int width = loadedImage.getWidth();
                int height = loadedImage.getHeight();
    
                BoxBlurFilter filter = new BoxBlurFilter();
                filter.setRadius(8.5f);
    
                int[] src = AndroidUtils.bitmapToIntArray(loadedImage);
                src = filter.filter(src, width, height);
    
                loadedImage = Bitmap.createBitmap(src, width, height, Config.ARGB_8888);
                imageView.setImageBitmap(loadedImage);
            }
        });
    }
    
    public void displayImage(String imageUri, final ImageView imageView, boolean callBackIfCached, long startTime, ImageLoadingListener listener) {
        ImageLoader imageLoader = ImageLoader.getInstance();
        imageLoader.displayImage(imageUri, imageView, defaultOptions, listener);
    }