6

I'm working on a RecyclerView that shows in horizontal orientation a list of items. Every item is a CardView with elevation=8dp. The elevation gives the items a shadow that gets cut by the RecyclerView. So to make it work I added android:clipChildren="false" and android:clipToPadding="false" so that the shadow is no longer clipped. Everything works fine up until the alpha of the RV changes.

The animation I use to hide RV:

view.apply {
            animate().alpha(0f)
                    .setDuration(300)
                    .withEndAction { view.visibility = outVisibility }
                    .start()
        }

...and the one to show it:

view.apply {
            if (visibility != View.VISIBLE) alpha = 0f
            animate().alpha(1f)
                    .setDuration(300)
                    .withStartAction { view.visibility = View.VISIBLE }
                    .start()
        }

ISSUE

As long as the alpha of RV is lower than 1.0 the items inside of the RV are being clipped(the shadows). As soon as the alpha reaches 1.0 the shadows suddenly stop being clipped and everything looks nice again.

Is there anything I can do to fix it?

Same issue occurs if I set the alpha to a fixed value below 1.0 in XML(not animating at all in this case). Also the same issue occurs when I try to animate alpha of items inside the RV.

When RV alpha = 1.0 https://www.dropbox.com/s/g1cun1jqv0op0qw/Screenshot_20190423-180952__01.jpg?dl=0

When RV alpha = 0.9 https://www.dropbox.com/s/s06f9f3orci135m/Screenshot_20190423-180819__01.jpg?dl=0

UPDATE 1 When I try to animate alpha RV parent instead of RV itself then the issue disappears. But it feels kinda hacky to have to wrap RV in extra parent just to animate its alpha :/

methedroid
  • 61
  • 3
  • 1
    To draw a transparent View, Android calls `canvas.saveLayerAlpha` which creates an intermediate buffer before drawing, calls `draw()`, and then `canvas.restore()` which blends the buffer into the surface (see `View.draw(Canvas canvas, ViewGroup parent, long drawingTime)`). Unfortunately, the bounds passed to `saveLayerAlpha` are not under our control, so clipChildren is effectively true if alpha != 1. Thus, there's no 'canonical' way, only workarounds. – Miha_x64 Jan 21 '21 at 21:41
  • I'm having a similar issue and have found it hard coming up with a solution. @Miha_x64 any chance you can expand on this issue? – Jona Oct 22 '21 at 02:14
  • 1
    @Jona if you need a shadow, use `RecyclerView.ItemDecoration`, for example, https://github.com/Miha-x64/FiftyShades – Miha_x64 Oct 22 '21 at 17:50

0 Answers0