如何在xml中的纯色形状后面使用可绘制对象添加阴影? [英] How to add shadow using a drawable behind solid color shape in xml?

查看:121
本文介绍了如何在xml中的纯色形状后面使用可绘制对象添加阴影?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个.png图像,它是圆形的,只是一个阴影.我所拥有的是一个ImageButton,它具有android:src属性的图像.对于它的背景,我可以在可绘制文件中有一个xml文件.我现在想要的是将具有png的阴影图像放在另一个实体形状之后-> android:shape ="oval".

I have a .png image which is a circular shape and is just a shadow. What I have is an ImageButton which has an image for it's android:src property. For it's background I can have an xml file in drawable file. What I want now is to have the shadow image which is a png to go behind another solid shape -> android:shape="oval".

<?xml version="1.0" encoding="utf-8"?>
<layer-list xmlns:android="http://schemas.android.com/apk/res/android">

    <item android:drawable="@drawable/shadowimg" />

    <item>
        <shape android:shape="oval">

            <solid
                android:color="#d63a33"/>

            <size
                android:width="70dp"
                android:height="70dp"/>
        </shape>
    </item>
</layer-list>

正如您在代码中看到的那样,我正在添加一个带有android:drawable属性的带有阴影图像的项目.但是,这不会让阴影显示出来.它只是显示红色圆圈形状.但是我想要红色实心圆形形状后面的阴影图像.根据我对层列表的了解,最上面的项目排在后面,最下面的项目排在第一.

As you see in the code I am adding an item with the shadow image with the android:drawable property. However this does not let the shadow show. It simply shows the red circle shape. But I want the shadow image behind the solid red circle shape. From my tiny knowledge of layer-list the top item goes in the back and the one on bottom comes first.

更新:将图像阴影移到底部后,我得到了效果,但是红色实心圆形的大小太大,并且更改大小不会更改大小.

UPDATE: After moving the image shadow to bottom I got the effect however the size of the solid red circle shape is too big and changing the size doesn't change the size.

更新的代码:

<?xml version="1.0" encoding="utf-8"?>
<layer-list xmlns:android="http://schemas.android.com/apk/res/android">

    <item>
        <shape android:shape="oval">

            <solid
                android:color="#d63a33"/>

            <size
                android:width="5dp"
                android:height="5dp"/>
        </shape>
    </item>
    <item android:drawable="@drawable/shadowimg" />
</layer-list>

****更新2:**** 我注意到,当我删除<item android:drawable="@drawable/shadowimg" />时,我可以更改实心圆形的大小,但是当我添加它时,它默认为更大的大小.

**** UPDATE 2: **** I noticed when I remove <item android:drawable="@drawable/shadowimg" /> I can change size of solid circle shape but when I add this it defaults to a bigger size.

会发生这种情况:

如果有人需要,这是阴影图像:

Here's the shadow image if anyone needs:

推荐答案

实际上,问题出在您正在使用的阴影中,该阴影本身留下了一些填充.因此我们可以做的是,使用笔触在第一项中填充纯色空间.

Actually the issue is in the shadow you are using, the shadow itself leaves some padding. so what we can do is, leave some space in the solid color we are filling in the first item by using stroke.

使用此代码:

<?xml version="1.0" encoding="utf-8"?>
<layer-list xmlns:android="http://schemas.android.com/apk/res/android" >
    <item>
        <shape android:shape="oval" >
            <solid android:color="#d63a33" />

            <stroke
                android:width="7dp"
                android:color="#00000000" />
        </shape>
    </item>
    <item android:drawable="@drawable/shadowimg"/>
</layer-list>

然后使用高度为30dp,宽度为30dp的

and then use with 30dp of height and 30dp of width, like

<TextView
    android:id="@+id/titleText"
    android:layout_width="30dp"
    android:layout_height="30dp"
    android:layout_centerVertical="true"
    android:layout_marginLeft="16dp"
    android:background="@drawable/circle_white"
    android:fontFamily="sans-serif-thin"
    android:gravity="center"
    android:text="Hi"
    android:textColor="#FF000000"
    android:textSize="16sp" />

如果要增加视图的高度和宽度,也只需增加笔触的宽度.

if you want to increase the height and width of the view, simply increase the width of the stroke too..

这篇关于如何在xml中的纯色形状后面使用可绘制对象添加阴影?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆