Android 缩放然后旋转可绘制的矩形
我想缩放一个可向下绘制的矩形,然后旋转它,这样一旦它被视图剪裁,它就像一个左侧倾斜的梯形: 旋转工作正常:Android 缩放然后旋转可绘制的矩形,android,imageview,android-drawable,xml-drawable,shapedrawable,Android,Imageview,Android Drawable,Xml Drawable,Shapedrawable,我想缩放一个可向下绘制的矩形,然后旋转它,这样一旦它被视图剪裁,它就像一个左侧倾斜的梯形: 旋转工作正常: <?xml version="1.0" encoding="utf-8"?> <layer-list xmlns:android="http://schemas.android.com/apk/res/android" > <item > <rotate android:fromDegrees="-19.5"
<?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>
但这只会导致矩形消失。有人知道如何最好地实现这一点吗?没有真正的答案,但我现在使用的解决方案是创建一个自定义可绘制形状:
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中实现这一点的方法。元素缩放需要属性drawable,而属性drawable需要进行缩放。这就是为什么什么都不显示。请看:您解决了吗?我有一个问题,我并没有只用XML解决,我使用了下面答案中的解决方案,并创建了一个自定义的可绘制文件。