缩放然后旋转矩形drawable [英] Scaling then rotating a rectangle drawable
问题描述
我想缩放一个向下绘制的矩形,然后旋转它,一旦它被视图剪切,它就像一个左侧倾斜的梯形:
I would like to scale a rectangle drawable downwards, then rotate it so once it is clipped by the view it resembles a trapezoid with the left side slanted:
轮换工作正常:
<?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%。我希望我可以这样做:
However to prevent a big gap where the rectangle has rotated away from the bottom of the view I want to scale vertically by 200% before the rotation happens. I was hoping that I could do something like this:
<?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>
但这只会导致矩形消失。有谁知道如何最好地实现这个目标?
but this just causes the rectangle to disappear. Does anyone know how best to achieve this?
推荐答案
没有真正的答案,但我现在使用的解决方案是创建绘制形状的自定义Drawable:
No really a true answer but I solution that I am using now is to create a custom Drawable that draws the shape:
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中执行此操作的方法。
This does the job for now, although it is frustrating that I cannot find a way to do this in the xml.
这篇关于缩放然后旋转矩形drawable的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!