【问题标题】:how to build a trapezoid shape in android?如何在android中构建梯形?
【发布时间】:2014-09-10 14:27:35
【问题描述】:

如何创建如下图所示的梯形?

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

【问题讨论】:

标签: android android-shape


【解决方案1】:

试试这个:

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

【讨论】:

  • 我推荐这个答案。请参阅我的回答 here 以获取文档链接和一个示例,该示例将提供更多关于如何使用它的想法。
  • 我希望有人能解释如何绘制这种矢量形状。
【解决方案2】:

这个类是一个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);
    }
}

【讨论】:

  • 第一眼看起来好多了。希望这是一个更好的解决方案,然后接受一个
【解决方案3】:

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

【讨论】:

    猜你喜欢
    • 2018-03-12
    • 1970-01-01
    • 1970-01-01
    • 2011-12-16
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多