如何清除ImageButton的背景?

问题描述:

我有一个ImageButton与默认可绘制图像。从代码中我设置了ImageButton的布局大小,所以在修改之后,由于centerInside的scaleType,图像并没有覆盖整个按钮。在OnClick事件中,我使用此代码替换ImageButton的图像。如何清除ImageButton的背景?

button.setImageBitmap(img); 
button.invalidate(); 

新图像比旧图像小。在替换出现新图像后,旧图像部分可以在新图像周围看到。我如何强制清除旧图像或者只是重新绘制整个ImageButton以仅显示新图像?

<ImageButton 
      android:id="@+id/btnPhotoFront" 
      android:layout_width="match_parent" 
      android:layout_height="wrap_content" 
      android:layout_margin="3dp" 
      android:layout_weight="1" 
      android:background="@android:color/transparent" 
      android:contentDescription="@string/front" 
      android:scaleType="centerInside" 
      android:src="@drawable/camera" /> 

我试过android:background =“#00000000”,但没有效果。 只有当我没有设置背景并离开默认灰色按钮层时,重绘才有效。

+0

android:background =“@ null” –

尝试

android:background="@null" 

另外请注意,您要设置两个不同的特性。 setImageBitmap不是Android的对应方法:背景

b.setBackground(drawable) 
    b.setImageBitmap(bm); 
+0

是的,我也试过这个。我需要背景属性将其设置为透明,并且需要一个不包含整个按钮的图像。问题是新图像不与旧图像重叠,并且旧图像的某些部分可见。 – 10yei

+0

我已经想出了一个解决方法。不漂亮,但工程。我创建了1x1像素大小的黑色png文件并将其放入绘图中。我用这个作为背景而不是颜色。在这种情况下,Android必须缩放此图像,并且在更改ImageButton的正面图像时强制进行预期的重绘。 – 10yei

我敢打赌,你的

android:src="@drawable/camera" 

图像文件不具有透明背景。使用Photoshop或GIMP或类似的东西,剪出你的图像,并把它放在透明的背景上。