2017-08-24 89 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