如何在按钮上添加模糊的阴影? [英] How to add a blurred drop shadow to a button?
问题描述
我需要在按钮上添加模糊的阴影:
I need to add blurred drop shadow to my button:
我试图用可绘制的图层列表xml创建背景,但看起来不像是模糊的.
I tried to create background with layer-list xml drawable, but it not looks like blur.
<?xml version="1.0" encoding="utf-8"?>
<layer-list xmlns:android="http://schemas.android.com/apk/res/android">
<!-- Drop Shadow Stack -->
<item>
<shape>
<corners android:radius="45dp" />
<padding
android:bottom="2dp"
android:left="1dp"
android:right="1dp"
android:top="1dp" />
<stroke
android:width="6dp"
android:color="#007879E8" />
</shape>
</item>
////// 10 more items
<item>
<shape>
<corners android:radius="45dp" />
<padding
android:bottom="2dp"
android:left="1dp"
android:right="1dp"
android:top="1dp" />
<stroke
android:width="6dp"
android:color="#177879E8" />
</shape>
</item>
<!-- Background -->
<item>
<shape>
<corners android:radius="45dp" />
<stroke
android:width="2dp"
android:color="@color/main_purple_text_color" />
</shape>
</item>
</layer-list>
我也尝试在具有模糊png的按钮后面使用背景元素,但它占用了很多资源,并且我无法创建选择器来更改悬停或单击时的背景.
Also i tried to use background element behind button with blured png but it uses to many resources and i cannot create selector to change background on hover or click.
我需要按钮具有单个背景文件,并使用选择器更改悬停/单击时的模糊和渐变. 有什么想法如何使用Android SDK达到这种效果吗?
I need to have single background file for button and use selector to change blur and gradient on hover/click. Any ideas how to achieve such effect with Android SDK ?
更新1
感谢大家的回答,但我不是在问如何创建渐变.我已经做到了.我需要创建模糊的阴影.
Thanks everybody for answers, but I'm not asking how to create gradient. I've already done this. I need to create blurred drop-shadow.
推荐答案
Well i found the solution: we need to create 9.png with blurred drop-shadow via this generator and pass it to drawable layer-list that contain button background gradient:
<?xml version="1.0" encoding="utf-8"?>
<layer-list xmlns:android="http://schemas.android.com/apk/res/android" >
<item android:drawable="@drawable/blurred_9_path_png" />
<item>
<shape android:shape="rectangle" android:padding="10dp">
<corners android:radius="45dp" />
<gradient
android:angle="45"
android:endColor="@color/facebook_btn_fill_grad2"
android:startColor="@color/facebook_btn_fill_grad1"
android:type="linear" />
<padding
android:bottom="0dp"
android:left="0dp"
android:right="0dp"
android:top="0dp" />
</shape>
</item>
</layer-list>
之后,我们可以使用具有不同9.path模糊阴影的可绘制对象来创建选择器.
After that we can use such drawables with different 9.path blurred shadow to create selector.
这篇关于如何在按钮上添加模糊的阴影?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!