在 Android 中以 XML 定义形状时,是否有与 setShadowLayer 等效的方法?
如果我通过代码绘制圆形矩形,我可以使用 setShadowLayer 为形状绘制阴影。在 XML 中定义形状时是否有等效的?
以下示例将圆形矩形背景绘制为形状。我需要添加什么才能将阴影添加到形状中?甚至可以使用 XML 吗?
shape_test.xml
<?xml version="1.0" encoding="UTF-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android">
<solid android:color="#8067BF6A"/>
<stroke android:width="3dp" android:color="#80000000" />
<padding android:left="1dp"
android:top="1dp"
android:right="1dp"
android:bottom="1dp" />
<corners android:bottomRightRadius="7dp"
android:bottomLeftRadius="7dp"
android:topLeftRadius="7dp"
android:topRightRadius="7dp"/>
</shape>
main.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:padding="10dp"
android:background="#ffdddddd"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
>
<Button android:background="@drawable/shape_test"
android:padding="5dp"
android:textStyle="bold"
android:textSize="28sp"
android:text="Hello"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
</LinearLayout>
If I draw a round rect shape by code I can use setShadowLayer to get a shadow drawn for the shape. Is there an equivalent when defining shapes in XML?
The following example draws a round rect background to a shape. What would I need to add to get a shadow added to the shape? Is it even possible using XML?
shape_test.xml
<?xml version="1.0" encoding="UTF-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android">
<solid android:color="#8067BF6A"/>
<stroke android:width="3dp" android:color="#80000000" />
<padding android:left="1dp"
android:top="1dp"
android:right="1dp"
android:bottom="1dp" />
<corners android:bottomRightRadius="7dp"
android:bottomLeftRadius="7dp"
android:topLeftRadius="7dp"
android:topRightRadius="7dp"/>
</shape>
main.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:padding="10dp"
android:background="#ffdddddd"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
>
<Button android:background="@drawable/shape_test"
android:padding="5dp"
android:textStyle="bold"
android:textSize="28sp"
android:text="Hello"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
</LinearLayout>
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
更多
发布评论
评论(1)
据我所知,使用 XML 还没有真正的方法可以做到这一点。我看到了一些建议,在物品后面制作第二个相同形状的盒子,然后用黑色填充它,但我认为这不是一个好的解决方案。一段时间以来,我一直在尝试寻找一种方法来自己做到这一点。
如果有帮助,这里有一个链接,指向我发布的类似问题,以及一些代码。我让它适用于一些图像,但有时似乎仍然无法找到 Alpha 通道。基本上,我已经重写了 ImageView 并将其放入
onDraw()
方法中:不过,这只是测试,因此显然很多参数必须更加通用。虽然我最近没有太多时间来研究它,但也许这会帮助你找到答案。
Using XML, there's no real way to do this that I know of. I've seen some suggestions of making a second box of the same shape behind the item, and just filling it with black, but I don't feel that's a good solution. I've been trying to find a way to do this myself for a while.
If it helps, here's a link to a similar question I posted, along with some code. I got it working for some images, but it still seems to have trouble finding the alpha channel sometimes. Basically, I've overridden ImageView and put this in the
onDraw()
method:That was just testing, though, so obviously a lot of the parameters would have to be more generic. I haven't had a lot of time to work on it lately though, but maybe this will help you find your answer.