2015-04-14 63 views
0

我要讓繪製如下圖所示,製作自定義可繪製形狀Android中

enter image description here

我能夠使矩形和Triange形狀在兩個不同的XML文件。但我想聯合他們來製作像這樣的drawable。

+0

你需要一個[(http://developer.android.com/guide/topics/resources/drawable-resource.html#LayerList) – Blackbelt

+0

如果您有它的形象,爲什麼使用drawable? – Harry

+1

如果我正確理解這樣的drawable的需要,你可以嘗試9-patch image ..並且只是使矩形區域可擴展。 –

回答

1

使用layer-list,使這個自定形狀drawable

/res/drawable/custom_shape.xml:

<?xml version="1.0" encoding="utf-8"?> 
<layer-list xmlns:android="http://schemas.android.com/apk/res/android"> 

    <!-- Transparent Rectangle --> 
    <item> 
     <shape android:shape="rectangle"> 
      <size 
       android:width="300dp" 
       android:height="60dp" /> 
      <solid android:color="@android:color/transparent" /> 
     </shape> 
    </item> 

    <!-- Colored Rectangle --> 
    <item 
     android:bottom="20dp"> 
     <shape android:shape="rectangle"> 
      <size 
       android:width="300dp" 
       android:height="60dp" /> 
      <solid android:color="#9A8585" /> 
     </shape> 
    </item> 

    <!-- Bottom Triangle --> 
    <item 
     android:left="80dp" 
     android:right="120dp" 
     android:top="0dp" 
     android:bottom="30dp"> 
     <rotate android:fromDegrees="45"> 
      <shape android:shape="rectangle"> 
       <solid android:color="#9A8585" /> 
      </shape> 
     </rotate> 
    </item> 

    <!-- Top Border --> 
    <item 
     android:bottom="75dp"> 
     <shape android:shape="rectangle"> 
      <solid android:color="#EFC1B3" /> 
     </shape> 
    </item> 

</layer-list> 

USE:

<LinearLayout 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:background="@drawable/custom_shape"/> 

OUTPUT:

enter image description here

希望這將有助於〜

0

我希望你需要一個Tabular導航器或ViewPager。我的建議是,

1)使用矩形作爲您的背景所有情況。 2)創建相同的可繪製的三角形,但使用背景顏色(白色或透明)作爲未選定的項目。

3)根據選擇狀態

0

使用<layer-list/>對於所有項目

4)的三角形視圖的手動setVisibility創建具有三角形背景的圖。

這裏是一個例子。

在項目目錄resDrawable文件夾, 使xml文件並將它命名爲layerlist.xml並粘貼下面的代碼爲您的要求。 您還可以在示例中的<item/>標記中添加可繪製的形狀,而不是繪製。並使用此xml作爲背景ImageView

<?xml version="1.0" encoding="utf-8"?> 
<layer-list xmlns:android="http://schemas.android.com/apk/res/android"> 
    <item> 
     <bitmap android:src="@drawable/android_red" 
     android:gravity="center" /> 
    </item> 
    <item android:top="10dp" android:left="10dp"> 
     <bitmap android:src="@drawable/android_green" 
     android:gravity="center" /> 
    </item> 
    <item android:top="20dp" android:left="20dp"> 
     <bitmap android:src="@drawable/android_blue" 
     android:gravity="center" /> 
    </item> 
</layer-list> 

這裏是利用<layer-list/>的結果。

enter image description here

我希望它可以幫助你......