切割 XML 形状的一部分
Cut part of XML shape
我在 XML 中有一个矩形视图:
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="rectangle">
<solid android:color="@color/lightblue"/>
</shape>
我想要的是把它切成两半,所以结果会是这样的:
可能吗?如果是,我该如何实现?
注:
1) 添加旋转的白色矩形不是解决方案。我需要保持蓝色形状的切割区域透明(它下面有更多的视图层)。
2)矩形的左下角有点圆(上图忘记画了)
玩转位,从 API 21 开始你可以使用 vector drawables
这是我所做的,根据需要更改数字。
创建了一个可绘制对象 my_custom_shape.xml
作为资源文件
<?xml version="1.0" encoding="utf-8"?>
<vector xmlns:android="http://schemas.android.com/apk/res/android"
android:height="100dp"
android:width="100dp"
android:viewportHeight="100"
android:viewportWidth="45" >
<group
android:name="triableGroup">
<path
android:name="triangle"
android:fillColor="#000"
android:pathData="m 0,0 l 50,100 -50,0 z" />
</group>
</vector>
输出:
并在您的 View
中将其设置为背景 android:background="@drawable/my_custom_shape"
注意:忘记尝试使用 px 而不是 dp,看看将有助于为每个设备保持相同的大小,试试看
像这样使用 ShapeDrawable
:
Drawable d = new ShapeDrawable(new S(Color.BLUE, 32));
其中 class S
是自定义 Shape
:
class S extends Shape {
final int color;
final float radius;
Path path = new Path();
public S(int color, float radius) {
this.color = color;
this.radius = radius;
}
@Override
protected void onResize(float width, float height) {
path.reset();
path.moveTo(0, 0);
path.lineTo(width, height);
path.lineTo(radius, height);
RectF oval = new RectF(0, height - 2 * radius, 2 * radius, height);
path.arcTo(oval, 90, 90);
path.close();
}
@Override
public void draw(Canvas canvas, Paint paint) {
paint.setColor(color);
canvas.drawPath(path, paint);
}
}
现在您可以在对 View#setBackground()
、TextView#setCompoundDrawables()
等的任何调用中使用 Drawable d
我在 XML 中有一个矩形视图:
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="rectangle">
<solid android:color="@color/lightblue"/>
</shape>
我想要的是把它切成两半,所以结果会是这样的:
可能吗?如果是,我该如何实现?
注:
1) 添加旋转的白色矩形不是解决方案。我需要保持蓝色形状的切割区域透明(它下面有更多的视图层)。
2)矩形的左下角有点圆(上图忘记画了)
玩转位,从 API 21 开始你可以使用 vector drawables
这是我所做的,根据需要更改数字。
创建了一个可绘制对象 my_custom_shape.xml
作为资源文件
<?xml version="1.0" encoding="utf-8"?>
<vector xmlns:android="http://schemas.android.com/apk/res/android"
android:height="100dp"
android:width="100dp"
android:viewportHeight="100"
android:viewportWidth="45" >
<group
android:name="triableGroup">
<path
android:name="triangle"
android:fillColor="#000"
android:pathData="m 0,0 l 50,100 -50,0 z" />
</group>
</vector>
输出:
并在您的 View
中将其设置为背景 android:background="@drawable/my_custom_shape"
注意:忘记尝试使用 px 而不是 dp,看看将有助于为每个设备保持相同的大小,试试看
像这样使用 ShapeDrawable
:
Drawable d = new ShapeDrawable(new S(Color.BLUE, 32));
其中 class S
是自定义 Shape
:
class S extends Shape {
final int color;
final float radius;
Path path = new Path();
public S(int color, float radius) {
this.color = color;
this.radius = radius;
}
@Override
protected void onResize(float width, float height) {
path.reset();
path.moveTo(0, 0);
path.lineTo(width, height);
path.lineTo(radius, height);
RectF oval = new RectF(0, height - 2 * radius, 2 * radius, height);
path.arcTo(oval, 90, 90);
path.close();
}
@Override
public void draw(Canvas canvas, Paint paint) {
paint.setColor(color);
canvas.drawPath(path, paint);
}
}
现在您可以在对 View#setBackground()
、TextView#setCompoundDrawables()
等的任何调用中使用 Drawable d