java - 在布局底部创建一个三角形
问题描述
我正在开发一个在活动顶部包含一个形状的 android 应用程序,我正在尝试实现它,但努力做到这一点。
我试图创建一个可绘制文件,该文件创建一个三角形并设置底角半径以匹配上面的形状但不起作用。任何人都可以帮助我,拜托。
解决方案
您可以使用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 中设置来禁用子视图的剪辑。
推荐阅读
- angular - 角度反应形式:如何在对话框中显示验证错误?
- python - 是否可以在使用 excel 打开之前格式化 .tsv 文件中的单个单元格?(通过python?)
- javascript - 使用 ActiveX 的 Outlook 约会
- javascript - 如何从 mountebank 响应块中的另一个 js 文件调用函数
- amazon-s3 - 在 AWS S3 的子目录中创建目录是错误的
- node.js - 如何检查我的 Node.js create-react-app 是否在 localhost 上运行?
- new-operator - 如何使用 EasyMock 为新关键字编写期望语句?
- cron - 除了每周日上午 10 点到下午 3 点外,Cron 表达式一直运行
- sql - 在迁移中使用另一列作为默认值
- javascript - Angular ChartJs 不显示多个图表的数据