android - 为按钮添加彩色阴影

标签 android shadow

我需要使用“来自 zeplin”的这些属性向按钮添加阴影:

enter image description here

这是设计

enter image description here

我试过这段代码

<Button
        android:id="@+id/btn_sign_in"
        android:layout_width="match_parent"
        android:layout_height="48dp"
        android:layout_marginTop="35dp"
        android:background="@drawable/auth_button_shape"
        android:shadowColor="#41ff4800"
        android:shadowDx="0"
        android:shadowDy="8"
        android:text="@string/sign_in"
        android:textAllCaps="true"
        android:textColor="@android:color/white"
        android:textSize="14sp" />

auth_button_shape

<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="rectangle">
<solid android:color="#ff7c44" />
<corners android:radius="100dp" />
</shape>

但没有用,其他属性“Blur”和“Spread”如何为按钮设置它们。

谢谢。

最佳答案

多亏了 Taras,我有了解决方案。你可以用这个 library用于为按钮创建彩色阴影。只需将您的 View 元素放在阴影布局中。当然基本布局是ConstraintLayout。 例如一些代码

 <com.gigamole.library.ShadowLayout
    android:id="@+id/shadowLayout"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    app:layout_constraintEnd_toEndOf="parent"
    app:layout_constraintStart_toStartOf="parent"
    app:layout_constraintTop_toTopOf="parent"
    app:sl_shadow_angle="90"
    app:sl_shadow_color="@color/light_orange_color"
    app:sl_shadow_distance="2dp"
    app:sl_shadow_radius="7dp"
    app:sl_shadowed="true">

    <ImageView
        android:id="@+id/ivYourImageName"
        android:layout_width="144dp"
        android:layout_height="44dp"
        android:background="@drawable/button_shape"
        android:foregroundGravity="center"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintHorizontal_bias="0.0"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent"
        app:layout_constraintVertical_bias="1.0" />

</com.gigamole.library.ShadowLayout>

按钮形状:

<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
    <item>
        <shape android:shape="rectangle">
            <corners android:radius="2dp" />
            <gradient android:angle="180" android:endColor="#ffc349" android:startColor="#ff8006" />
        </shape>
    </item>
</selector>

结果:

关于android - 为按钮添加彩色阴影,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51265584/

相关文章:

android - 收到短信没有通知

android - 我可以仅更新一个.so文件的OpenSSL版本吗

android - 如何重建以前的 Activity ?

c++ - 带阴影的聚光灯变成方形

c++ - 点光源阴影贴图和光照着色器

android - 我如何在我的代码中插入回调代码

Android EditText 结合 InputFilter vs TextWatcher

css - 盒子阴影对于 s​​afari 和 chrome 看起来不一样

Javascript在特定时间改变文本阴影方向

android - 捕获图像时TextView阴影被破坏