Appcompat v21 Toolbar elevation pre-lollipop

后端 未结 10 638
你的背包
你的背包 2020-12-23 13:33

First off, I know that this question has been asked before, but it hasn\'t been answered before. I hope someone can give me an answer.

In my application, I use the T

10条回答
  •  予麋鹿
    予麋鹿 (楼主)
    2020-12-23 14:31

    It's possible to have real shadows - animated and generated. The method used by Lollipop is available since Froyo. Hardware acceleration used for shadow generation is available since Honeycomb I guess. 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). I'm not going to dive into the details, because there's a lot of them including issues with compilation and minor performance optimisations. But it's possible.

    Why there's no shadows in the official support library?

    • it would require changes in the UI framework as it's impossible to freely draw outside view bounds
    • smooth animation requires a quite good GPU

    See:

    • https://www.youtube.com/watch?v=jbU4SXblO5s
    • https://androidreclib.wordpress.com/2014/11/25/lollipops-shadows-on-gingerbread/
    • https://github.com/ZieIony/Carbon

提交回复
热议问题