Reputation: 11891
I want to achieve this:
So I thought I could use a MaterialShapeDrawable
to apply that transformation:
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)
}
But unfortunately it changes nothing. So I've tried to apply to a direct children like this:
<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
And it kinda works but I get a funky result due to app:cardElevation="10dp"
(which is needed):
So my questions are:
1 - Is it possible to apply transformations to a CardView? if so, how?
2 - How can I solve this "shadow on top" problem?
Thanks.
Upvotes: 4
Views: 2445
Reputation: 364828
Since you are drawing outside the CardView
, in the parent view you should use something like:
<LinearLayout
android:clipChildren="false"
android:clipToPadding="false"
android:padding="16dp"
..>
<com.google.android.material.card.MaterialCardView
app:cardElevation="2dp"
../>
Then apply the changes to ShapeAppearanceModel
but getting the existing ShapeAppearanceModel
used by the cardview (in your code you are replacing the existing MaterialShapeDrawable
not only applying a change in the 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());
Starting with the 1.2.0-beta01
you can also use the OffsetEdgeTreatment
with a MarkerEdgeTreatment
.
Something like:
MarkerEdgeTreatment markerEdgeTreatment = new MarkerEdgeTreatment(size);
OffsetEdgeTreatment offsetEdgeTreatment = new OffsetEdgeTreatment(markerEdgeTreatment,offset);
cardView.setShapeAppearanceModel(cardView.getShapeAppearanceModel()
.toBuilder()
.setTopEdge(offsetEdgeTreatment)
.build());
Upvotes: 4