2010-05-28 346 views
80

我想将线性渐变应用于我的ListView。 这是我绘制的XML内容:Android:使用线性渐变作为背景看起来带状

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

所以我把它应用到我的ListView有:

android:background="@drawable/shape_background_grey" 

它的工作原理,但它看起来很“杀鸡取卵”的模拟器和实际设备上也。

有什么办法可以减少这种“行为”吗?

+3

你可以发布截图吗? – CommonsWare 2010-05-28 12:53:24

+18

只是一个更新:加入 getWindow()。setFormat(PixelFormat.RGBA_8888); \t \t getWindow()。addFlags(WindowManager.LayoutParams.FLAG_DITHER); 在onCreate方法似乎也为hdpi与amoled屏幕(N1 /欲望) – 2010-06-08 13:40:31

+2

' @ Francesco'太棒了!太棒了!它帮助我的** Galaxy S ** ** Android 2.2 **。请将您的有用评论转换为答案,以便人们可以投票给它。 – 2011-06-10 22:49:31

回答

79

由于罗曼盖伊提示:

listView.getBackground().setDither(true); 

解决我的问题

如果这还不够特别是对于AMOLED和/或HDPI设备试试这个:

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

这不适用于Android 1.6及以上版本。看到我的其他评论 – 2010-05-31 09:53:39

+8

你可以在XML中做到这一点? – 2010-09-02 11:17:50

+7

“无效”或“现在有效”?那里差别很大。 – 2011-06-16 20:07:37

38

您可以简单地在您的Drawable对象上启用抖动。

+1

是的,它的作品! 非常感谢。 顺便说一句我试图使用这个属性到可​​绘制的xml定义中,但是当我更好地阅读文档时,只有 选择器元素支持此属性。 – 2010-05-28 19:47:43

+0

不幸的是,这不适用于Android版本> 1.5。我使用Android 1.6和Android 1.5在真实设备上进行测试。添加抖动到我的绘制渐变不起作用。这些设备都是320 x 480(180 ppi)。任何建议? Tnx – 2010-05-31 09:52:54

+1

setDither()'在API级别23中被弃用。此属性现在被忽略。 – 2017-03-17 00:58:16

8

在将这个你活动:

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

对于我的HTC Desire的工作原理是这样

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

'setDither()'在API级别23中被弃用。现在忽略此属性。 – 2017-03-17 00:58:46

1

对我的条纹的消失时,我设置了android:在梯度useLevel = “真”: 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”我绘制的创建和使用

enter image description here

0

只工作对我来说是一个集略微透明的alpha通道上都startColor和ENDCOLOR的事情。

<?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问题试试这个:android:dither=“true” does not dither, what's wrong?

1

如果抖动和RGBA_888设置不会在某些手机上的帮助,该解决方案可以为元素设置layerTypesoftware ,禁用硬件加速

android:layerType="software" 

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

+0

解决了我的问题 - 谢谢! – 2015-11-12 17:50:52