2017-07-03 20 views
4

我試圖不使用圖像視圖(因爲後來,我想生成許多具有相同的形狀,但不同的顏色,所以我不會需要不斷添加圖像視圖,而只需在我的代碼中添加顏色。 enter image description here如何繪製帶狀Android Studio?

我如何能在Android Studio中創建這些圖像?(我是過目油漆,onDraw有,帆布等......但看起來那麼難我)

+2

取一個白色三角形圖像的任何顏色塗染它,保持它在左,更改背景您需要的其餘視圖 – Bills

+3

有很多方法可以做到這一點,但最好的方法可能是使用矢量繪製 –

+0

檢查我的答案 –

回答

2

如果你想從XML使用,試試這個辦法,將工作

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


    <item> 
     <shape android:shape="rectangle"> 
      <size 
       android:width="100dp" 
       android:height="40dp" /> 
      <solid android:color="#5EB888" /> 
      <corners android:radius="0dp"/> 
     </shape> 
    </item> 


    <item 
     android:top="-26dp" 
     android:bottom="31dp" 
     android:left="-90dp" 
     android:right="75dp"> 
     <rotate 
      android:fromDegrees="45"> 
      <shape android:shape="rectangle"> 
       <solid android:color="#ffffff" /> 
      </shape> 
     </rotate> 
    </item> 



</layer-list> 

OUTPUT

enter image description here

+0

是否可以去除那種白色? –

+0

第二個矩形給出了白色,我試圖用透明...但是那個時候它看起來像矩形 –

+0

,因爲我們總會有其他背景... –

1

你可以使用任何載體創建矢量圖像如Adobe Illustrator程序或使用轉換工具(如vectormagic)轉換它,或找到已有的,然後將其導入到android studio。

enter image description here

它將被導入爲XML文件,你可以改變顏色,只要你想

1

創建一個可繪製的文件名爲left_arrow.xml

<?xml version="1.0" encoding="utf-8"?> 
<layer-list xmlns:android="http://schemas.android.com/apk/res/android" > 
<item> 
    <rotate 
     android:fromDegrees="45" 
     android:toDegrees="90" 
     android:pivotX="0%" 
     android:pivotY="1%" > 
     <shape android:shape="rectangle" > 
      <stroke 
       android:width="10dp" 
       android:color="#00000000" /> 

      <solid android:color="#ffffff" /> 
     </shape> 
    </rotate> 
</item> 

現在,創建視圖佈局創建這種類型的條紋或圖像這樣的,這裏是下面的代碼: -

<?xml version="1.0" encoding="utf-8"?> 
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
android:layout_width="match_parent" 
android:layout_height="match_parent" 
android:orientation="vertical"> 

<LinearLayout 
    android:id="@+id/llMain" 
    android:layout_width="match_parent" 
    android:layout_height="0dp" 
    android:layout_weight="0.1" 
    android:background="@color/black" 
    android:orientation="horizontal"> 

    <ImageView 
     android:layout_width="0dp" 
     android:layout_height="match_parent" 
     android:layout_weight="0.1" 
     android:background="@drawable/left_arrow" 
     android:contentDescription="@string/app_name" /> 

    <ImageView 
     android:layout_width="0dp" 
     android:layout_height="wrap_content" 
     android:layout_weight="0.9" 
     android:contentDescription="@string/app_name" /> 

</LinearLayout> 

<LinearLayout 
    android:layout_width="match_parent" 
    android:layout_height="0dp" 
    android:layout_weight="0.9" /> 

以上佈局創建所需的箭頭條紋作爲佈局。

現在將此佈局添加到所需的佈局中,您不想顯示此條紋。例如,

<?xml version="1.0" encoding="utf-8"?> 
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
android:orientation="vertical" android:layout_width="match_parent" 
android:layout_height="match_parent"> 

<include 
android:id="@+id/llMain" 
layout="@layout/xtra"/> 

</LinearLayout> 

現在,您可以通過使用 「llMain」 也使用OnClickListener。 希望它會有所幫助。

0

你可以使用你已經擁有的白色PNG圖像和使用上的圖像視圖類的setColorFilter方法與你的願望