Android:使用线性渐变作为背景看起来是带状的

Android:使用线性渐变作为背景看起来是带状的,android,gradient,shape,Android,Gradient,Shape,我正在尝试对我的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"

我正在尝试对我的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>
它可以工作,但在模拟器和真实设备上看起来也非常“带状”


有没有办法减少这种“行为”?

您只需在可绘制对象上启用抖动。

您只需在可绘制对象上启用抖动。

正如Romain Guy所建议的:

listView.getBackground().setDither(true);
解决我的问题

如果这还不够,特别是对于AMOLED和/或hdpi设备,请尝试以下方法:

@Override
public void onAttachedToWindow() {
    super.onAttachedToWindow();
    Window window = getWindow();
    window.setFormat(PixelFormat.RGBA_8888);
}

正如Romain Guy所建议的:

listView.getBackground().setDither(true);
解决我的问题

如果这还不够,特别是对于AMOLED和/或hdpi设备,请尝试以下方法:

@Override
public void onAttachedToWindow() {
    super.onAttachedToWindow();
    Window window = getWindow();
    window.setFormat(PixelFormat.RGBA_8888);
}

将此内容放在您的活动中:

@Override
public void onAttachedToWindow() {
    super.onAttachedToWindow();
    Window window = getWindow();
    window.setFormat(PixelFormat.RGBA_8888);
}

将此内容放在您的活动中:

@Override
public void onAttachedToWindow() {
    super.onAttachedToWindow();
    Window window = getWindow();
    window.setFormat(PixelFormat.RGBA_8888);
}

对我来说,HTC Desire就是这样工作的

window.getDecorView().getBackground().setDither(true);

对我来说,HTC Desire就是这样工作的

window.getDecorView().getBackground().setDither(true);

对我来说,当我在渐变上设置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>

但首先,我尝试使用一个带有“噪波”的图层列表来删除条带,这很有帮助,但仍然有一些条带

<?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>

我创建并使用的“noise_repeater”绘图设备


对我来说,当我在渐变上设置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>

但首先,我尝试使用一个带有“噪波”的图层列表来删除条带,这很有帮助,但仍然有一些条带

<?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>

我创建并使用的“noise_repeater”绘图设备


唯一对我有效的方法是在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>


我从另一个SO问题中得到了尝试的想法:

唯一对我有效的方法是在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>


我从另一个SO问题中得到了这个想法:

如果抖动和RGBA_888设置在某些手机上没有帮助,解决方案是将元素
layerType
设置为
软件
,以禁用硬件加速

android:layerType="software"

这解决了我在三星S5手机上的问题。

如果抖动和RGBA_888设置在某些手机上没有帮助,解决方案是将element
layerType
设置为
软件,以禁用硬件加速

android:layerType="software"

这解决了我在三星S5手机上的问题。

是的,它可以工作!非常感谢你。顺便说一句,我试图在可绘制xml定义中使用此属性,但随着我对文档的深入了解,此属性仅支持选择器元素。不幸的是,这在Android版本>1.5上不起作用。我用安卓1.6和安卓1.5在真实设备上进行了测试。在我的可绘制渐变中添加抖动不起作用。两台设备均为320 x 480(180 ppi)。有什么建议吗?Tnx
setDither()
在API级别23中被弃用。此属性现在被忽略。是的,它可以工作!非常感谢你。顺便说一句,我试图在可绘制xml定义中使用此属性,但随着我对文档的深入了解,此属性仅支持选择器元素。不幸的是,这在Android版本>1.5上不起作用。我用安卓1.6和安卓1.5在真实设备上进行了测试。在我的可绘制渐变中添加抖动不起作用。两台设备均为320 x 480(180 ppi)。有什么建议吗?Tnx
setDither()
在API级别23中被弃用。此属性现在被忽略。这在android 1.6及以上版本上不起作用。见我的另一半comment@Artem在我的例子中,在Android上>=1.6
listView.getBackground().setDither(true)
不起作用,我必须重写
onAttachedToWindow
方法。setDither()现在不受欢迎。这在android 1.6及更高版本上不起作用。见我的另一半comment@Artem在我的例子中,在Android上>=1.6
listView.getBackground().setDither(true)不起作用,我必须重写
onAttachedToWindow
方法。setDither()现在不推荐使用调整更新:添加getWindow().setFormat(PixelFormat.rgba8888);getWindow().addFlags(WindowManager.LayoutParams.FLAG_抖动);在onCreate方法中,似乎同样适用于带amoled屏幕的hdpi(N1/Desire)
@Francesco
很棒!它在我的Galaxy S上帮助了Android 2.2。请将您的有用评论转换为答案,以便人们投票支持。只需更新:添加getWindow().setFormat(PixelFormat.rgba8888);getWindow().addFlags(WindowManager.LayoutParams.FLAG_抖动);在onCreate方法中,似乎同样适用于带amoled屏幕的hdpi(N1/Desire)
@Francesco
很棒!它在我的Galaxy S上帮助了Android 2.2。请将有用的注释转换为答案,以便人们可以投票支持。
setDither()
在API级别23中被弃用。此属性现在被忽略。
setDither()
在API级别23中被弃用。此属性现在被忽略。