how to build a trapezoid shape in android?

旧城冷巷雨未停 提交于 2020-01-21 04:17:40

问题


how can I create a trapezoid shape like below image ?

I don't want to use an image or 9.png .


回答1:


Try this:

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




回答2:


This class is a View that defines and draws a trapezoid ShapeDrawable. Thus the trapezoid, being a Drawable, can be used in backgrounds as well.

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);
    }
}



回答3:


You can try by making a LayerDrawable in either code or xml. Make a rectangle shape and a triangle shape Check out the developer site for more information: http://developer.android.com/reference/android/graphics/drawable/LayerDrawable.html



来源:https://stackoverflow.com/questions/25768037/how-to-build-a-trapezoid-shape-in-android

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!