如何清除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”,但没有效果。 只有当我没有设置背景并离开默认灰色按钮层时,重绘才有效。
答
尝试
android:background="@null"
另外请注意,您要设置两个不同的特性。 setImageBitmap不是Android的对应方法:背景
b.setBackground(drawable)
b.setImageBitmap(bm);
答
我敢打赌,你的
android:src="@drawable/camera"
图像文件不具有透明背景。使用Photoshop或GIMP或类似的东西,剪出你的图像,并把它放在透明的背景上。
android:background =“@ null” –