How to make glow effect around a bitmap?

Hongbo picture Hongbo · Dec 2, 2010 · Viewed 25.4k times · Source

The following code is what I got so far. However, there are 2 issues:

  1. I want both inner and outer glow effects, which look similar to the Photoshop's blending options. But I only managed to make the outer glow, if I set BlurMaskFilter.Blur.INNER or other value, the whole image is blocked, instead of just edges.

  2. Despite I set "FF" as alpha value, the glow color is still very dark.

    Bitmap alpha = origin.extractAlpha();
    BlurMaskFilter blurMaskFilter = new BlurMaskFilter(5, BlurMaskFilter.Blur.OUTER);
    
    Paint paint = new Paint();
    paint.setMaskFilter(blurMaskFilter);
    paint.setColor(0xffffffff);
    
    Canvas canvas = new Canvas(origin);
    canvas.drawBitmap(alpha, 0, 0, paint);
    
    return origin;
    

alt text

Answer

Nik picture Nik · Dec 23, 2013

Try this, based on XGouchet's answer.

private void setBackgroundGlow(ImageView imgview, int imageicon,int r,int g,int b)
{
    // An added margin to the initial image
    int margin = 24;
    int halfMargin = margin / 2;
    // the glow radius
    int glowRadius = 40;

    // the glow color
    int glowColor = Color.rgb(r, g, b);

    // The original image to use
    Bitmap src = BitmapFactory.decodeResource(getResources(),imageicon);

    // extract the alpha from the source image
    Bitmap alpha = src.extractAlpha();

    // The output bitmap (with the icon + glow)
    Bitmap bmp =  Bitmap.createBitmap(src.getWidth() + margin, src.getHeight() + margin, Bitmap.Config.ARGB_8888);

    // The canvas to paint on the image
    Canvas canvas = new Canvas(bmp);

    Paint paint = new Paint();
    paint.setColor(glowColor);

    // outer glow
    paint.setMaskFilter(new BlurMaskFilter(glowRadius, Blur.OUTER));//For Inner glow set Blur.INNER
    canvas.drawBitmap(alpha, halfMargin, halfMargin, paint);

    // original icon
    canvas.drawBitmap(src, halfMargin, halfMargin, null);

    imgview.setImageBitmap(bmp);


}