In android 5.0+ exists property elevation, which add shadows for views. Who can suggest how make compatiblity with older versions 5.0< ? 9-patch not good idea, because in my app shadow everywhere and views different (with rounded corners, not rounded and e.t.c).
you can use card view from support. but if you meant Ripple animation you should draw it by yourself
Related
A somewhat well-known issue in Android, since Lollipop and the introduction of Material Design, is that elevated Views with see-through backgrounds reveal ugly artifacts from their shadow draws behind them.
Those visual glitches are all caused by the shadows being adjusted for increasing elevation by kind of "zooming out" on the gradients, which causes their inner borders to shrink inward within the View's bounds, normally unseen in ones with opaque backgrounds. No clipping is done there, and every relevant check I've come across in the source turns the shadow off completely when that artifact would be seen, so that clipping is seemingly omitted intentionally, likely for efficiency.
As you might expect, many questions have been posted about it here over the years, but turning it off or somehow avoiding it altogether seem to be about the only generally effective solutions other users have found, as well:
How to set semi transparent background color for Android CardView?
CardView background alpha colour not working correctly
Cardview - rounded corners with transparent background
Elevation + transparency bug on Android Lollipop
How to create a shape with a transparent background and a shadow, but the shadow should not be visible behind the shape outline?
CardView with transparent background issue on > API 21
Weird view behavior when an elevation and a transparent background color are used on API 21
Remove background colour from FloatingActionButton
Android Floating Action Button Semi Transparent Background Color
How to set border and background color of Floating Action Button with transparency through xml?
How to center image in FloatingActionButton behind transparent background?
How to remove those dark circular background from floating action button?
Android transparency and Shadows
a shadow appear below material button after setting background color tint in android
Multiple layers of shadows in android recyclerview elevation
How to adjust shadow in translucent navbar?
Transparent white button looks bad in Android Material Design
Various bespoke workarounds are available for a few common setups, some users ignore it or don't realize what it is, and some have even integrated the effect into their designs, but I still have not found a single example where it's been truly fixed. I haven't gone digging into the native graphics code yet, but I also can't find any instance in the SDK source where anything is done about it other than disabling the shadow when that might be visible, and if we're not able to do it at the app level it doesn't really matter what the low-level graphics stuff can do.
There doesn't seem to be much out there about the general problem, but recently I'd shared some information about it on this old question concerning CardView, including a couple of basic techniques for creating clipped shadow replicas as replacements. However, the examples in that answer are quite specific, and adjusting them for multiple different Views would be tedious and error-prone, and would likely require some not-insignificant modifications to an existing setup in order to add them.
Is there any way to apply those workarounds generally, and with minimal changes?
I am trying to implement shadows for my views. I have given a sample image which describe what I want, I need a google standard methods for implementing shadows, I have tried with drawable images but as you know which is not standard method.
You mean like this?
Defining Shadows and Clipping Views
https://developer.android.com/training/material/shadows-clipping.html
You have to use CardView as view container (which is in an official support library) and set properties:
app:cardElevation="4dp" // shadow
app:cardUseCompatPadding="true">
where app namespace is:xmlns:app="http://schemas.android.com/apk/res-auto"
I'm guessing that you're thinking about older devices.
You can render shadows using RenderScript blurs and layer composition. This method is available since Android Froyo. It's pretty easy to create such shadows from scratch. You have to:
Draw a View as a black shape to an offscreen bitmap.
Blur that bitmap with ScriptIntrisincBlur
Draw that bitmap with alpha
Draw the original View on top of that shadow.
You have to specify a blur radius based on View's elevation. You may also offset the shadow a bit. For implementation details see:
https://github.com/ZieIony/Carbon/blob/master/carbon/src/main/java/carbon/shadow/ShadowGenerator.java
And
https://github.com/ZieIony/Carbon/blob/master/carbon/src/main/java/carbon/widget/LinearLayout.java#L128
In general, with API21 you can use the view.setElevation() method.
Better if you use the ViewCompat.setElevation() method.
Pay attention, it creates a shadow only in devices with API21+.
If you would like to create an elevation for other views for device with api<21, you have to use a custom drawable.
Currently the only standard view that supports elevation in all devices, is the CardView.
In this case you can use the setCardElevation() method.
I want to create something close to Material Theme in app that is supposed to work with Android 4.x and 5.x.
I have tried to use Material theme, but I need api 21 for that. My current minimum api is 15 and I want to keep that.
Do I have to create shadows as PNG images or there is easier way?
I want to keep application compatybile with Android 4.x and I have no time to maintain two versions (for Android 4.x and 5.x).
I wouldn't call it an easy way, but you can mimic real animated shadows on any device above Android Cupcake. Here's how it works:
draw your view to an off-screen bitmap with LightingColorFilter set to 0,0
blur the black shape (the off-screen bitmap) using the ScriptIntrinsicBlur class and elevation value as radius
draw the bitmap beneath the view
It requires adding custom elevation attributes, custom views capable of rendering shadows, and using render script and the compatibility library (for older devices).
If you'd like to see that solution in action, check my library:
https://github.com/ZieIony/Carbon
I'm using the CardView layout from the v7 support library as a primary design pattern in my app. It works great on Lollipop as you'd expect and looks like this:
If I run this same app on 4.4 or below the content in the card doesn't go to the edge as seen here:
Is there a way that I can get the content to go to the edges on all versions of Android and not just 5.0?
Thanks
Unfortunately the creators of the CardView didn't come up with an efficient way to create rounded corners that would work for any layout on devices running earlier versions that Lollipop. Because of that, padding is simply added to the content of the cards. To prevent this padding use this method:
cardView.setPreventCornerOverlap(false);
If you do this however, the ImageView's corners will be drawn over the rounded corners of the CardView. A solution to this problem is to create a custom ImageView (or Drawable, or Picasso transfromation...) that draws rounded corners. Check this Gist on how to do that.
Since you are using a common layout for cards with a header image, you could check this library by the same author which creates the rounded corners without padding.
I am trying to draw and animate several ImageView items on a screen and I would like to have the images display the blue borders used in other Honeycomb apps (examples of borders here: http://www.youtube.com/watch?v=UPSBctbYc9Q # 1:10).
I've seen hacks/workarounds for older versions of Android using a background image, but I was hoping to avoid that. Can anyone help me to add such borders?
Thanks
I'd suggest just creating a custom view using FrameLayout as its base to layer up the background/border, and then the ImageView on top using an appropriate padding value such that the border remains visible.