1

我想向下缩放一个可绘制的矩形,然后旋转它,以便一旦它被视图剪辑,它就像一个左侧倾斜的梯形: 在此处输入图像描述

旋转工作正常:

<?xml version="1.0" encoding="utf-8"?>
<layer-list xmlns:android="http://schemas.android.com/apk/res/android" >
<item >
    <rotate
        android:fromDegrees="-19.5"
        android:toDegrees="-19.5"
        android:pivotX="0%"
        android:pivotY="0%"
         >
        <shape
            android:shape="rectangle" >                
            <solid
                android:color="@android:color/black" />
        </shape>
    </rotate>
</item>
</layer-list>

但是,为了防止矩形从视图底部旋转离开的大间隙,我想在旋转发生之前垂直缩放 200%。我希望我能做这样的事情:

<?xml version="1.0" encoding="utf-8"?>
<layer-list xmlns:android="http://schemas.android.com/apk/res/android" >
<item >
    <scale 
        android:scaleWidth="100%"
        android:scaleHeight="200%"
        android:scaleGravity="top"
        >
        <rotate
            android:fromDegrees="-19.5"
            android:toDegrees="-19.5"
            android:pivotX="0%"
            android:pivotY="0%"
             >
            <shape
                android:shape="rectangle" >                
                <solid
                    android:color="@android:color/black" />
            </shape>
        </rotate>
    </scale>
</item>
</layer-list>

但这只会导致矩形消失。有谁知道如何最好地实现这一目标?

4

1 回答 1

0

没有真正的答案,但我现在使用的解决方案是创建一个绘制形状的自定义 Drawable:

public void setColor(int color) {
    _color = color;
}

@Override
public void draw(Canvas canvas) {
    int width = this.getBounds().width();
    int height = this.getBounds().height();
    double angle = 19.5 * (Math.PI / 180.0);
    double offsetX = height * Math.tan(angle);

    Path path = new Path();
    Paint paint = new Paint();
    path.moveTo(0, 0);
    path.lineTo(width, 0);
    path.lineTo(width, height);
    path.lineTo((int)offsetX, height);
    path.close();

    paint.setColor(_color);
    paint.setStyle(Paint.Style.FILL);
    canvas.drawPath(path, paint);
}

现在可以完成这项工作,尽管令人沮丧的是我无法在 xml 中找到执行此操作的方法。

于 2014-05-22T10:02:44.437 回答