Android:BitmapFactory.decodeStream(...) 和资产文件夹导致 PNG 透明度失败
我使用以下方法从 Android 应用程序的资产文件夹中提取 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 item 的背景不是黑色的。事实上,无论背景颜色设置成什么,图像的透明部分总是显示为黑色。
不过,考虑一下......如果我将 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;
}
}
但这给了我同样糟糕的结果。
有什么想法吗?
谢谢。
I am using the following method to pull a PNG file from the assets folder in my Android application:
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;
}
}
I am then setting the source of an ImageView, in the item of a GridView, to that Bitmap.
Here is the layout XML in question:
<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>
And the style referred to in that XML is:
<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>
Here is the code that sets the source of the ImageView:
ImageView ivPackageIcon = (ImageView)containingView.findViewById(R.id.ivPackageIcon);
if(ivPackageIcon != null) {
Bitmap coverImage = getBitmapFromAssets(containingView.getContext(), "myimage.png");
ivPackageIcon.setImageBitmap(coverImage);
}
The PNG image has some transparent areas, but for some reason when the image is displayed in my GridView, the transparent areas come through as black.
To preempt some questions: No, the background of the Activity, ImageView, GridView, and GridView item are not black. As a matter of fact, no matter what the background colors are set to, the transparent parts of the image always come through as black.
Consider this, though...If I place the PNG image in the drawable folder and set the ImageView as follows, the transparency is perfect:
ivPackageIcon.setImageResource(R.drawable.myimage);
I'm pretty sure that I'm using the decodeStream(...) method incorrectly somehow, but I don't know what I'm doing wrong. I even modified my original method to set some options as shown here:
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;
}
}
But that gave me the same poor result.
Any ideas, anyone?
Thank you.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
相反,尝试:
Instead this, try:
如果您的图像是由应用程序资源制成的,并且放置在 res/drawable/ 下,而不是特定分辨率文件夹之一(例如 res/drawable-hdpi),则
Android 编译器正在优化 PNG,因此透明像素将变为白色(如果是 GIFF 文件则为黑色)
分辨率:
将您的 PNG 文件移动到 res/drawable-hdpi 或其他可绘制文件夹之一,应该没问题。
In case your image is made out of application resource and it is placed under res/drawable/ instead of one of the specific resolution folders (e.g. res/drawable-hdpi)
The android compiler is optimizing the PNG so the transparent pixels are made white (or black in case of GIFF files)
Resolution:
move your PNG file to res/drawable-hdpi or one of the other drawable folders, and you should be OK.