0

我有一个带有默认可绘制图像的 ImageButton。从代码中我设置了 ImageButton 布局大小,因此在此修改后,由于 centerInside scaleType,图像不会覆盖整个按钮。在 OnClick 事件中,我使用此代码替换 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" 但没有效果。仅当我不设置背景并保留默认的灰色按钮层时,重绘才有效。

4

2 回答 2

1

尝试

android:background="@null"

另请注意,您正在设置两个不同的属性。setImageBitmap 不是 android:background 的对应方法

    b.setBackground(drawable)
    b.setImageBitmap(bm);
于 2013-05-15T14:02:50.373 回答
0

我打赌你

android:src="@drawable/camera" 

图像文件没有透明背景。使用 Photoshop 或 GIMP 或类似的工具,剪下您的图像并将其放在透明背景上。

于 2015-01-27T12:21:42.310 回答