【问题标题】:Android button with outer glow带外发光的 Android 按钮
【发布时间】:2014-08-01 07:54:49
【问题描述】:

我知道这个话题已经讨论过了,但我没有找到真正想要做的事情。

我有这些按钮(屏幕截图在底部)。现在我想添加一个外发光。除了将其保存为可绘制文件夹中的 .png 之外,还有其他可能吗?这样可以减少工作量。

问候 尼尔斯

【问题讨论】:

  • 对于 shadowglow 是别的东西),只需使用 9 补丁可绘制。 radleymarx.com/blog/simple-guide-to-9-patch
  • 假设这些红色、绿色图像是“ImageButton”,那么您可以在 xml 中为每个 ImageButton 添加背景,这将是一个在边界上发光并在其他地方透明的图像。
  • @DerGolem 我认为它被称为发光,因为在 Illustrator 中它是这样命名的。不过谢谢,我稍后会尝试你的提示。
  • @yosagar 非常感谢。试过了,完美! :)

标签: android button glow


【解决方案1】:

试试这个代码

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

并在您的视图上设置返回的位图

像这样在你的图像按钮中设置

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

【讨论】:

  • 您可以根据需要更改发光颜色
  • 谢谢,但它不起作用。我试过: Button test = (Button) findViewById(R.id.button1); Drawable d = new BitmapDrawable(getResources(), setGlow(R.id.buttonGruen) ); test.setBackgroundDrawable(d);是否有另一种方法是将 BitMap 设置为背景或 ImageButton 的 src
  • 查看我编辑的答案..我在 imagebutton 上设置它并且它工作..你也可以采取 imageview
  • 仅适用于可绘制的背景,但不适用于我的情况
  • 是的,它适用于图像..对不起,您可能提到它是彩色的..我错误地将其读作图像
猜你喜欢
  • 2017-03-27
  • 2011-08-08
  • 1970-01-01
  • 1970-01-01
  • 2011-01-30
  • 2014-03-08
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多