9

我正在使用以下方法从我的 Android 应用程序的 assets 文件夹中提取 PNG 文件:

public static Bitmap getBitmapFromAssets(Context context, String fileName) {
        try {
            AssetManager assetManager = context.getAssets();
            InputStream inputStream = assetManager.open(fileName);
            Bitmap bitmap = BitmapFactory.decodeStream(inputStream);
            return bitmap;
        }
        catch(Exception ex) {
            ex.printStackTrace();
            return null;
        }
    }

然后,我将 GridView 项目中的 ImageView 的源设置为该位图。

这是有问题的布局 XML:

<LinearLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/containingLayout"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:orientation="horizontal"
    android:gravity="center_horizontal"
    android:background="@android:color/transparent"
    android:padding="0dp"
    android:layout_margin="0dp"
>
    <ImageView
        android:id="@+id/ivPackageIcon"
        style="@style/smLargeGridItemPackageIconStyle"
    />
</LinearLayout>

该 XML 中引用的样式是:

<style name="smLargeGridItemPackageIconStyle">
        <item name="android:scaleType">fitXY</item>
        <item name="android:layout_width">100dp</item>
        <item name="android:layout_height">142dp</item>
        <item name="android:layout_margin">5dp</item>
        <item name="android:background">@android:color/transparent</item>
    </style>

下面是设置 ImageView 来源的代码:

ImageView ivPackageIcon = (ImageView)containingView.findViewById(R.id.ivPackageIcon);
        if(ivPackageIcon != null) {
            Bitmap coverImage = getBitmapFromAssets(containingView.getContext(), "myimage.png");
            ivPackageIcon.setImageBitmap(coverImage);
        }

PNG 图像有一些透明区域,但由于某种原因,当图像显示在我的 GridView 中时,透明区域会变成黑色。

抢先问一些问题: 不,Activity、ImageView、GridView 和 GridView 项的背景不是黑色的。事实上,无论背景颜色设置为什么,图像的透明部分总是以黑色显示。

不过考虑一下……如果我将 PNG 图像放在可绘制文件夹中并按如下方式设置 ImageView,则透明度是完美的:

ivPackageIcon.setImageResource(R.drawable.myimage);

我很确定我以某种方式错误地使用了 decodeStream(...) 方法,但我不知道我做错了什么。我什至修改了我原来的方法来设置一些选项,如下所示:

public static Bitmap getBitmapFromAssets(Context context, String fileName) {
        try {
            AssetManager assetManager = context.getAssets();
            InputStream inputStream = assetManager.open(fileName);

            BitmapFactory.Options options = new BitmapFactory.Options();
            options.inDither = true;
            options.inPreferredConfig = Config.ARGB_8888;

            Bitmap bitmap = BitmapFactory.decodeStream(inputStream, null, options);
            return bitmap;
        }
        catch(Exception ex) {
            ex.printStackTrace();
            return null;
        }
    }

但这给了我同样糟糕的结果。

任何想法,任何人?

谢谢你。

4

2 回答 2

0

相反,请尝试:

ImageView img=new ImageView(this);
        Drawable d = Drawable.createFromStream(getAssets().open(path_in_assets), null);
        img.setImageDrawable(d);
于 2013-06-04T18:57:07.320 回答
0

如果您的图像是由应用程序资源制成的,并且它被放置在 res/drawable/ 下,而不是特定分辨率文件夹之一(例如 res/drawable-hdpi)

android 编译器正在优化 PNG,使透明像素变为白色(如果是 GIFF ​​文件,则变为黑色)

解决方案:将您的 PNG 文件移动到 res/drawable-hdpi 或其他可绘制文件夹之一,您应该没问题。

于 2015-03-31T12:56:43.007 回答