Android ImageView中的PNG具有黑色背景

Android ImageView中的PNG具有黑色背景,android,android-imageview,Android,Android Imageview,“我的图像视图”设置为PNG位图。但问题是透明部分是黑色的。我已经将背景设置为@android:color/transparent和#00000000,但它仍然是黑色的。我试过搜索,但没有任何帮助。提前谢谢 以下是imageView及其父级的xml: <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" andr

“我的图像视图”设置为PNG位图。但问题是透明部分是黑色的。我已经将背景设置为@android:color/transparent和#00000000,但它仍然是黑色的。我试过搜索,但没有任何帮助。提前谢谢

以下是imageView及其父级的xml:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="@color/mainbg"
    android:orientation="vertical" >

    <LinearLayout
        android:id="@+id/vbTempLink"
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:layout_weight="15"
        android:background="@color/mainbg"
        android:orientation="vertical" >

        <LinearLayout
            android:id="@+id/linearLayout1"
            android:layout_width="match_parent"
            android:layout_height="0dp"
            android:layout_marginLeft="20dp"
            android:layout_marginRight="20dp"
            android:layout_weight="10"
            android:gravity="center"
            android:orientation="vertical" >

            <ImageView
                android:id="@+id/tempImage1"
                android:layout_width="match_parent"
                android:layout_height="0dp"
                android:layout_weight="25"
                android:background="@android:color/transparent"
                android:gravity="center"
                android:paddingBottom="5dp"
                android:paddingTop="5dp"
                android:scaleType="fitCenter" />

是的,这张图片的背景是透明的。

原来是关于我如何压缩位图的

bitmap.compress(Bitmap.CompressFormat.JPEG, 90, out);

没有别的办法,只有另一种办法。谢谢大家

我假设,如果您希望png背景是透明的,是因为后面还有其他东西。然后,我让透明的东西与此一起工作:

Drawable[] layers = new Drawable[2];
layers[0]=resources.getDrawable(colour);
layers[1]=resources.getDrawable(R.drawable.somethingToBeDrawn);
(someView).setImageDrawable(new LayerDrawable(layers));

其中,
color
是后面的图片,在我的例子中是绿色或灰色的png(这就是所谓的“颜色”)。

我知道这是一篇非常古老的文章,但这可能会帮助其他人

从您的代码中,我可以清楚地看到您是从本地存储中检索图像,而不是从Android资源中检索图像

Bitmap bitmap = BitmapFactory.decodeFile(path + s);
所以我认为您正在尝试检索JPEG压缩图像如果文件扩展名与其他压缩格式一起存储,则文件扩展名毫无意义

如果要存储和检索PNG图像,请将源图像存储为PNG格式,然后检索

bitmap.compress(Bitmap.CompressFormat.PNG, 100, out);

因为默认情况下,JPEG压缩将以黑色存储透明背景像素。所以最好避免将其压缩为JPEG格式

您是否使用新设备运行应用程序?在哪里设置png?并向我们显示ImageView的父级。父文件可能是黑色的png文件是否透明(即:是否在图层上,而不是背景上)?背景被删除了吗?我不知道你为什么要放透明的。我认为默认情况下这是透明的。请使用总文件更新。已更新。是的,文件已经更新了,但事实就是这样。修复方法是将其格式化为JPEG。“因为默认情况下JPEG压缩将以黑色存储透明背景像素。所以最好避免将其压缩为JPEG。”这是绝对正确的。我有png图像,我用jpeg压缩它,因此背景是黑色的。改为.png压缩解决了这个问题。@RaghavSharma,伙计!如何获得像WhatsApp贴纸一样透明的图像。在使用.PNG之后,仍然是黑色背景。
bitmap.compress(Bitmap.CompressFormat.PNG, 100, out);