Android:使用线性渐变作为背景看起来带状
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/2928101/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Android: Using linear gradient as background looks banded
提问by Francesco Laurita
I'm trying to apply a linear gradient to my ListView. This is the content of my drawable xml:
我正在尝试对我的 ListView 应用线性渐变。这是我的可绘制 xml 的内容:
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android">
<gradient
android:startColor="#3A3C39"
android:endColor="#181818"
android:angle="270"
/>
<corners android:radius="0dp" />
</shape>
So I apply it to my ListView with:
所以我将它应用到我的 ListView 中:
android:background="@drawable/shape_background_grey"
It works but it looks very "banded" on emulator and on a real device too.
它可以工作,但它在模拟器和真实设备上看起来也非常“带状”。
Is there any way to reduce this "behaviour"?
有什么办法可以减少这种“行为”?
回答by Francesco Laurita
As Romain Guy suggests:
正如罗曼盖伊建议的那样:
listView.getBackground().setDither(true);
solves my problem
解决了我的问题
If this is not enough especially for AMOLED and/or hdpi devices try this:
如果这还不够,特别是对于 AMOLED 和/或 hdpi 设备,请尝试以下操作:
@Override
public void onAttachedToWindow() {
super.onAttachedToWindow();
Window window = getWindow();
window.setFormat(PixelFormat.RGBA_8888);
}
回答by Romain Guy
You can simply enable dithering on your Drawable object.
您可以简单地在 Drawable 对象上启用抖动。
回答by Mi?a Pei?
Put this in your Activity:
把它放在你的活动中:
@Override
public void onAttachedToWindow() {
super.onAttachedToWindow();
Window window = getWindow();
window.setFormat(PixelFormat.RGBA_8888);
}
回答by Sebastian
For me on HTC Desire works like this
对我来说,HTC Desire 是这样工作的
window.getDecorView().getBackground().setDither(true);
回答by TouchBoarder
For me the banding disappeared when I set the android:useLevel="true" on the gradient: gradient_dark.xml
对我来说,当我在渐变上设置 android:useLevel="true" 时,条带消失了:gradient_dark.xml
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="rectangle">
<gradient android:startColor="#464646"
android:endColor="#323232"
android:angle="270"
android:type="linear"
android:useLevel="true"
/>
</shape>
But first I tried using a Layer-List with a "noise" drawable to remove the banding, it helps but there is still some banding.
但首先我尝试使用带有“噪音”可绘制的图层列表来删除条带,它有帮助,但仍然有一些条带。
<?xml version="1.0" encoding="utf-8"?>
<layer-list xmlns:android="http://schemas.android.com/apk/res/android" >
<item android:drawable="@drawable/gradient_dark"/>
<item>
<bitmap
android:gravity="center"
android:src="@drawable/noise_repeater"
android:tileMode="repeat" />
</item>
</layer-list>
The "noise_repeater" drawable i created and used
我创建和使用的“noise_repeater”drawable
回答by peter.bartos
If the dither and RGBA_888 setting don't help on some phones, the solution can be to set the element layerType
to software
, to disable the hardware acceleration
如果抖动和 RGBA_888 设置在某些手机上没有帮助,解决方案可以是将元素设置layerType
为software
,禁用硬件加速
android:layerType="software"
This fixed my problem on a Samsung S5 phone.
这解决了我在三星 S5 手机上的问题。
回答by Greg
The only thing that worked for me was to set a slightly-transparent alpha channel on both the startColor and endColor.
唯一对我有用的是在 startColor 和 endColor 上设置一个稍微透明的 alpha 通道。
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android">
<gradient
android:startColor="#FE3A3C39"
android:endColor="#FE181818"
android:angle="270"
/>
</shape>
I got the idea to try this from this other SO question: android:dither=“true” does not dither, what's wrong?
我从另一个 SO 问题中得到了尝试这个的想法:android:dither=“true”不抖动,有什么问题?