用三角形绘制android xml形状对角线

时间:2015-10-01 09:02:10

标签: android xml shape diagonal

您好我想画这样的东西

Diagonal

有形状可能吗?

6 个答案:

答案 0 :(得分:4)

@Pauli 。您可以使用Image进行简单练习,可以尝试这种方式。使用这个逻辑

     <?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礼貌

  

Diagonally spilliting background of a Relative Layout

     

Making a triangle shape using xml definitions?

答案 1 :(得分:4)

  1. 在colors.xml文件中创建颜色资源

    <color name="primaryColor">#3F51B5</color>
    <color name="primaryColorDark">#303F9F</color>
    
  2. 创建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. 现在在您的布局中添加自定义视图:

  4. <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"/>
    

    您可以从本教程中获得帮助,以创建具有三角形的形状对角线:

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

答案 2 :(得分:1)

您可以使用以下内容:

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

两个旋转的矩形在图像中完成填充棕色边的工作。

答案 3 :(得分:1)

我知道回答这个问题为时已晚,但仍然在这里发布我的答案,以便其他人可以参考:

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

答案 4 :(得分: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>

答案 5 :(得分:-1)

您可以使用渐变工具......

http://angrytools.com/gradient/