【问题标题】:How to make a semicircular Hole in a rectangle side?如何在矩形边上制作一个半圆形孔?
【发布时间】:2020-05-02 00:31:33
【问题描述】:

我需要制作一个中间有一个圆孔的矩形

【问题讨论】:

  • 使用 ShapeDrawable 和自定义 Shape - 实现 Shape.draw() 方法时使用 Canvas.drawPath()
  • 我不知道如何使用,你能告诉我完整的代码
  • MyShape 类中有什么?
  • 不,我没有...
  • 看看 MaterialComponents Library 中的 MaterialShapeDrawable。只需在底部边缘应用BottomAppBarTopEdgeTreatment(或以它为例)即可获得您的形状。

标签: android geometry draw shapes android-shapedrawable


【解决方案1】:

有多种获取方式。
您可以使用材料组件库和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);

【讨论】:

  • 这听起来很完美,但我找不到 MaterialShapeDrawable 依赖项,你也可以添加它吗?
  • @amirhesni 你可以找到所有info here。使用版本 1.1.0-rc01
猜你喜欢
  • 1970-01-01
  • 2015-08-08
  • 2018-10-03
  • 1970-01-01
  • 2014-07-17
  • 1970-01-01
  • 2019-05-07
  • 2021-12-30
  • 1970-01-01
相关资源
最近更新 更多