1. 程式人生 > >Android Layer-List實現自定義Shape陰影

Android Layer-List實現自定義Shape陰影

一、給右側和底層加陰影

<?xml version="1.0" encoding="utf-8"?>  <layer-list xmlns:android="http://schemas.android.com/apk/res/android" >  
   
    <!-- 陰影部分 -->  
    <!-- 個人覺得更形象的表達:top代表下邊的陰影高度,left代表右邊的陰影寬度。其實也就是相對應的offset,solid中的顏色是陰影的顏色,也可以設定角度等等 -->  
    <item  
        android:left="2dp"  
        android:top="2dp">  
        <shape android:shape="rectangle" >  
   
            <gradient  
                android:angle="270"  
   
                android:endColor="#0F000000"  
                android:startColor="#0F000000" />  
   
            <corners  
                android:bottomLeftRadius="6dip"  
                android:bottomRightRadius="6dip"  
                android:topLeftRadius="6dip"  
                android:topRightRadius="6dip" />  
        </shape>  
    </item>  
   
    <!-- 背景部分 -->  
    <!-- 形象的表達:bottom代表背景部分在上邊緣超出陰影的高度,right代表背景部分在左邊超出陰影的寬度(相對應的offset) -->  
    <item  
        android:bottom="3dp"  
        android:right="3dp">  
        <shape android:shape="rectangle" >  
   
            <gradient  
                android:angle="270"  
                android:endColor="#FFFFFF"  
                android:startColor="#FFFFFF" />  
   
            <corners  
                android:bottomLeftRadius="6dip"  
                android:bottomRightRadius="6dip"  
                android:topLeftRadius="6dip"  
                android:topRightRadius="6dip" />  
        </shape>  
    </item>  
   </layer-list>

 

給左右下加陰影

<?xml version="1.0" encoding="utf-8"?><layer-list xmlns:android="http://schemas.android.com/apk/res/android" >
    <item android:top="2dip"
        android:left="1dip"
        >
        <shape xmlns:android="http://schemas.android.com/apk/res/android"
            android:shape="rectangle">
            <gradient android:angle="270"
                android:startColor="#0f000000"
                android:endColor="#0f000000" />
            <corners android:radius="@dimen/demen_4" />
        </shape>
    </item>

    <item   android:right="1dip"
            android:bottom="2dip"
        >
        <inset
            android:drawable="@drawable/ic_white_corner_bg"
            android:insetTop="0dip"
            android:insetRight="0dip"
            android:insetBottom="0dip"
            android:insetLeft="1.5dip" />
    </item></layer-list>