失敗我使用下面的方法來拉動從資產文件夾中的PNG文件在我的Android應用程序:安卓:PNG透明度與BitmapFactory.decodeStream(...)和資產文件夾
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圖像有一些透明區域,但由於某些原因,當圖像顯示在我的GridV中時透視區域以黑色顯示。
爲了搶佔一些問題:不,活動,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;
}
}
但是,這給了我同樣差的結果。
任何想法,任何人?
謝謝。
我有同樣的問題:(任何更新? – fvisticot 2013-02-10 20:47:33
似乎你正在失去你的alpha通道。你可以轉儲像素對象(解碼後),並檢查你在透明區域有正確的alpha值嗎?使用Bitmap.getPixels()。 – SirKnigget 2013-05-13 20:16:19