Take control of view’s shadow — Android (Part 2)

Arman Soudi
2 min readFeb 16, 2020

I’m back with the second part of this blog post — Finally!

Lots of things changed in the Android eco-system, we have a whole fancy line of libraries that Google made for us and I can say that life as an Android developer is much easier than before.

But, playing with shadows in Android views is still challenging and I’m glad that you guys liked the first part of this blog post.

So what we’re gonna do this time is:

  • We’re gonna use Kotlin only (What else!)
  • We’re gonna take care of the old code that we had, and try to make it work with the new version of Android
  • We’re gonna mask some shapes on the View with shadow (of course!)
  • There were some issues with using shadowed view in the Recycler View: we will check to see what we can do about this.

Let’s start!

First of all, I want to wrap up all codes in the View class Extension, So we can use it in all available Views.

This should work with all Android versions available till now (API 29)

Only thing that we need to do is call setShadow and pass the parameters that are required.

Lovely!

Now let’s mask a circle on top of the FrameLayout. One thing you should know about masking is that the whole process is very experimental. For different shapes you may need to change some numbers and codes to find the best result.

We’re gonna define Paint object that has a shadow layer, and we’re gonna use that to draw a circle on top of the View.

P.S. I checked TestFrameLayout with RecyclerView and I could’t find any performance issue. If you face any issue please put a comment and I’ll try to answer as soon as I can.

Cheers.

--

--