-->

缩放然后转动一个矩形绘制(Scaling then rotating a rectangle dra

2019-10-20 03:43发布

我想向下扩展矩形绘制,然后旋转它,一旦它被视图修剪它类似于与左侧倾斜的梯形:

旋转工作正常:

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

但这恰恰使矩形消失。 有谁知道如何最好地实现这一目标?

Answer 1:

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

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做到这一点。



文章来源: Scaling then rotating a rectangle drawable