Android button with outer glow
Asked Answered
C

1

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

Cuboid answered 11/6, 2014 at 9:53 Comment(3)
for the shadow (a glow is something else), just use a 9 patch drawable. radleymarx.com/blog/simple-guide-to-9-patchPears
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.Fudge
@DerGolem I thought it was called glow because in Illustrator it's named like this. But thanks, I will try your hint later on.Cuboid
R
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));

Reposit answered 11/6, 2014 at 10:5 Comment(5)
you can change the glow colour as per your needReposit
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 ImageButtonCuboid
see my edited answer..i set it on imagebutton and it worked..you can also take imageviewReposit
only works with an drawable as background but not with a color like in my caseCuboid
ya it work with images..sorry might be you had mentioned that it is colour..i mistakenly read it as imagesReposit

© 2022 - 2024 — McMap. All rights reserved.