Android button with outer glow

若如初见. 提交于 2019-12-11 02:50:57

问题


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


回答1:


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



来源:https://stackoverflow.com/questions/24159859/android-button-with-outer-glow

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!