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

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

本文介绍了如何在android中的drawable上添加阴影效果的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我创建了带有可绘制外观的聊天气泡,看起来不错,现在我想添加阴影低于可绘制对象的效果,因此可以产生3d效果.我不想使用9间距图像.我只想知道如何在此可绘制对象上添加阴影效果.我的代码是

I have created chat bubble with drawable which looks fine, now I want to add shadow effect below the drawable so it make 3d effect.I don't want to use 9-pitch image. I just want to know how i can add shadow effect on this drawable. my code is

---- right_bubble_chat_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" />

---用于拐角指针'chat_laftarraow'

--- for corner pointer 'chat_laftarraow'

  <?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>

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

Please suggest me how can I add shadow effect below the bubble as in the image below

推荐答案

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

You can try by implementing a layer-list that will act as the background for the LinearLayout and add your view inside this.

引用这个问题的答案:

<?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>

然后将图层列表添加为LinearLayout中的背景.

Then add the the layer-list as background in your LinearLayout.

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

EDIT

EDIT

您可以创建单独的xml来创建类似于thsis的灰度图像:

You can create seprate xml for creating gray image like thsis:

---- right_bubble_shdw_chat_drawable

----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" />

---用于拐角指针'chat_laftarraow_shdw'

--- for corner pointer 'chat_laftarraow_shdw'

  <?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>

这篇关于如何在android中的drawable上添加阴影效果的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-02 02:22