r/androiddev • • Jul 27 '19

How would you implement adaptive CardView drop-shadow like in this screenshot?

Post image
218 Upvotes

38 comments sorted by

30

u/Dimezis Jul 28 '19

I implemented this recently in my pet project with Renderscript blur. Works fast, can open source it if you're interested

5

u/TheEggRoller Jul 28 '19

Yes please, I would definitely be interested.

11

u/Dimezis Jul 29 '19 edited Jul 29 '19

Ok folks, so here you go.

https://github.com/Dimezis/ShadowView/

I didn't publish it as a library for couple of reasons, but there's a separate module with the ShadowView and an example project.

Currently it has some bugs/limitations and maybe not the best API.

So gotcha #1: parent view has to set `android:clipChildren="false"` so the shadow is not clipped. It can be done without it, but I was too lazy back in time.

Gotcha #2: if you want to cast a shadow for an ImageView (like in the example), you gotta be aware that ImageView has a quirky way of drawing itself on canvas if you set a scale type. So for instance, if you set scale type `centerCrop`, the shadow would be much bigger than the ImageView itself, like the bitmap was scaled.

I haven't worked it around yet and would appreciate a contribution.

Gotcha #3: in some cases shadow might be cut, I'm assuming it might be related to ImageView's scale type again.

Regarding the performance, it works super fast, as I'm blurring a very downscaled version of the View's snapshot.

I'll add some description later, and also if you want to help me to make it a library, be my guest.
Ping me here or on the GitHub.

1

u/Dimezis Jul 29 '19

And if you want to mimic 1 to 1 the example OP provided, you can just apply additional scaling on the blurred bitmap so the shadow won't be cast in all directions

4

u/[deleted] Jul 28 '19

Upvote if interested

2

u/paolovalerdi Jul 29 '19

Would be awesome

48

u/liuwenhao Jul 27 '19

Write your own custom view that applies a guassian blur to the thumbnail and offsets it so that you get that dropshadow blur effect you are looking for. AFAIK that layout is not possible by default (but I could be wrong), so you need to hack around it and a custom view should not be too difficult.

20

u/Nickx000x Jul 27 '19

If you don't async it shouldn't hurt the initial loading performance much. There's several libraries like Blurry or Dali that can apply those effects either synchronously or asynchronously using ScriptIntrinsicBlur, perhaps someone could build off of those.

21

u/michellbak Jul 27 '19

Probably a good use case for Picasso/Glide transformations where you draw a blurry version of the loaded image on a canvas and put the "normal" version on top. This would also allow you to create the rounded borders and cache the images for performance.

2

u/printeastwoodcz Jul 27 '19

absolutely agree, elevation couldn't be coloured like on screenshot, you need blurred image under top card.

13

u/hnocturna Jul 27 '19

This is the only solution I've found for it for API 28+. I've tried to generalize the gist solution for lower API levels and didn't have any success, but maybe you will: https://stackoverflow.com/questions/51001921/changing-cardview-shadow-color?rq=1

11

u/kemb0 Jul 28 '19

I wonder if an acceptable solution, rather than these cpu intensive blur suggestions, would be too create a new bitmap from the bottom row of pixels of the base image, then stretch it over the area you want for the blur and finally apply you're own generic "fade out" style mask that'll give that gentle fade out look.

Ultimately I don't think these blurs really use any discernible colour data other than the bottom row of colours from the image so why perform complex operations on an entire image where 99% of that image won't be seen in the result?

Might look crap but very simple to try out and if it is acceptable it ought to be a snappy solution.

2

u/Advait1306 Jul 29 '19

I like what you just did there.

37

u/bernaferrari Jul 27 '19

You just don't, Android isn't good at it.

You probably want this: https://github.com/armcha/ColoredShadowImageView

But be aware performance is not good and etc, etc, etc. As someone else mentioned, there is an API for CardView at API 28+ that lets you choose the shadow color (but not put an image like you asked).. And that's it.

7

u/[deleted] Jul 27 '19

Android isn't good at it.

Why? Just because there isn't a built in CardView method called showCoolColorShadow()? Or is there some bad performance specifically in Android when creating these effects?

5

u/bernaferrari Jul 27 '19

is there some bad performance

I don't remember exactly, but IIRC to make those things you end up disabling hardware acceleration or something like it (there are plenty of anti_alias flags to play with) and this is one of the factors that contribute to low performance. If you search for why API 28 has support for it while others doesn't (and you can't include it in jetpack) you might find one of the answers. There are many factors involved. There is also the carbon library that does the colorful shadow on a lot of APIs, but it has other issues (which I don't remember, but it has).

1

u/[deleted] Jul 27 '19

Gotcha, thanks for clarifying! Couldn't tell from your first comment if it just wasn't easy and fun to do.

1

u/bernaferrari Jul 27 '19

Yeah, I never had luck with it and never seen someone that has, except one or other isolated case like Google Play Games. It's just like the xamarin post someone made today that everybody is screaming saying "no, run away". Same thing for me with colorful shadows.

8

u/Sauronshit Jul 27 '19

How come things like these are so hard/impossible to do on Android, while on the web it's so much easier?

23

u/dancovich Jul 27 '19

It's not hard or impossible, just implement a custom view that draws the effect using the Canvas. It's just that we don't have an official component that does this.

Speed is the bigger issue. To draw the effect you need to render the child of the card on a back buffer, grab the image, apply some blur and some transparency and draw the resulting image before drawing the child. That leads to drawing the content at least twice and apply some heavy effects which means such a component can tank the performance of a budget phone.

9

u/Micrococonut Jul 28 '19

Sounds like something that could be precomputed once and cached for rendering. I don’t see why this would be slow.

1

u/manys Jul 28 '19

It looks like circular segments blurred based on the colors in each bottom row of pixels.

4

u/bernaferrari Jul 27 '19

Well.. nowadays people usually do what is in the official Material Design spec and this isn't, unfortunately. Also, most people follow material design components and these components are always being rewritten - mdc is currently in alpha for 1 year! We can't even get the basics right, I have 3 bugs opened for the Input box for months.. I think when we get the basics right, those things will become easier. And the only chance we have to get the basics right is with Compose UI. So pray that by next year we get something like that in Compose!

1

u/Mavamaarten Jul 28 '19

Hmmm. It's a good question, but hard to explain. It's got mostly to do with the fact that you want to keep touch latency as low as possible. It's possible, but you'd have to do a lot of tricks to keep the frame times low. Even the slightest delay in scrolling makes an app feel like a sluggish mess. That's often a challenge even without visual effects.

A webbrowser also works completely differently. There everything is rendered into a buffer that can be scrolled. An android app has separate views that are / can be rendered individually.

To implement this on Android, you would have to precalculate the blurred images and do some tricks to show them in the correct location, because it's not as simple as applying some blur shader to a view. It's going to be a mess. So I'd argue to just leave out that feature because it's a very minor effect with no actual gains.

5

u/Manishalexin Jul 27 '19

One approach might be to reduce the image size and then apply gussian blur and finally overlay with the orignal image with offset.

5

u/[deleted] Jul 28 '19

I've got some ideas, I'll give it a go next week if I can find the time. Use Renderscript's intrinsic blur and only use the bottom 1/5 of the image to speed it up.

13

u/[deleted] Jul 27 '19

With flutter xD

6

u/bernaferrari Jul 27 '19

Flutter is also not good at it. I tried, things got complicated fast (and performance issue).. but yeah, it is a lot easier/better than native Android currently for this.

1

u/[deleted] Jul 28 '19

Do you not just wrap a widget in ClipRRect and BoxShadow? Or is the issue that the shadow is just grey?

1

u/bernaferrari Jul 28 '19

Yeah, but when you start playing with it you loose the default shadow and etc.. I mean, I just want the shadow from material with other color, I don't want to tweak the spread and a lot of other stuff to try to find the correct value so that it becomes equivalent to the native. If the material widget had a shadow color attribute things would be a lot easier.

7

u/[deleted] Jul 28 '19

Here's my attempt in web https://codepen.io/shoopy/full/PMbVLB

Essentially you have a floating image view beneath the card. In that view the image should be blurred.

A bit of inset shadow as well so you can read the white text on bright images. You can achieve that by either darkening the image or having a floating image view beneath the text of a transparent vignette.

5

u/TheIronMarx Jul 27 '19

I would make a service that would take the image and apply a blur and then provide that processed image in the API along with the starting image. Just show one offset from the other. Performant.

3

u/TheIronMarx Jul 27 '19

Some other tricks with the Palette API would probably work just fine, too. Be creative!

1

u/quocthinhvo Jul 28 '19

Beautifull

0

u/gbhall Jul 28 '19

I have a modified question, how do you do this on iOS? This is beautiful 😍

-9

u/SXHarrasmentPanda Jul 27 '19 edited Jul 28 '19

You could achieve this in CSS if you were to serve a web app using something like nativescript https://www.nativescript.org/ but obviously that requires web dev skillset and has its pros and cons

EDIT: You can downvote me, but I'm not wrong. This is a valid solution to the problem πŸ™ƒ

-7

u/VenuPrasathM Jul 28 '19

Can be done with a bottomSheetDialog fragment.

0

u/[deleted] Jul 28 '19

[deleted]

2

u/Gudin Jul 28 '19

Because everyone is suggesting to apply blur to image, but they are missing step to manipulate alpha to make shadow-like appearance.