2014-07-12 36 views
0

我用半透明邊框創建了一個相當簡單的shaperawable,並將其用作兩個相鄰視圖的背景。Android ShapeDrawable筆畫模糊

要麼srtoke顏色是部分透明的,我期待一個堅實的筆畫(如右圖),但我得到的是一個模糊的筆畫(左圖)。

我在做什麼錯了?

actual vs expected

(圖像從ADT預覽拍攝,在模擬器中效果更明顯)

這是ShapeDrawable(theme_base_bg_framed.xml):

<?xml version="1.0" encoding="utf-8"?> 
<shape xmlns:android="http://schemas.android.com/apk/res/android" 
    android:shape="rectangle"> 
    <solid 
     android:color="@color/base_frame_solid_color" 
     /> 
    <stroke 
     android:width="@dimen/frame_border_size" 
     android:color="@color/base_frame_border_color" 
     /> 
    <padding 
     android:left="@dimen/frame_padding_size" 
     android:top="@dimen/frame_padding_size" 
     android:right="@dimen/frame_padding_size" 
     android:bottom="@dimen/frame_padding_size" 
     /> 
</shape> 

它使用這些顏色和尺寸定義:

<color name="base_frame_solid_color">#20ffffff</color> 
<color name="base_frame_border_color">#40ffffff</color> 

<dimen name="frame_border_size">2dp</dimen> 
<dimen name="frame_padding_size">2dp</dimen> 

個兩個可繪被分配給View的背景

<style name="ViewWithBorder"> 
    <item name="android:background">@drawable/theme_base_bg_framed</item> 
</style> 

編輯:

在ShapeDrawable使用的顏色被alphaed的一個原因。背景中的視圖將包含其他視圖和/或圖像。這是我得到的(左)和我期望得到的(右)的更好例子。

enter image description here

+0

刪除填充,然後再試一次 – Gunaseelan

+0

@Gunaseelan:相同的結果 – Ohmnibus

+0

@ FrankN.Stein:我的意思是「犀利」中風至少爲暗黑色佈局,它的工作原理半透明色 – Ohmnibus

回答

1

我用這個值來得到你想要的結果: colors.xml:

<color name="base_frame_solid_color">#20000000</color> 
<color name="base_frame_border_color">#40ffffff</color> 

形狀:

<shape xmlns:android="http://schemas.android.com/apk/res/android" 
    android:shape="rectangle"> 
    <solid 
     android:color="@color/base_frame_solid_color" 
     /> 
    <stroke 
     android:width="@dimen/frame_border_size" 
     android:color="@color/base_frame_border_color" 
     /> 
</shape> 

希望這有助於!

+0

。其他背景的 – Mike

+0

只是使純色完全透明 - #00000000或純色。 – Mike

+0

我很抱歉我的問題不清楚,我編輯它以添加更好的示例。 填充物不會影響衝程清晰度。 固化固體顏色爲#20000000確實使行程尖銳,但它不是我要找的顏色 – Ohmnibus

0

好的,我發現發生了什麼,以及如何解決它。

似乎Android在用邊框填充ShapeDrawable時,並沒有將其填充到它的全尺寸,而只是到了中間。所以,在2dp的中風下,它周圍留下1dp的空間。只有使用帶邊框的邊框才能注意到這一點,就像我正在做的那樣,因爲通常邊框會覆蓋它。

芹苴解決這個問題,我使用了含有兩個ShapeDrawables,一個是純色LayerList繪製(無邊框),一個用於中風(無純色):

<?xml version="1.0" encoding="utf-8"?> 
<layer-list xmlns:android="http://schemas.android.com/apk/res/android"> 
    <item> 
     <shape 
      android:shape="rectangle"> 
      <solid 
       android:color="@color/base_frame_solid_color" 
       /> 
     </shape> 
    </item> 
    <item> 
     <shape 
      android:shape="rectangle"> 
      <stroke 
       android:width="@dimen/frame_border_size" 
       android:color="@color/base_frame_border_color" 
       /> 
      <padding 
       android:left="@dimen/frame_padding_size" 
       android:top="@dimen/frame_padding_size" 
       android:right="@dimen/frame_padding_size" 
       android:bottom="@dimen/frame_padding_size" 
       /> 
     </shape> 
    </item> 
</layer-list> 

它的工作原理,但作爲邊界疊加到「堅實」的背景,它的顏色需要一些調整。