I am developing an Android app for both Lollipop and Previous versions.
I am using CardView (This cardView does not have any child, it simply placed behind my View) to create shadow.
But the problem arise when it runs on the pre Lollipop devices.
so I set cardUseCompatPadding to true. I am wondering if I could get the value of this compat padding?
Is there anywhere I could find the reference to the value?
CardView is a new widget in Android that can be used to display any sort of data by providing a rounded corner layout along with a specific elevation. CardView is the view that can display views on top of each other. The main usage of CardView is that it helps to give a rich feel and look to the UI design.
For a more consistent UI, use cardUseCompatPadding=true. CardView adds additional padding to draw shadows on platforms before Lollipop. This may cause Cards to have different sizes between Lollipop and before Lollipop.
CardView uses elevation property on Lollipop for shadows and falls back to a custom emulated shadow implementation on older platforms. Due to expensive nature of rounded corner clipping, on platforms before Lollipop, CardView does not clip its children that intersect with rounded corners.
The compat padding added to the CardView depends on the elevation and the radius of the corners you have set. You can find the actual calculation in the RoundRectDrawableWithShadow class in the support library.
You can calculate it at runtime using something like:
float elevation = cardView.getMaxCardElevation();
float radius = cardView.getRadius();
double cos45 = Math.cos(Math.toRadians(45));
int horizontalPadding = (int) (elevation + (1 - cos45) * radius);
int verticalPadding = (int) (elevation * 1.5 + (1 - cos45) * radius);
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With