-1

I want to set downloaded image into my imageview. But I need it as blur image. And i used universal image loader. So my question is how to set blur image using universal image loader and renderscript? thank you.

ImageLoader.getInstance().displayImage(QuickRepostApplication.getInstance().getImage(), main_image, options, new ImageLoadingListener() {

            @Override
            public void onLoadingStarted(String s, View view) {

            }

            @Override
            public void onLoadingFailed(String s, View view, FailReason failReason) {

            }

            @Override
            public void onLoadingComplete(String imageUri, View view, Bitmap loadedImage) {
                float r = 25.0f;
                Bitmap bitmap = Bitmap.createBitmap(
                        loadedImage.getWidth(), loadedImage.getHeight(),
                        Bitmap.Config.ARGB_8888);

                RenderScript renderScript = RenderScript.create(MainActivity.this);

                Allocation blurInput = Allocation.createFromBitmap(renderScript, loadedImage);
                Allocation blurOutput = Allocation.createFromBitmap(renderScript, bitmap);

                ScriptIntrinsicBlur blur = ScriptIntrinsicBlur.create(renderScript,
                        Element.U8_4(renderScript));
                blur.setInput(blurInput);
                blur.setRadius(r);
                blur.forEach(blurOutput);

                blurOutput.copyTo(bitmap);
                renderScript.destroy();
            }

            @Override
            public void onLoadingCancelled(String s, View view) {

            }

        }, new ImageLoadingProgressListener() {
            @Override
            public void onProgressUpdate(String imageUri, View view, int current, int total) {
            }
        });
Das
  • 141
  • 13

3 Answers3

0

you can try this method imageLoader.displayImage(images.get(position),child, options, new ImageLoadingListener() {});

Levy
  • 1
  • ImageLoadingListener this callback have a method onLoadingComplete(String s, View view, Bitmap bitmap) you can inside here do blur and then set on your imageview. – Levy Sep 29 '15 at 10:14
  • onLoadingComplete(String s, View view, Bitmap bitmap) (ImageView)view.setImageBitmap(bitmap),this bitmap you can do blur. – Levy Sep 29 '15 at 10:20
0

Use callback method of UIL that returns bitmap and then you can blur your bitmap as desired. Here is a sample project which can help you blur the downloaded image.

Blurry

Khawar Raza
  • 15,870
  • 24
  • 70
  • 127
  • but my question is how to set bitmap.... here very simple example given... i tried like that but no change... – Das Sep 29 '15 at 10:57
  • Use imageView.setImageBitmap(bitmap); to set your bitmap to imageview. If its not the case, clearly describe your problem and what you have done so far. If you can paste some code, it will be very helpful. – Khawar Raza Sep 29 '15 at 11:05
0

I found this post helpful. I am providing the main code here too for quick reference,

public class BlurBuilder {

//Increase scailing to increase the blurring. Maximum value = 1f
private static final float BITMAP_SCALE = 0.4f;

//0 <= BLUR_RADIUS <= 25f
private static final float BLUR_RADIUS = 7.5f;
RenderScript rs;

public static Bitmap blur(Context context, Bitmap image) {
    int width = Math.round(image.getWidth() * BITMAP_SCALE);
    int height = Math.round(image.getHeight() * BITMAP_SCALE);

    Bitmap inputBitmap = Bitmap.createScaledBitmap(image, width, height, false);
    Bitmap outputBitmap = Bitmap.createBitmap(inputBitmap);

    if(rs == null)
        rs = RenderScript.create(context);

    ScriptIntrinsicBlur theIntrinsic = ScriptIntrinsicBlur.create(rs, Element.U8_4(rs));
    Allocation tmpIn = Allocation.createFromBitmap(rs, inputBitmap);
    Allocation tmpOut = Allocation.createFromBitmap(rs, outputBitmap);
    theIntrinsic.setRadius(BLUR_RADIUS);
    theIntrinsic.setInput(tmpIn);
    theIntrinsic.forEach(tmpOut);
    tmpOut.copyTo(outputBitmap);

    return outputBitmap;
    }
}

This is only applicable for API level >= 17. With UIL, use it like this :

imageLoader.displayImage(userImage, backImage, op, new ImageLoadingListener() {
            @Override
            public void onLoadingStarted(String s, View view) {

            }

            @Override
            public void onLoadingFailed(String s, View view, FailReason failReason) {

            }

            @Override
            public void onLoadingComplete(String s, View view, Bitmap bitmap) {
              Bitmap blurredBitmap = BlurBuilder.blur(MyActivity.this, bitmap);
              view.setBackgroundDrawable(new BitmapDrawable( getResources(), blurredBitmap ) );
            }

            @Override
            public void onLoadingCancelled(String s, View view) {

            }
    });

Hope this helps.

Shubham A.
  • 2,446
  • 4
  • 36
  • 68
  • You really want to only create the RenderScript object once if you can avoid sinking it into your "blur" routine (i.e. have the first call to this function create your object, while subsequent calls can reuse it). While newer Android releases have sped up context creation, it will cause a lot of GC churn on older devices. – Stephen Hines Dec 11 '15 at 19:22