Draw diagonal xml android with triangles

Hi i would like to do something like this

Diagonal

with form Is this possible?

+6
source share
4 answers
  • Creating color resources in colors.xml

    <color name="primaryColor">#3F51B5</color> <color name="primaryColorDark">#303F9F</color> 
  • Create the 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); } } 
  • Now add a custom view to 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 in this tutorial to create a diagonal shape with a triangle:

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

+3
source

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> 

Two rotating rectangles do the job of filling the brown side in your image.

+1
source

@Pauli . You can use Image to simplify your practice and you can try this. 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

Relatively diagonal spray pattern background

Creating a triangle shape using xml definitions?

0
source

You can use the gradient tool ...

http://angrytools.com/gradient/

0
source

All Articles