Phil Andrews
Phil Andrews

Reputation: 409

Trouble creating a bitmap blur effect on imageview in android

I am trying to create a blurred effect on an image being displayed in an image view. I have came across an example that stated downsizing the image in a bitmap and then upscaling it again (bilinear filtering I think), so far the image quality is really poor. I am trying to achieve a nice smooth blur. The code I am currently using is:

originalImage = BitmapFactory.decodeResource(getResources(),
            mImageIds[coverFlow.getSelectedItemPosition()]);

    xPos = originalImage.getWidth() / 2;

    zoomedImage = Bitmap.createBitmap(originalImage, xPos / 2, yPos, width,
            height);

    int upWidth = (int) (width * 3.5); //width is 450 >> upWidth is 1575
    int upHeight = (int) (height * 3.5); //height is 700 >> upHeight is 2450

    int downWidth = (int) ((width * 1.75)*0.06); //width is 450 >> downWidth is 787.5
    int downHeight = (int) ((height * 1.75)*0.06); //height is 700 >> downHeight is 1225

    Bitmap ScaledUp = Bitmap.createScaledBitmap(zoomedImage, upWidth,
            upHeight, true);
    Bitmap BlurredImage = Bitmap.createScaledBitmap(ScaledUp, downWidth,
            downHeight, true);

    background.startAnimation(myFadeInAnimation);
    background.setImageBitmap(BlurredImage);
    background.setFocusable(true);

Could someone explain what is going wrong? I just can't seem to get a smooth blur, its always pixilated.

Upvotes: 1

Views: 1600

Answers (1)

Tas Morf
Tas Morf

Reputation: 3075

Off the top of my head, I would try something like the following:

//downscale Bitmap bmp = Bitmap.createScaledBitmap(originalBitmap, originalBitmap.getWidth()/scale, originalBitmap.getHeight()/scale, true); //blur Bitmap blurred = blurAlgorithm.blur(bmp, BLUR_RADIUS); //draw the blurred image Canvas c = new Canvas(blurred); //upscale c.scale(scale, scale);

For a good blur algorithm for android, have a look at this. Generally, that project has a lot of info on how to do blurring properly.

Upvotes: 1

Related Questions