如何在android中为drawable添加阴影效果

时间:2014-10-14 08:29:05

标签: android android-drawable xml-drawable shapedrawable

enter image description here我用drawable创建了聊天气泡,看起来很好,现在我想在drawable下面添加阴影效果,这样就可以产生3d效果。我不想使用9-pitch图像。我只是想知道如何在这个drawable上添加阴影效果。我的代码是

---- right_bubble_chat_drawable

 <?xml version="1.0" encoding="utf-8"?>
 <shape android:shape="rectangle" xmlns:android="http://schemas.android.com/apk/res/android">
        <corners android:radius="15dp" />

        <solid android:color="@color/chatrightbubbleColor" />

        <padding
            android:bottom="10dp"
            android:left="10dp"
            android:right="10dp"
            android:top="10dp" />

        <size
            android:height="@dimen/normal_button_height"
            android:width="@dimen/normal_button_width" />

---用于角落指针&#39; chat_laftarraow&#39;

  <?xml version="1.0" encoding="utf-8"?>
   <layer-list xmlns:android="http://schemas.android.com/apk/res/android" >
    <item >
     <rotate
        android:fromDegrees="90"
        android:toDegrees="-90"
        android:pivotX="50%"
        android:pivotY="50%" >
    <rotate
        android:fromDegrees="45"
        android:toDegrees="45"
        android:pivotX="-40%"
        android:pivotY="86%" >
        <shape
            android:shape="rectangle"  >
            <stroke android:color="#00aaef" android:width="1dp"/>
            <solid
                android:color="#00aaef"  />

        </shape>
       </rotate>
       </rotate>
      </item>
    </layer-list>

-------我正在使用它们

 <?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content"
android:layout_height="wrap_content" >
   <View
    android:id="@+id/left_chatArror"
    android:layout_width="20dp"
    android:layout_height="20dp"
    android:layout_alignParentRight="true"
    android:layout_marginTop="6dp"
    android:background="@drawable/chat_laftarraow"/>

<RelativeLayout

    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:maxWidth="220dp"
    android:layout_marginRight="-3dp"
    android:orientation="horizontal"
    android:layout_toLeftOf="@+id/left_chatArror"
    android:paddingBottom="5dp"
    android:paddingTop="5dp"
    android:background="@drawable/right_bubble_chat_drawable">

    </RelativeLayout>
  </RelativeLayout>

请建议我如何在气泡下方添加阴影效果,如下图所示

enter image description here

3 个答案:

答案 0 :(得分:26)

您可以尝试实施一个图层列表,作为LinearLayout的背景,并在其中添加view

引用this question的答案:

  

将background_with_shadow.xml文件添加到res/drawable。含有:

<?xml version="1.0" encoding="utf-8"?>
<layer-list xmlns:android="http://schemas.android.com/apk/res/android">
    <item >
        <shape 
            android:shape="rectangle">
        <solid android:color="@android:color/darker_gray" />
        <corners android:radius="5dp"/>
        </shape>
    </item>
    <item android:right="1dp" android:left="1dp" android:bottom="2dp">
        <shape 
            android:shape="rectangle">
        <solid android:color="@android:color/white"/>
        <corners android:radius="5dp"/>
        </shape>
    </item>
</layer-list>
     

然后在LINELayout中添加图层列表作为背景。

<LinearLayout
  android:layout_width="wrap_content"
  android:layout_height="wrap_content"
  android:background="@drawable/background_with_shadow"/>

<强> 修改

你可以创建seprate xml来创建像thsis这样的灰色图像:

---- right_bubble_shdw_chat_drawable

 <?xml version="1.0" encoding="utf-8"?>
 <shape android:shape="rectangle" xmlns:android="http://schemas.android.com/apk/res/android">
        <corners android:radius="15dp" />

        <solid android:color="@android:color/darker_gray" />

        <padding
            android:bottom="10dp"
            android:left="10dp"
            android:right="10dp"
            android:top="10dp" />

        <size
            android:height="@dimen/normal_button_height"
            android:width="@dimen/normal_button_width" />

---用于角落指针&#39; chat_laftarraow_shdw&#39;

  <?xml version="1.0" encoding="utf-8"?>
   <layer-list xmlns:android="http://schemas.android.com/apk/res/android" >
    <item >
     <rotate
        android:fromDegrees="90"
        android:toDegrees="-90"
        android:pivotX="50%"
        android:pivotY="50%" >
    <rotate
        android:fromDegrees="45"
        android:toDegrees="45"
        android:pivotX="-40%"
        android:pivotY="86%" >
        <shape
            android:shape="rectangle"  >
            <stroke android:color="@android:color/darker_gray" android:width="1dp"/>
            <solid
                android:color="@android:color/darker_gray"  />

        </shape>
       </rotate>
       </rotate>
      </item>
    </layer-list>

-------我正在使用它们

 <?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content"
android:layout_height="wrap_content" >
   <View
    android:id="@+id/left_chatArror"
    android:layout_width="20dp"
    android:layout_height="20dp"
    android:layout_alignParentRight="true"
    android:layout_marginTop="6dp"
    android:background="@drawable/chat_laftarraow"/>

<RelativeLayout

    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:maxWidth="220dp"
    android:layout_marginRight="-3dp"
    android:orientation="horizontal"
    android:layout_toLeftOf="@+id/left_chatArror"
    android:paddingBottom="5dp"
    android:paddingTop="5dp"
    android:background="@drawable/right_bubble_chat_drawable">

    </RelativeLayout>


   <View
    android:id="@+id/left_chatArrorShdw"
    android:layout_width="20dp"
    android:layout_height="20dp"
    android:layout_alignParentRight="true"
    android:layout_marginTop="15dp"
    android:background="@drawable/chat_laftarraow_shdw"/>

<RelativeLayout

    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:maxWidth="220dp"
    android:layout_marginRight="-3dp"
    android:orientation="horizontal"
    android:layout_toLeftOf="@+id/left_chatArror"
    android:paddingBottom="5dp"
    android:paddingTop="5dp"
    android:background="@drawable/right_bubble_shdw_chat_drawable">
  </RelativeLayout>

答案 1 :(得分:1)

将round_big_green_down.xml作为背景添加到您的职位。

<LinearLayout
       android:orientation="vertical"
       android:layout_width="match_parent"
       android:layout_height="match_parent"
       android:background="@drawable/**round_big_green_down**"
       android:paddingBottom="@dimen/button_margin_100"/>

res / drawable / round_big_green_down.xml

<?xml version="1.0" encoding="utf-8"?>
<layer-list xmlns:android="http://schemas.android.com/apk/res/android">
    <!-- Drop Shadow Stack -->
    <item>
        <shape>
            <padding android:top="1.5dp" />
            <solid android:color="#00000000" />
            <corners android:radius="@dimen/button_radius" />
        </shape>
    </item>
    <item>
        <shape>
            <padding android:top="1.5dp" />
            <solid android:color="#01000000" />
            <corners android:radius="@dimen/button_radius" />
        </shape>
    </item>
    <item>
        <shape>
            <padding android:top="1.5dp" />
            <solid android:color="#02000000" />
            <corners android:radius="@dimen/button_radius" />
        </shape>
    </item>
    <item>
        <shape>
            <padding android:top="1.5dp" />
            <solid android:color="#03000000" />
            <corners android:radius="@dimen/button_radius" />
        </shape>
    </item>
    <item>
        <shape>
            <padding android:top="1.5dp" />
            <solid android:color="#04000000" />
            <corners android:radius="@dimen/button_radius" />
        </shape>
    </item>
    <item>
        <shape>
            <padding android:top="1.5dp" />
            <solid android:color="#05000000" />
            <corners android:radius="@dimen/button_radius" />
        </shape>
    </item>
    <item>
        <shape>
            <padding android:top="1.5dp" />
            <solid android:color="#06000000" />
            <corners android:radius="@dimen/button_radius" />
        </shape>
    </item>
    <item>
        <shape>
            <padding android:top="1.5dp" />
            <solid android:color="#06000000" />
            <corners android:radius="@dimen/button_radius" />
        </shape>
    </item>
    <item>
        <shape>
            <padding android:top="1.5dp" />
            <solid android:color="#06000000" />
            <corners android:radius="@dimen/button_radius" />
        </shape>
    </item>
    <item>
        <shape>
            <padding android:top="1.5dp" />
            <solid android:color="#06000000" />
            <corners android:radius="@dimen/button_radius" />
        </shape>
    </item>

    <!-- White Top color -->
    <item android:top="@dimen/button_margin_5">
        <shape >
            <gradient
                android:angle="90"
                android:endColor="@color/primary_color_50"
                android:startColor="@color/primary_color_500" />
            <corners
                android:topLeftRadius="@dimen/button_radius"
                android:topRightRadius="@dimen/button_radius"/>
        </shape>
    </item>
</layer-list>

res / values / dimens /

<dimen name="button_radius">25dp</dimen>

答案 2 :(得分:0)

我正在使用此drawable / shadow.xml文件

<!-- Drop Shadow Stack -->
<item>
    <shape>
        <padding
            android:bottom="1dp"
            android:left="1dp"
            android:right="1dp"
            android:top="1dp" />

        <solid android:color="#00CCCCCC" />

        <corners android:radius="3dp" />
    </shape>
</item>
<item>
    <shape>
        <padding
            android:bottom="1dp"
            android:left="1dp"
            android:right="1dp"
            android:top="1dp" />

        <solid android:color="#10CCCCCC" />

        <corners android:radius="3dp" />
    </shape>
</item>
<item>
    <shape>
        <padding
            android:bottom="1dp"
            android:left="1dp"
            android:right="1dp"
            android:top="1dp" />

        <solid android:color="#20CCCCCC" />

        <corners android:radius="3dp" />
    </shape>
</item>

<!-- Background -->
<item>
    <shape>
        <solid android:color="@android:color/white" />

        <corners android:radius="3dp" />
    </shape>
</item>