问题描述
我正在尝试在我的活动中放置背景,但图像质量不是预期的.
I'm trying to place a background in my activity, but the image quality is not the expected.
图片在蓝色条纹上方有一个简单的渐变,目前看起来像这样:
The image has a simple gradient above blue stripes, and currently looks like this:
我的活动布局:
<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 文件,它是活动的 LinearLayout 背景.但我已经尝试了到目前为止我发现的一切.启用抖动、抗锯齿、平铺、RGBA_8888 模式......你的名字.有没有人有不同的解决方案或想法我可以尝试?我将不胜感激.
Currently I have an XML file describing a BitmapDrawable, which is the actitivy's LinearLayout background. But I've tried everything I found so far. Enabled dither, antialias, tile, RGBA_8888 modes... You name it. Does anyone have a different solution or idea I could try? I'd be very grateful.
顺便说一句,我目前正在 Galaxy S II 中开发应用程序.
Btw, I'm currently developing the app in a Galaxy S II.
推荐答案
首先,确保您的原始图像看起来不错,这样您就不会只是从那里得到问题.
然后,在您的 onCreate() 方法中,执行:
First of all, make sure that your original image looks good so you're not just getting the problem from there.
Then, in your onCreate() method, do:
code1:
getWindow().getDecorView().getBackground().setDither(true);
getWindow().setFormat(PixelFormat.RGBA_8888);
已弃用:
要将您的图像显式加载为 32 位图像(RGBA-8888 配置),请在加载视图的位置添加以下内容:
And to load your image explicitly as a 32-bit image (RGBA-8888 configuration) add the following where you load your views:
代码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));
不同方法之间的比较:(这些都是生成的应用程序的屏幕截图)
Comparison between different approaches:(these are all screenshots from the resulting application)
我的源图像(左侧 64 色,右侧 24 位):
图像 1 和图像 2:
1:原始 64 色 图像 (image1) 从布局 XML 设置为背景:
2:同一张图片(image1),使用code1:
3:使用 code1 和 code2 的相同图像 (image1):
4:image2,加载了 code1 和 code2(在这种情况下,抖动并不重要,因为源和目标都使用每种颜色 8 位):
My source images (64 colors to the left, 24 bit to the right):
image1 and image2:
1: Raw 64-color image (image1) set as background from layout XML:
2: The same image (image1), using code1:
3: The same image (image1) using both code1 and code2:
4: image2, loaded with code1 and code2 (in this case the dithering is not really important as both the source and destination use 8 bits per color):
请注意图像 3 中生成的伪影如何已存在于原始图像中.
注意:如果有人知道如何将图像缩小一点,请随时编辑这篇文章......
Note: if anyone knows how to shrink the images down a bit, feel free to edit this post...
这篇关于Android中糟糕的背景图像质量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!