2013-01-05 100 views
2

我一直在寻找这一段时间,但不能真正找出任何干净的方式来做到这一点。Android:更改src alpha并保持背景(混合src与颜色)

我开发了一些按钮的应用程序,主要是这样定义的:

<ImageButton 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:background="#000000" 
    android:contentDescription="@null" 
    android:src="@drawable/btn1" /> 

有一个onTouchListener改变与setAlpha(0.5f)按钮α,但它也改变了背景阿尔法,这是不是我需要。我想让它看起来更黑暗,而不是透明。

我看到这个问题:Android Graphics: Changing the alpha of just an image (not the background),并且该解决方案实际上可行,但对我而言,在我的应用中为每个按钮创建一个FrameLayout听起来不太合适。

鉴于背景是平坦的颜色,我认为会有一个更简单的解决方案,比如颜色混合或类似的东西,但是在任何地方都找不到它。

SOLUTION

刚刚发现了这个帖子:http://tech.chitgoks.com/2011/04/17/android-change-imagebutton-tint-to-simulate-button-pressed/

解决办法很简单,只要:

button.setColorFilter(Color.argb(150,0,0,0)); 

回答

2

可以使用getDrawable()方法获取图像绘制,并试图改变其alpha 。

+0

谢谢,它实际上做到了。但是如果我的纽扣是圆形的呢?有没有办法让它们变暗而没有大的黑色方块? – Aboud