关于android中ImageView中的png图像透明背景变黑的问题

it2023-10-11  79

做一个悬浮窗,显示一张图片在桌面上。结果本来是透明的png图片背景全部成为了黑色。如下图所示: 上面图片的代码是这样的:

<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" tools:ignore="MissingDefaultResource" android:orientation="horizontal"> <ImageView android:layout_width="90dp" android:layout_height="90dp" android:id="@+id/img_ftjy" android:src="@drawable/tjy"/> </LinearLayout>

如图所示,太极鱼周围本来是透明的,现在全成为黑色。上网上搜了一下,大部分说是从软件导出的原因,导致格式成为bmp或者jpg格式的。但我反复查看,通过不同的看图软件确定我的这个图片确实是透明背景的png图片。且是本地用,不是从网上找的图片。 偶尔在图片后面加了一个SurfaceView,发现图片竟然透明了。但是SurfaceView成黑色的了。代码如下面所示:

<ImageView android:layout_width="90dp" android:layout_height="90dp" android:id="@+id/img_ftjy" android:src="@drawable/tjy"/> <SurfaceView android:id="@+id/surfaceView" android:layout_width="match_parent" android:layout_height="match_parent"/>

在ImageView后面加了SurfaceView 如下图所示: l图片竟然透明了。但是SurfaceView是黑色的。试了试在后面加入TextView,不行,图片背景还是黑色的。即然SurfaceView可以,那就加它,把它的尺寸设置小一点。还直行。

<ImageView android:layout_width="90dp" android:layout_height="90dp" android:id="@+id/img_ftjy" android:src="@drawable/tjy"/> <SurfaceView android:id="@+id/surfaceView" android:layout_width="0.1dp" android:layout_height="0.1dp"/>

宽、高均为0.1dp,最终效果: 图片背景透明了。 虽然实现了效果。但是不知道原理。不知道还有没有其它办法?

最新回复(0)