Android 如何在矩形边上打一个半圆孔?

Android 如何在矩形边上打一个半圆孔?,android,geometry,draw,shapes,android-shapedrawable,Android,Geometry,Draw,Shapes,Android Shapedrawable,我需要做一个矩形,中间有一个圆孔,像这样 获得它有不同的方法。 您可以使用材质组件库和创建自定义形状路径(需要版本1.1.0) 例如,您可以执行以下操作: LinearLayout linearLayout= findViewById(R.id.linear_rounded); //Use the BottomAppBarTopEdgeTreatment to apply the bottom edge shape, or just create a custom class to obtain

我需要做一个矩形,中间有一个圆孔,像这样


获得它有不同的方法。
您可以使用材质组件库和创建自定义形状路径(需要版本
1.1.0

例如,您可以执行以下操作:

LinearLayout linearLayout= findViewById(R.id.linear_rounded);
//Use the BottomAppBarTopEdgeTreatment to apply the bottom edge shape, or just create a custom class to obtain a similar shape
BottomAppBarTopEdgeTreatment bottomAppBarTopEdgeTreatment = new BottomAppBarTopEdgeTreatment(
    getResources().getDimension(R.dimen.margin),
    getResources().getDimension(R.dimen.rounded_corner),
    getResources().getDimension(R.dimen.vertical_offset)
);
bottomAppBarTopEdgeTreatment.setFabDiameter(getResources().getDimension(R.dimen.diameter));

ShapeAppearanceModel shapeAppearanceModel = new ShapeAppearanceModel()
    .toBuilder()
    .setAllCorners(CornerFamily.ROUNDED,radius)
    .setBottomLeftCorner(CornerFamily.ROUNDED,0)  
    .setBottomRightCorner(CornerFamily.ROUNDED,0)
    .setBottomEdge(bottomAppBarTopEdgeTreatment)
    .build();
MaterialShapeDrawable shapeDrawable = new MaterialShapeDrawable(shapeAppearanceModel);
ViewCompat.setBackground(linearLayout,shapeDrawable);

使用
ShapeDrawable
自定义
Shape
-在实现
Shape.draw()
方法时使用
Canvas.drawPath()
我不知道如何使用它。你能给我完整的代码看看MyShape类中有什么吗?不,我没有…看看MaterialComponents库中的MaterialShapeDrawable。只需在底部边缘应用BottomAppBarTopEdge处理(或以其为例)即可获得您的形状。这听起来很完美,但我找不到材质形状可移除依赖项。您也可以添加它吗?@amirhesni您可以找到全部。使用版本1.1.0-rc01