2013-05-15 41 views
0

我有一個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」 –

回答

1

嘗試

android:background="@null" 

另外請注意,您要設置兩個不同的特性。 setImageBitmap不是Android的對應方法:背景

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

是的,我也試過這個。我需要背景屬性將其設置爲透明,並且需要一個不包含整個按鈕的圖像。問題是新圖像不與舊圖像重疊,並且舊圖像的某些部分可見。 – 10yei

+0

我已經想出了一個解決方法。不漂亮,但工程。我創建了1x1像素大小的黑色png文件並將其放入繪圖中。我用這個作爲背景而不是顏色。在這種情況下,Android必須縮放此圖像,並且在更改ImageButton的正面圖像時強制進行預期的重繪。 – 10yei

0

我敢打賭,你的

android:src="@drawable/camera" 

圖像文件不具有透明背景。使用Photoshop或GIMP或類似的東西,剪出你的圖像,並把它放在透明的背景上。