ImageView中的PNG具有黑色背景

时间:2014-07-08 12:42:55

标签: android android-imageview

我的imageview设置为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" />

我用这些代码设置了png(bg是字符串):

background.setBackgroundDrawable(bitmapToDrawable((bg)));

public Drawable bitmapToDrawable(String s) {
    Bitmap bitmap = BitmapFactory.decodeFile(path + s);
    Drawable drawable = new BitmapDrawable(getResources(), bitmap);
    return drawable;
}

是的,图像肯定有透明的背景。

3 个答案:

答案 0 :(得分:4)

我知道这篇文章很老,但这可能有助于其他人。

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

Bitmap bitmap = BitmapFactory.decodeFile(path + s);

所以我认为您正在尝试检索JPEG压缩图像。 如果文件扩展名已经存储了一些其他压缩格式,那么它们就毫无意义。

如果您想存储和检索PNG图像,请以PNG格式存储源图像,然后将其检索。

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

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

答案 1 :(得分:2)

原来我是如何压缩位图的。

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

别无他法,只能找到另一种方式。谢谢你们!

答案 2 :(得分:0)

我认为如果你想让png背景透明是因为后面还有别的东西。然后,我使用透明的东西:

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

其中colour是后面的图片,在我的例子中是绿色或灰色的png(这就是它被称为&#34;颜色&#34;)