首页 > 解决方案 > 在布局底部创建一个三角形

问题描述

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

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

标签: javaandroidandroid-drawablematerial-components-androidmaterial-components

解决方案


您可以使用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);

在此处输入图像描述

android:clipChildren="false"同样对于边缘处理,父视图必须通过在 xml 中设置来禁用子视图的剪辑。


推荐阅读