绘制带有三角形的android xml形状对角线

时间:2023-02-09 23:55:24

Hi I would like to draw something like this

你好,我想画一个这样的东西

绘制带有三角形的android xml形状对角线

with a shape Is it possible ?

有形状的,可能吗?

4 个解决方案

#1


4  

  1. Create color resources in colors.xml file

    创建颜色资源。xml文件

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

    创建Java文件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/

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

#2


1  

@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?

使用xml定义创建三角形?

#3


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.

这两个旋转的矩形在你的图像中做填充棕色的工作。

#4


0  

You can use a gradient tool...

你可以使用渐变工具…

http://angrytools.com/gradient/

http://angrytools.com/gradient/

#1


4  

  1. Create color resources in colors.xml file

    创建颜色资源。xml文件

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

    创建Java文件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/

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

#2


1  

@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?

使用xml定义创建三角形?

#3


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.

这两个旋转的矩形在你的图像中做填充棕色的工作。

#4


0  

You can use a gradient tool...

你可以使用渐变工具…

http://angrytools.com/gradient/

http://angrytools.com/gradient/