gpt4 book ai didi

android - MaterialCardView 与 MaterialShapeDrawable

转载 作者:行者123 更新时间:2023-12-03 23:06:53 24 4
gpt4 key购买 nike

我想实现这一点:

enter image description here

所以我想我可以使用 MaterialShapeDrawable应用该转换:

binding.card.background = MaterialShapeDrawable(
ShapeAppearanceModel.builder()
.setAllCornerSizes(5.dpToPx().toFloat())
.setTopEdge(object : TriangleEdgeTreatment(7.dpToPx().toFloat(), false){
override fun getEdgePath(length: Float, center: Float, interpolation: Float, shapePath: ShapePath) {
super.getEdgePath(length, 12.dpToPx().toFloat(), interpolation, shapePath)
}
})
.build()
).apply {
fillColor = resourcesProvider().colorStateListFromAttr(R.attr.colorSurface, R.style.App_CardView)
}

但不幸的是,它没有任何改变。所以我试图申请像这样的直接 child :
<com.google.android.material.card.MaterialCardView
style="@style/Widget.MaterialComponents.CardView"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginStart="20dp"
android:layout_marginTop="5dp"
android:layout_marginEnd="20dp"
android:clipChildren="false"
app:cardBackgroundColor="@android:color/transparent"
app:cardElevation="10dp">

<androidx.constraintlayout.widget.ConstraintLayout
android:layout_width="match_parent"
android:layout_height="100dp"
android:layout_marginTop="10dp"> //without this margin nothing is shown

它有点工作,但由于 app:cardElevation="10dp",我得到了一个时髦的结果(这是必需的):

enter image description here

所以我的问题是:
1 - 是否可以对 CardView 应用转换?如果是这样,如何?
2 - 我该如何解决这个“顶部阴影”问题?

谢谢。

最佳答案

由于您在 CardView 之外绘图,在父 View 中你应该使用类似的东西:

  <LinearLayout
android:clipChildren="false"
android:clipToPadding="false"
android:padding="16dp"
..>

<com.google.android.material.card.MaterialCardView
app:cardElevation="2dp"
../>

然后将更改应用到 ShapeAppearanceModel 但获取现有的 ShapeAppearanceModel由 cardview 使用(在您的代码中,您将替换现有的 MaterialShapeDrawable 不仅应用更改 ShapeAppearanceModel ):
cardView.setShapeAppearanceModel(cardView.getShapeAppearanceModel()
.toBuilder()
.setTopEdge(new TriangleEdgeTreatment(size, false){

@Override public void getEdgePath(float length, float center, float interpolation,
@NonNull ShapePath shapePath) {
//...... your implementation
}
}
)
.build());

enter image description here

开始1.2.0-beta01 您也可以使用 OffsetEdgeTreatmentMarkerEdgeTreatment .

就像是:
MarkerEdgeTreatment markerEdgeTreatment = new MarkerEdgeTreatment(size);
OffsetEdgeTreatment offsetEdgeTreatment = new OffsetEdgeTreatment(markerEdgeTreatment,offset);

cardView.setShapeAppearanceModel(cardView.getShapeAppearanceModel()
.toBuilder()
.setTopEdge(offsetEdgeTreatment)
.build());

enter image description here

关于android - MaterialCardView 与 MaterialShapeDrawable,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/62019643/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com