2

I know this topic has been discussed yet but I didn't find really what I wanna do.

I have those buttons (screenshot at the bottom). Now I want to add a outer glow. Is there an other possibility to do this than saving it as .png in the drawable folder? That would make much less work.

Greetings Nils

the buttons now:

Like I want it

nilskober
  • 188
  • 2
  • 12
  • for the **shadow** (a **glow** is something else), just use a 9 patch drawable. http://radleymarx.com/blog/simple-guide-to-9-patch/ – Phantômaxx Jun 11 '14 at 09:55
  • Say these red, green images are "ImageButton" then you can add background in xml to each ImageButton which would be an image with glow on boundaries and transparent at other places. – sagarpatidar Jun 11 '14 at 09:58
  • @DerGolem I thought it was called glow because in Illustrator it's named like this. But thanks, I will try your hint later on. – nilskober Jun 11 '14 at 10:06

1 Answers1

4

try this code

public Bitmap setGlow(int resourceId) {
    Bitmap bmp = null;
    try {
        int margin = 30;
        int halfMargin = margin / 2;

        int glowRadius = 15;

        int glowColor = Color.rgb(0, 192, 200);

        Bitmap src = BitmapFactory.decodeResource(getResources(),
                resourceId);

        Bitmap alpha = src.extractAlpha();

        bmp = Bitmap.createBitmap(src.getWidth() + margin, src.getHeight()
                + margin, Bitmap.Config.ARGB_8888);

        Canvas canvas = new Canvas(bmp);

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

        paint.setMaskFilter(new BlurMaskFilter(glowRadius, Blur.OUTER));
        canvas.drawBitmap(alpha, halfMargin, halfMargin, paint);

        canvas.drawBitmap(src, halfMargin, halfMargin, null);

    } catch (Exception e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
    return bmp;
}

and set the returned bitmap on your view

set in your imagebutton like this

btnClick.setImageBitmap(setGlow(R.drawable.ic_launcher));

Meenal
  • 2,879
  • 5
  • 19
  • 43
  • you can change the glow colour as per your need – Meenal Jun 11 '14 at 10:05
  • Thanks, but it doesn't work. I tried: Button test = (Button) findViewById(R.id.button1); Drawable d = new BitmapDrawable(getResources(), setGlow(R.id.buttonGruen) ); test.setBackgroundDrawable(d); Is there an another was to set the BitMap as background or as a src of an ImageButton – nilskober Jun 11 '14 at 10:49
  • see my edited answer..i set it on imagebutton and it worked..you can also take imageview – Meenal Jun 11 '14 at 11:04
  • only works with an drawable as background but not with a color like in my case – nilskober Jun 11 '14 at 11:25
  • ya it work with images..sorry might be you had mentioned that it is colour..i mistakenly read it as images – Meenal Jun 11 '14 at 11:29