使用通用图像加载器进行图像模糊的Android renderscript

时间:2015-09-29 09:48:32

标签: android universal-image-loader renderscript

我想将下载的图像设置到我的imageview中。但我需要它作为模糊图像。我使用通用图像加载器。所以我的问题是如何使用通用图像加载器和renderscript设置模糊图像?谢谢。

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) {
            }
        });

3 个答案:

答案 0 :(得分:0)

你可以尝试这种方法 imageLoader.displayImage(images.get(position),child,options,new ImageLoadingListener(){});

答案 1 :(得分:0)

使用UIL的回调方法返回位图,然后根据需要模糊位图。这是一个示例项目,可以帮助您模糊下载的图像。

Blurry

答案 2 :(得分:0)

我发现this帖子很有帮助。我也在这里提供主要代码以供快速参考,

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;
    }
}

对于API级别&gt; = 17,这只是适用。对于UIL,请像这样使用:

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) {

            }
    });

希望这有帮助。