【问题标题】:Making a drawable triangle wider - Android使可绘制三角形更宽 - Android
【发布时间】:2016-05-22 08:10:46
【问题描述】:

我将这段代码放在一个可构建三角形的可绘制对象中。但是我认为与三角形的宽度相比,斜边太长了。我希望它像一个等边三角形。我该怎么做呢?谢谢!

<?xml version="1.0" encoding="utf-8"?>

<layer-list xmlns:android="http://schemas.android.com/apk/res/android" >
    <item>
        <rotate
            android:fromDegrees="45"
            android:toDegrees="45"
            android:pivotX="-45%"
            android:pivotY="100%" >
            <shape
                android:shape="rectangle"
                >
                <solid
                    android:color="@color/loseBackground" />
            </shape>
        </rotate>
    </item>
</layer-list>

我已将此代码添加到按钮的背景中。

<Button
        android:id="@+id/loseScreen"
        android:layout_width="200dp"
        android:layout_height="200dp"
        android:rotation="90"
        android:visibility="invisible"
        android:layout_x="140dp"
        android:layout_y="120dp"
        android:background="@drawable/triangle_shape" />

【问题讨论】:

标签: android android-drawable xml-drawable


【解决方案1】:

我发现在 XML 中处理旋转的正方形对于创建我想要的三角形有点不可预测。我制作了一个自定义视图类,它根据您给它的宽度和高度绘制三角形:

public class TriangleView extends View {

    public static final int UP = 0;
    public static final int DOWN = 1;
    public static final int LEFT = 2;
    public static final int RIGHT = 3;

    private int colour;
    private int direction;
    private Paint paint;

    public int getColour() {return colour;}
    public int getDirection() {return direction;}

    public void setColour(int colour) {
        this.colour = colour;
        invalidate();
        requestLayout();
    }
    public void setDirection(int direction) {
        this.direction = direction;
        invalidate();
        requestLayout();
    }


    public TriangleView(Context context, AttributeSet attrs) {

        super(context, attrs);
        initAttributes(attrs);
        setupPaint();
    }


    private void initAttributes(AttributeSet attrs) {

        TypedArray attrArray = getContext().getTheme().obtainStyledAttributes(attrs, R.styleable.TriangleView, 0, 0);

        try {

            colour = attrArray.getColor(R.styleable.TriangleView_triangleColor, Color.BLACK);
            direction = attrArray.getInt(R.styleable.TriangleView_direction, UP);
        } finally {

            attrArray.recycle();
        }
    }

    private void setupPaint() {

        paint = new Paint();
        paint.setStyle(Paint.Style.FILL);
        paint.setColor(colour);
    }

    @Override
    protected void onDraw(Canvas canvas) {

        super.onDraw(canvas);
        canvas.drawPath(getTrianglePath(), paint);

    }

    protected Path getTrianglePath() {

        Point p1, p2, p3;

        switch (direction) {

            case UP:

                p1 = new Point(getPaddingLeft(), getHeight() - getPaddingBottom());
                p2 = new Point(getWidth() - getPaddingRight(), p1.y);
                p3 = new Point((getWidth() - getPaddingLeft() - getPaddingRight()) / 2 + getPaddingLeft() , getPaddingTop());
                break;

            case DOWN:
            default:

                p1 = new Point(getPaddingLeft(), getPaddingTop());
                p2 = new Point(getWidth() - getPaddingRight(), p1.y);
                p3 = new Point((getWidth() - getPaddingLeft() - getPaddingRight()) / 2 + getPaddingLeft(), getHeight() - getPaddingBottom());
                break;
        }


        Path path = new Path();
        path.moveTo(p1.x, p1.y);
        path.lineTo(p2.x, p2.y);
        path.lineTo(p3.x, p3.y);

        return path;
    }
}

您还需要将以下内容添加到您的 res/values/attrs.xml 文件中:

<declare-styleable name="TriangleView">
    <attr name="triangleColor" format="color" />
    <attr name="direction" format="enum">
        <enum name="up" value="0" />
        <enum name="down" value="1" />
        <enum name="left" value="2" />
        <enum name="right" value="3" />
    </attr>
</declare-styleable>

然后您可以在您的布局 xml 中任何您想绘制三角形的地方使用它:

<com.mypackagename.TriangleView
    android:id="@+id/loseScreen"
    android:layout_width="200dp"
    android:layout_height="200dp"
    app:triangleColor="@color/loseBackground"
    app:direction="up"
    android:padding="10dp"/>

记得将应用命名空间添加到您的 xml 布局中:

xmlns:app="http://schemas.android.com/apk/res-auto"

【讨论】:

  • 不,不,不,如果您想要Drawable,请创建您的自定义Drawable,而不是自定义View,或者您可以使用ShapeDrawable 和自定义Shape
  • 以上内容对我很有帮助,但我肯定会研究自定义 Shape 类方法,谢谢!
  • 它是一个抽象类,你只需要实现draw方法
  • 这对我来说有点不知所措。我不太了解该代码。我一直在寻找更简单的东西。感谢您的努力。
【解决方案2】:

我只是用这个解决了它:

<TextView 
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="▼"/>

效果很好。如果我找到更好的解决方案,我可能会更改它。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2014-12-10
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-11-07
    • 2014-11-18
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多