how to set shadow to a View in android?

后端 未结 6 1302
醉话见心
醉话见心 2020-11-27 03:24

I want to know how to add a shadow layer to any general View in android. for eg: suppose i have a layout xml, showing something like this..



        
6条回答
  •  甜味超标
    2020-11-27 04:20

    The best way to create a shadow is to use a 9patch image as the background of the view (or a ViewGroup that wraps the view).

    The first step is to create a png image with a shadow around it. I used photoshop to create such an image. Its really simple.

    • Create a new image with Photoshop.
    • Add a layer and create a black square of 4x4.
    • Create a shadow on the layer by selecting the layer in layer explorer and clicking on a button titled fx and choosing drop shadow.
    • Export the image as png.

    The next step is to create 9-patch drawables from this image.

    • Open draw9patch from android-sdk/tools
    • Open the image in draw9patch
    • Create 4 black lines on the four sides of the square like the following and then save the image as shadow.9.png.

    Now you can add this shadow as the background of the views you want to add the shadow to. Add shadow.9.png to res/drawables. Now add it as a background:

    
    

    I recently wrote a blog post that explains this in detail and includes the 9patch image that I use for creating the shadow.

提交回复
热议问题