如何创建按钮的可绘制阴影?

问题描述 投票:0回答:1

我已经尝试过使用9-patch可绘制图像作为图层列表,并且还使用了渐变,但是在两种情况下我都无法获得准确的结果,我们如何设计阴影可绘制图像?

enter image description here

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

    <item
        android:left="@dimen/_10sdp"
        android:right="@dimen/_10sdp">
        <shape android:shape="rectangle">

            <corners
                android:bottomLeftRadius="@dimen/_4sdp"
                android:bottomRightRadius="@dimen/_4sdp"
                android:topLeftRadius="@dimen/_50sdp"
                android:topRightRadius="50sp"
                />

            <gradient
                android:angle="90"
                android:startColor="#0D4c4c4c"
                android:endColor="#0D4c4c4c"
                android:centerColor="#334c4c4c"
                android:type="linear" />

        </shape>

    </item>
    <item android:bottom="@dimen/_7sdp">
        <shape android:shape="rectangle">
            <solid android:color="#b9b9b9" />
        </shape>
    </item>
</layer-list>
android gradient drawable nine-patch
1个回答
0
投票

您可以在自己的xml文件:android:elevation="6dp"


0
投票
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_pressed="true" android:state_enabled="true">
    <set>
        <objectAnimator android:propertyName="translationZ"
                        android:duration="@integer/button_pressed_animation_duration"
                        android:valueTo="@dimen/button_pressed_z_material"
                        android:valueType="floatType"/>
        <objectAnimator android:propertyName="elevation"
                        android:duration="0"
                        android:valueTo="@dimen/button_elevation_material"
                        android:valueType="floatType"/>
    </set>
</item>
<!-- base state -->
<item android:state_enabled="true">
    <set>
        <objectAnimator android:propertyName="translationZ"
                        android:duration="@integer/button_pressed_animation_duration"
                        android:valueTo="0"
                        android:startDelay="@integer/button_pressed_animation_delay"
                        android:valueType="floatType"/>
        <objectAnimator android:propertyName="elevation"
                        android:duration="0"
                        android:valueTo="@dimen/button_elevation_material"
                        android:valueType="floatType" />
    </set>
</item>
<item>
    <set>
        <objectAnimator android:propertyName="translationZ"
                        android:duration="0"
                        android:valueTo="0"
                        android:valueType="floatType"/>
        <objectAnimator android:propertyName="elevation"
                        android:duration="0"
                        android:valueTo="0"
                        android:valueType="floatType"/>
    </set>
</item>

在xml中与此一起使用:

android:stateListAnimator="@anim/button_shadow_animator"

此功能适用于Android 5.0及以上版本

© www.soinside.com 2019 - 2024. All rights reserved.