简体   繁体   中英

Triangle shape in android

I have referred many StackOverflow questions regarding drawing triangle shapes.

But, I have a special case here. Following is a combined shape I want to create. Rectangle, I can manage. But, I am facing issues with the triangle. I am getting confused with the angles.

Angles of the triangle are mentioned.

I have the following code, already :

<?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="0"
            android:pivotX="270%"
            android:pivotY="180%" >

            <shape
                android:shape="rectangle"  >
                <stroke android:color="@android:color/transparent" android:width="10dp"/>
                <solid
                    android:color="#000000"  />
            </shape>
        </rotate>
    </item>
</layer-list>

在此输入图像描述

I want to know the following values that satisfies my need :

android:fromDegrees=?
android:toDegrees=?
android:pivotX=?
android:pivotY=?

Please, help me with this.

Since I am guessing the point you have stuck has nothing to do with android (meaning you know how to create a random triangle) you need help to find the 3 points. Assuming you know the point in the 50 degrees ( x1,y1 ),

  1. The point in the 90 has coords ( x1+d1,y1 ) where d1 is distance of the edge connecting those 2 points.
  2. The point in the bottom has coords ( x1+d1,y2 ), where y2 = acos(50)*d1 . Of course atan2 should take degrees to work or you must convert it to rads.

This is the general idea. If it happens to know another point you act in the same way. Hope that helped.

i can draw triangle shape using XML how you can use as per your requirement that i don't know

triangle .xml

<item>
    <rotate
        android:fromDegrees="45"
        android:pivotX="-40%"
        android:pivotY="87%"
        android:toDegrees="45" >
        <shape android:shape="rectangle" >
            <stroke
                android:width="10dp"
                android:color="#00000000" />

            <solid android:color="#00ACED" />  //your color
        </shape>
    </rotate>
</item>

三角形

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM