【问题标题】:How can clear ImageButton's background?如何清除 ImageButton 的背景?
【发布时间】:2013-05-15 13:20:50
【问题描述】:

我有一个带有默认可绘制图像的 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" 但没有效果。 仅当我不设置背景并保留默认的灰色按钮层时,重绘才有效。

【问题讨论】:

  • android:background="@null"

标签: android bitmap background imagebutton


【解决方案1】:

试试

android:background="@null"

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

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

【讨论】:

  • 是的,我也试过这个。我需要背景属性将其设置为透明,并且需要一个不覆盖整个按钮的图像。问题是新图像不会与旧图像重叠,并且旧图像的某些部分是可见的。
  • 我想出了一个解决方法。不漂亮但有效。我创建了 1x1 像素大小的黑色 png 文件并将其放入可绘制对象中。我用它作为背景而不是颜色。在这种情况下,Android 必须缩放此图像,这会在我更改 ImageButton 的正面图像时强制进行预期的重绘。
【解决方案2】:

我赌你

android:src="@drawable/camera" 

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2013-09-16
    • 1970-01-01
    • 2016-11-23
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多