如何在矩形边上打一个半圆孔?
How to make a semicircular Hole in a rectangle side?
我需要制作一个中间有一个圆孔的长方形
获取方式多种多样
您可以使用 Material 组件库和 MaterialShapeDrawable
创建自定义形状路径(您需要版本 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);
我需要制作一个中间有一个圆孔的长方形
获取方式多种多样
您可以使用 Material 组件库和 MaterialShapeDrawable
创建自定义形状路径(您需要版本 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);