我正在尝试在我的 Activity 中放置背景,但图像质量不是预期的。
图片在蓝色条纹上方有一个简单的渐变,目前看起来像这样:
我的 Activity 布局:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical"
android:background="@drawable/background_register"
android:drawingCacheQuality="high" >
</LinearLayout>
背景描述符(drawable/background_register
):
<bitmap xmlns:android="http://schemas.android.com/apk/res/android"
android:antialias="true"
android:dither="true"
android:filter="true"
android:tileMode="repeat"
android:gravity="center"
android:src="@drawable/background_blue_565" />
目前我有一个描述 BitmapDrawable 的 XML 文件,它是 Activity 的 LinearLayout 背景。但我已经尝试了到目前为止我发现的一切。启用抖动、抗锯齿、平铺、RGBA_8888 模式......你的名字。有没有人有不同的解决方案或想法我可以尝试?我将不胜感激。
顺便说一句,我目前正在 Galaxy S II 中开发应用程序。
最佳答案
首先,确保您的原始图像看起来不错,这样您就不会从那里得到问题。
然后,在您的 onCreate() 方法中,执行:
code1:
getWindow().getDecorView().getBackground().setDither(true);
getWindow().setFormat(PixelFormat.RGBA_8888);
已弃用:
getWindow().addFlags(WindowManager.LayoutParams.FLAG_DITHER);
要将您的图像显式加载为 32 位图像(RGBA-8888 配置),请在加载 View 的位置添加以下内容:
代码2:
BitmapFactory.Options options = new BitmapFactory.Options();
options.inPreferredConfig = Bitmap.Config.ARGB_8888;
Bitmap gradient = BitmapFactory.decodeResource(getResources(), R.drawable.gradient, options);
findViewById(R.id.main).setBackgroundDrawable(new BitmapDrawable(gradient));
不同方法的比较:
(这些都是生成的应用程序的屏幕截图)
我的源图像(左侧 64 色,右侧 24 位):
image1 和 image2:
1:原始 64 色 图像 (image1) 从布局 XML 设置为背景:
2:同一张图片(image1),使用code1:
3:使用 code1 和 code2 的相同图像(image1):
4:image2,加载了 code1 和 code2(在这种情况下,抖动并不重要,因为源和目标都使用每种颜色 8 位):
请注 Intent 像 3 中生成的伪影如何已存在于原始图像中。
注意:如果有人知道如何将图像缩小一点,请随时编辑这篇文章......
关于android - Android中糟糕的背景图像质量,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8417608/