gpt4 book ai didi

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

转载 作者:IT王子 更新时间:2023-10-28 23:30:28 28 4
gpt4 key购买 nike

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

--- 对于角指针'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>

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

enter image description here

最佳答案

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

引用 this question 的回答:

Add background_with_shadow.xml file to res/drawable. Containing:

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

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

编辑

您可以创建单独的 xml 来创建像这样的灰色图像:

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

  <?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 - 如何在android中的drawable上添加阴影效果,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26356060/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com