2017-08-24 103 views
1

我創造了這個繪製:如何在我的視圖的左上角繪製一個三角形?

<?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="10" 
      android:pivotX="100%" 
      android:pivotY="-0%" > 
      <shape 
       android:shape="rectangle" > 
       <solid 
        android:color="@color/color_primary" /> 
      </shape> 

     </rotate> 
    </item> 
</layer-list> 

在預覽它看起來像這樣:

enter image description here

而且我已經把它變成這個View

 <View 
     android:id="@+id/my_view" 
     android:layout_width="32dp" 
     android:layout_height="32dp" 
     android:background="@drawable/triangle" 
     app:layout_constraintLeft_toLeftOf="parent" 
     app:layout_constraintTop_toTopOf="parent" 
     /> 

View在預覽(和電話)上看起來像這樣:

enter image description here

爲什麼不View只是顯示在像三角形預覽角落的三角形?另外,我應該提到,我希望它能填滿View廣場的一半,基本上從右上角開始到左下角。

謝謝。

編輯:我得到了一個建議使用:

<?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="-135" 
      android:pivotX="90%" 
      android:pivotY="-45%" > 
      <shape 
       android:shape="rectangle" > 
       <solid 
        android:color="@color/color_primary" /> 
      </shape> 

     </rotate> 
    </item> 
</layer-list> 

這確實能產生三角形的角落,但它不填充半個View廣場。這就是它的作用: enter image description here

回答

2

我會用<vector>繪製,而不是<layer-list>的解決這個問題。

<vector 
    xmlns:android="http://schemas.android.com/apk/res/android" 
    android:width="24dp" 
    android:height="24dp" 
    android:viewportWidth="24.0" 
    android:viewportHeight="24.0"> 

    <path 
     android:fillColor="@color/color_primary" 
     android:pathData="M0 24v-24h24z"/> 

</vector> 

enter image description here

+0

非常感謝,謝謝。 – casolorz

+0

如何製作帶矢量的圓角三角形?你能幫忙嗎? –

0

將您的XML更改爲以下內容,它將按照您的要求工作。

triangle.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="-135" 
      android:pivotX="90%" 
      android:pivotY="-45%" > 
      <shape 
       android:shape="rectangle" > 
       <solid 
        android:color="@color/color_primary" /> 
      </shape> 

     </rotate> 
    </item> 
</layer-list> 

Preview
Device screenshot

+0

雖然是把它放在角落裏,它不填充半個'View'廣場這正是我想要的。我正在爲我的帖子添加一張圖片。 – casolorz