Android 是否可以使用CardView为浮动操作按钮制作阴影?

Android 是否可以使用CardView为浮动操作按钮制作阴影?,android,shadow,android-cardview,floating-action-button,Android,Shadow,Android Cardview,Floating Action Button,我知道cardwiew不是为这个设计的,但理论上,如果cardCornerRadius=view\u size/2应该会产生一个圆圈 我错过什么了吗 从技术上讲似乎是可能的,但实际上并非如此 如果你想像那样使用按钮,我建议你使用这个库。它工作得很好 不建议这样做,这与模式背道而驰卡片视图用于列表。您可以使用ImageView创建自己的浮动操作按钮和阴影。 activity\u layout.xml: <LinearLayout xmlns:android="http://sche

我知道
cardwiew
不是为这个设计的,但理论上,如果
cardCornerRadius=view\u size/2
应该会产生一个圆圈

我错过什么了吗


从技术上讲似乎是可能的,但实际上并非如此


如果你想像那样使用按钮,我建议你使用这个库。它工作得很好

不建议这样做,这与模式背道而驰<代码>卡片视图用于列表。您可以使用
ImageView
创建自己的
浮动操作按钮和阴影。
activity\u layout.xml

<LinearLayout 
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    ....
    <ImageButton
            android:id="@+id/add_courses_button"
            android:layout_width="64dp
            android:layout_height="64dp"

            android:layout_alignParentRight="true"
            android:layout_alignParentEnd="true"
            android:layout_alignParentBottom="true"

            android:layout_marginBottom="16dp"
            android:layout_marginRight="16dp"

            android:background="@drawable/fab"
            android:tint="@color/white"
            android:src="@drawable/add_image"/>
    ...
</LinearLayout>
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
    <item android:state_pressed="true">
        <layer-list>
            <!-- Shadow -->
            <item android:top="1dp" android:right="1dp">
                <layer-list>
                    <item>
                        <shape android:shape="oval">
                            <solid android:color="#08000000"/>
                            <padding
                                android:bottom="3px"
                                android:left="3px"
                                android:right="3px"
                                android:top="3px"
                                />
                        </shape>
                    </item>
                    <item>
                        <shape android:shape="oval">
                            <solid android:color="#09000000"/>
                            <padding
                                android:bottom="2px"
                                android:left="2px"
                                android:right="2px"
                                android:top="2px"
                                />
                        </shape>
                    </item>
                    <item>
                        <shape android:shape="oval">
                            <solid android:color="#10000000"/>
                            <padding
                                android:bottom="2px"
                                android:left="2px"
                                android:right="2px"
                                android:top="2px"
                                />
                        </shape>
                    </item>
                    <item>
                        <shape android:shape="oval">
                            <solid android:color="#11000000"/>
                            <padding
                                android:bottom="1px"
                                android:left="1px"
                                android:right="1px"
                                android:top="1px"
                                />
                        </shape>
                    </item>
                    <item>
                        <shape android:shape="oval">
                            <solid android:color="#12000000"/>
                            <padding
                                android:bottom="1px"
                                android:left="1px"
                                android:right="1px"
                                android:top="1px"
                                />
                        </shape>
                    </item>
                    <item>
                        <shape android:shape="oval">
                            <solid android:color="#13000000"/>
                            <padding
                                android:bottom="1px"
                                android:left="1px"
                                android:right="1px"
                                android:top="1px"
                                />
                        </shape>
                    </item>
                    <item>
                        <shape android:shape="oval">
                            <solid android:color="#14000000"/>
                            <padding
                                android:bottom="1px"
                                android:left="1px"
                                android:right="1px"
                                android:top="1px"
                                />
                        </shape>
                    </item>
                    <item>
                        <shape android:shape="oval">
                            <solid android:color="#15000000"/>
                            <padding
                                android:bottom="1px"
                                android:left="1px"
                                android:right="1px"
                                android:top="1px"
                                />
                        </shape>
                    </item>
                    <item>
                        <shape android:shape="oval">
                            <solid android:color="#16000000"/>
                            <padding
                                android:bottom="1px"
                                android:left="1px"
                                android:right="1px"
                                android:top="1px"
                                />
                        </shape>
                    </item>
                </layer-list>
            </item>

            <!-- Blue button pressed -->
            <item>
                <shape android:shape="oval">
                    <solid android:color="#0099cc"/>
                </shape>
            </item>
        </layer-list>
    </item>

    <item android:state_enabled="true">

        <layer-list>
            <!-- Shadow -->
            <item android:top="1dp" android:right="1dp">
                <layer-list>
                    <item>
                        <shape android:shape="oval">
                            <solid android:color="#08000000"/>
                            <padding
                                android:bottom="3px"
                                android:left="3px"
                                android:right="3px"
                                android:top="3px"
                                />
                        </shape>
                    </item>
                    <item>
                        <shape android:shape="oval">
                            <solid android:color="#09000000"/>
                            <padding
                                android:bottom="2px"
                                android:left="2px"
                                android:right="2px"
                                android:top="2px"
                                />
                        </shape>
                    </item>
                    <item>
                        <shape android:shape="oval">
                            <solid android:color="#10000000"/>
                            <padding
                                android:bottom="2px"
                                android:left="2px"
                                android:right="2px"
                                android:top="2px"
                                />
                        </shape>
                    </item>
                    <item>
                        <shape android:shape="oval">
                            <solid android:color="#11000000"/>
                            <padding
                                android:bottom="1px"
                                android:left="1px"
                                android:right="1px"
                                android:top="1px"
                                />
                        </shape>
                    </item>
                    <item>
                        <shape android:shape="oval">
                            <solid android:color="#12000000"/>
                            <padding
                                android:bottom="1px"
                                android:left="1px"
                                android:right="1px"
                                android:top="1px"
                                />
                        </shape>
                    </item>
                    <item>
                        <shape android:shape="oval">
                            <solid android:color="#13000000"/>
                            <padding
                                android:bottom="1px"
                                android:left="1px"
                                android:right="1px"
                                android:top="1px"
                                />
                        </shape>
                    </item>
                    <item>
                        <shape android:shape="oval">
                            <solid android:color="#14000000"/>
                            <padding
                                android:bottom="1px"
                                android:left="1px"
                                android:right="1px"
                                android:top="1px"
                                />
                        </shape>
                    </item>
                    <item>
                        <shape android:shape="oval">
                            <solid android:color="#15000000"/>
                            <padding
                                android:bottom="1px"
                                android:left="1px"
                                android:right="1px"
                                android:top="1px"
                                />
                        </shape>
                    </item>
                    <item>
                        <shape android:shape="oval">
                            <solid android:color="#16000000"/>
                            <padding
                                android:bottom="1px"
                                android:left="1px"
                                android:right="1px"
                                android:top="1px"
                                />
                        </shape>
                    </item>
                </layer-list>
            </item>

            <!-- Blue button -->
            <item>
                <shape android:shape="oval">
                    <solid android:color="#33b5e5"/>
                </shape>
            </item>
        </layer-list>

    </item>

</selector>
结果如下:

绘制真实的动画阴影并不是那么困难,你可以尝试在任何Android上,甚至Froyo上实现棒棒糖的效果。下面是它的工作原理:

  • 使用LightingColorFilter(0,0)将视图绘制到屏幕外位图
  • 使用ScriptIntrisInBlur模糊该位图。将模糊半径设置为高程值
  • 在浮动操作按钮下方绘制模糊位图
  • 有关实施示例,请参见:

    • 阴影生成器,模糊视图:
    • 支持阴影的线性布局,请选中drawChild()方法:

    基本上,碳库尽量模仿棒棒糖的效果。我知道导入这样的库可能会有点过头,但如果您希望只复制代码中感兴趣的部分,并在应用程序中使用它。

    尝试使用app:cardPreventCornerOverlap=“false”查看您的卡。不确定它是否能按您的意愿工作@德米特罗Danylyk@Pooja正如你在屏幕截图上看到的,这个属性已经设置好了。谢谢你的回答,但是这样的按钮很难维护。这个阴影在高像素密度的设备上看起来可以吗?我还没试过,但我只是想知道如果我用dp来代替它是否会起作用。我使用的是更简单的解决方案
    setShadowLayer
    FYI:而不是
    Bitmap.Config.ARGB_8888
    use
    Bitmap.Config.ALPHA_8
    它会将生成的
    Bitmap
    的大小减小3倍。setShadowLayer在硬件加速时效果不佳,这对我来说是不可接受的。感谢ALPHA_8 config的提示-我必须检查:)
    setShadowLayer
    如果在内部视图
    Canvas
    中使用它,则它不起作用。如果您是从
    Bitmap
    创建
    Canvas
    对象,它会工作,因为由简单位图支持的画布不是硬件加速的。这意味着阴影也不是硬件加速的。我想知道该解决方案的性能如何。