6

Hi I would like to draw something like this

Diagonal

with a shape Is it possible ?

Pauli
  • 538
  • 8
  • 22

6 Answers6

7

I know its too late to answer this question but still posting my answer here so that others can have reference:

<?xml version="1.0" encoding="utf-8"?>
<vector xmlns:android="http://schemas.android.com/apk/res/android"
android:height="100dp"
android:width="100dp"
android:viewportHeight="100"
android:viewportWidth="100">

<path
    android:name="dark_triangle"
    android:fillColor="#FFFFFF"
    android:pathData="M 100,0 L 0,100 100,100 z" />

<path
    android:name="light_triangle"
    android:fillColor="#d0021b"
    android:pathData="M 0,0 L 100,0 0,100 z" />

</vector>
rupesh
  • 2,865
  • 4
  • 24
  • 50
5

@Pauli .You can use Image for easy practice and can try this way . Use this logic

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

<item>
    <rotate
        android:fromDegrees="50"
        android:pivotX="10%"
        android:pivotY="85%"
         >
        <shape

            android:shape="rectangle">
            <solid android:color="#000000" />

        </shape>
    </rotate>
</item>

SO Courtesy

Diagonally spilliting background of a Relative Layout

Making a triangle shape using xml definitions?

Community
  • 1
  • 1
IntelliJ Amiya
  • 74,896
  • 15
  • 165
  • 198
4
  1. Create color resources in colors.xml file

    <color name="primaryColor">#3F51B5</color>
    <color name="primaryColorDark">#303F9F</color>
    
  2. Create Java file TriangleBackgroundView.Java:

    public class TriangleBackgroundView extends View {
    Paint paint;
    Paint bgPaint;
    
    public TriangleBackgroundView(Context context) {
        super(context);
        init();
    }
    
    public TriangleBackgroundView(Context context, AttributeSet attrs) {
        super(context, attrs);
        init();
    }
    
    public TriangleBackgroundView(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        init();
    }
    
    private void init() {
        paint = new Paint();
        paint.setStrokeWidth(1);
        paint.setAntiAlias(true);
        paint.setStrokeCap(Paint.Cap.SQUARE);
        paint.setStyle(Paint.Style.FILL);
        paint.setColor(getResources().getColor(R.color.primaryColor));
    
        bgPaint= new Paint();
        bgPaint.setStyle(Paint.Style.FILL);
        bgPaint.setColor(getResources().getColor(R.color.primaryColorDark));
    }
    
    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        super.onMeasure(widthMeasureSpec, heightMeasureSpec);
    }
    
    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);
    
        int h = getMeasuredHeight();
        int w = getMeasuredWidth();
    
        canvas.drawRect(0,0,w,h,bgPaint);
    
        Path path = new Path();
        path.moveTo(0, h);
        path.lineTo(w, h);
        path.lineTo(0, 0);
        path.lineTo(0, h);
        path.close();
        canvas.drawPath(path,paint);
    }
    }
    
  3. Now add custom view in your layout:

<com.yourpackage.TriangleBackgroundView
    android:layout_height="match_parent"
    android:layout_width="match_parent"/>

<TextView android:text="@string/hello_world" android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:textStyle="bold"
    android:textSize="20dp"
    android:textColor="@android:color/white"
    android:layout_centerInParent="true"/>

You can get help from this tutorial to create shape diagonal with triangle:

Source: http://marcouberti.net/2015/06/23/create-a-custom-triangle-background-view-in-android/

Gagan Sethi
  • 397
  • 1
  • 8
  • While this link may answer the question, it is better to include the essential parts of the answer here and provide the link for reference. Link-only answers can become invalid if the linked page changes. - [From Review](/review/low-quality-posts/11054400) – Abhinav Singh Maurya Jan 28 '16 at 05:49
  • Edited the post @AbhinavSinghMaurya – Gagan Sethi Jan 28 '16 at 07:13
1

You can use something like this:

<?xml version="1.0" encoding="utf-8"?>
<layer-list xmlns:android="http://schemas.android.com/apk/res/android" >
    <item>
        <shape android:shape="rectangle" >
            <solid android:color="@color/yellow_color"/>
        </shape>
    </item>
    <item>
        <rotate
            xmlns:android="http://schemas.android.com/apk/res/android"
            android:fromDegrees="45"
            android:pivotX="85%"
            android:pivotY="135%">
            <shape>
                <solid android:color="@color/brown_color" />

            </shape>
        </rotate>
    </item>
    <item>
        <rotate
            xmlns:android="http://schemas.android.com/apk/res/android"
            android:fromDegrees="45"
            android:pivotX="-35%"
            android:pivotY="85%">
            <shape android:shape="rectangle">
                <solid android:color="@color/brown_color" />

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

The two rotated rectangles do the work of fill brown side in your image.

leobelizquierdo
  • 1,648
  • 12
  • 20
1

half_diagonal.xml

<?xml version="1.0" encoding="utf-8"?>
<vector xmlns:android="http://schemas.android.com/apk/res/android"
    android:width="200dp"
    android:height="200dp"
    android:viewportHeight="100"
    android:viewportWidth="100">

    <path
        android:name="dark_triangle"
        android:fillColor="#673AB7"
        android:pathData="M 0,0 L 100,0 0,100 z" />

</vector>
Bad Boy
  • 381
  • 2
  • 14
0

You can use a gradient tool...

http://angrytools.com/gradient/

  • 1
    Whilst this may theoretically answer the question, [it would be preferable](//meta.stackoverflow.com/q/8259) to include the essential parts of the answer here, and provide the link for reference. – Bhargav Rao Oct 18 '16 at 08:33
  • background: -moz-linear-gradient(45deg, rgba(192,192,192,1) 0%, rgba(192,192,192,1) 49%, rgba(0,0,0,1) 50%, rgba(0,0,0,1) 100%); /* ff3.6+ */ background: -webkit-gradient(linear, left bottom, right top, color-stop(0%, rgba(192,192,192,1)), color-stop(49%, rgba(192,192,192,1)), color-stop(50%, rgba(0,0,0,1)), color-stop(100%, rgba(0,0,0,1))); /* safari4+,chrome */ background: -webkit-linear-gradient(45deg, rgba(192,192,192,1) 0%, rgba(192,192,192,1) 49%, rgba(0,0,0,1) 50%, rgba(0,0,0,1) 100%); /* safari5.1+,chrome10+ */ – Dimitris DK Oct 18 '16 at 10:22