在版面底部创建三角形

问题描述 投票:0回答:1

我正在开发一个在活动顶部包含一个形状的android应用程序,我正在尝试实现它,但努力做到这一点。the shape in the green color

我试图创建一个可绘制的文件,该文件创建一个三角形并设置底角半径以匹配上面的形状,但不起作用。任何人都可以帮助我。

java android android-drawable material-components-android material-components
1个回答
1
投票

您可以使用官方EdgeTreatment中包含的EdgeTreatment

只需用以下内容扩展Material Components Library

EdgeTreatment

然后应用它:

public class MyTriangleEdge extends EdgeTreatment {

  private final float size;
  private final boolean inside;

  public MyTriangleEdge(float size, boolean inside) {
    this.size = size;
    this.inside = inside;
  }

  @Override
  public void getEdgePath(
      float length, float center, float interpolation, @NonNull ShapePath shapePath) {
    shapePath.lineTo(0, 0);
    shapePath.lineTo(center, inside ? size  : -size );
    shapePath.lineTo(length, 0);
  }

MyTriangleEdge edgeTreatment = new MyTriangleEdge(height,false); LinearLayout linearLayout= findViewById(R.id.xxxx); ShapeAppearanceModel shapeAppearanceModel = new ShapeAppearanceModel() .toBuilder() .setBottomEdge(edgeTreatment) .build(); MaterialShapeDrawable shapeDrawable = new MaterialShapeDrawable(shapeAppearanceModel); ViewCompat.setBackground(linearLayout,shapeDrawable);

[也用于边缘处理,父视图必须通过在xml中设置enter image description here来禁用子级的剪切。

© www.soinside.com 2019 - 2024. All rights reserved.