2010-05-28 341 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