如何在android中构建一个梯形?

时间:2014-09-10 14:27:35

标签: android android-shape

如何创建如图像下方的梯形形状?

enter image description here

我不想使用图片或9.png。

3 个答案:

答案 0 :(得分:15)

试试这个:

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

    <path
        android:strokeWidth="1.5"
        android:strokeMiterLimit="10"
        android:pathData="M 27.046 96.615 L 16.416 150.396 L 271.534 150.396 L 186.495 22.836 L 37.676 22.836 L 27.046 86.615 Z" />

    <path
        android:fillColor="#00ff00"
        android:strokeWidth="1.5"
        android:strokeMiterLimit="10"
        android:pathData="M 16.046 20.615 L 13.416 150.396 L 271.534 150.396 L 186.495 22.836 L 37.676 22.836 L 16.046 23.615 Z " />
</vector>

enter image description here

答案 1 :(得分:4)

此类是View,用于定义和绘制梯形ShapeDrawable。因此,作为Drawable的梯形也可用于背景中。

package com.stackoverflow.questions.q25768037;

import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Path;
import android.graphics.drawable.ShapeDrawable;
import android.graphics.drawable.shapes.PathShape;
import android.util.AttributeSet;
import android.view.View;

public class TrapezoidView extends View {

    private ShapeDrawable mTrapezoid;

    public TrapezoidView(Context context, AttributeSet attrs) {
        super(context, attrs);

        Path path = new Path();
        path.moveTo(0.0f, 0.0f);
        path.lineTo(100.0f, 0.0f);
        path.lineTo(200.0f, 100.0f);
        path.lineTo(0.0f, 100.0f);
        path.lineTo(0.0f, 0.0f);

        mTrapezoid = new ShapeDrawable(new PathShape(path, 200.0f, 100.0f));
        mTrapezoid.getPaint().setStyle(Paint.Style.FILL_AND_STROKE);
        mTrapezoid.getPaint().setStrokeWidth(1.0f);
        mTrapezoid.getPaint().setColor(Color.GREEN);
    }

    @Override
    protected void onSizeChanged(int w, int h, int oldw, int oldh) {
        mTrapezoid.setBounds(0, 0, w, h);
    }

    @Override
    protected void onDraw(Canvas canvas) {
        mTrapezoid.draw(canvas);
    }
}

答案 2 :(得分:2)

您可以尝试在代码或xml中创建LayerDrawable。制作矩形和三角形 查看开发者网站以获取更多信息:http://developer.android.com/reference/android/graphics/drawable/LayerDrawable.html