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.
Related
i am trying to coding the below image for android app design, i can not get shadow as same as image how i can do it perfectly? and can control with opacity, degree and color of shadow?
You can use cardView which has an elevation attribute which gives shadows to your card view.
also there are third party libraries which you can use like this:
https://github.com/loopeer/shadow
also here is Google documentation which describes how you should use shadows in your ui:
https://developer.android.com/training/material/shadows-clipping
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
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.
What is the use of shadow in Android?
How do I implement shadow in thumbnails?
If someone has a sample screenshot, do you mind sharing it?
Shadows can be added to TextViews through XML or programmatically.
If you'r wanting to add a shadow to a standard view (or any subclasses) this thread should point you in the right direction.
Not knowing what are your objects could be tricky. you can define what's the background in the picture and filter it, then you can strech your image in the direction of the shadow. now filter everything but the background, so you've got 3 layers - background, shadows and objects. place the shadows on the background and on top of that the objects.