简体   繁体   English

如何填充波纹效果

[英]How to have padding for ripple effect

Previously, I'm using StateDrawable to achieve custom card view with shadow effect - https://stackoverflow.com/a/33829309/72437 .以前,我使用StateDrawable来实现带有阴影效果的自定义卡片视图 - https://stackoverflow.com/a/33829309/72437 This is due to the limitation of CardView , of not able to accept selector in its cardBackgroundColor这是由于CardView的限制,无法在其cardBackgroundColor接受选择器

Now, I want to add ripple effect.现在,我想添加涟漪效应。 I use the following XML.我使用以下 XML。

<?xml version="1.0" encoding="utf-8"?>
<ripple xmlns:android="http://schemas.android.com/apk/res/android"
    android:color="#00ff00">
    <item android:drawable="@drawable/statelist_item_background"/>
</ripple>

However, this is having side effect where the green ripple extends till it touches shadow region.然而,这会产生副作用,即绿色波纹一直延伸到触及阴影区域。

I want to avoid the green ripple from touching shadow region.我想避免接触阴影区域的绿色波纹。 I try to add in padding information.我尝试添加填充信息。

<?xml version="1.0" encoding="utf-8"?>
<ripple xmlns:android="http://schemas.android.com/apk/res/android"
    android:color="#00ff00">
    <item>
        <shape android:shape="rectangle">
            <padding android:top="10dp" android:right="10dp" android:bottom="10dp" android:left="10dp" />
        </shape>
    </item>

    <item android:drawable="@drawable/statelist_item_background"/>
</ripple>

在此处输入图片说明

However, it makes no different.然而,它没有什么不同。 Adding padding information within ripple tag makes no difference.ripple标签中添加填充信息没有区别。

May I know how can I have padding for ripple effect?我可以知道如何填充波纹效果吗?

You can use the inset tag to define the padding you need.您可以使用inset标签来定义您需要的填充。 There is an inset attribute to define global padding or 4 attributes for each directions.有一个inset属性来定义全局填充或每个方向的 4 个属性。

<?xml version="1.0" encoding="utf-8"?>
<inset xmlns:android="http://schemas.android.com/apk/res/android"
    android:inset="10dp">
    <ripple
        android:color="#00ff00">
        <item android:drawable="@drawable/statelist_item_background"/>
    </ripple>
</inset>

First create the drawable file (ex: bg_ripple.xml)首先创建可绘制文件(例如:bg_ripple.xml)

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

    <ripple android:color="@color/gray_light">    

        <item android:id="@android:id/mask">
            <shape android:shape="rectangle">
                <!-- this color does not matter, it's a mask shape -->
                <solid android:color="@color/blue" />
                <corners android:radius="@dimen/button_corner_radius" />
            </shape>
        </item>

        <item android:id="@android:id/background">
            <shape android:shape="rectangle">
                <corners android:radius="@dimen/button_corner_radius" />
                <stroke
                    android:width="2dp"
                    android:color="@color/red"
                    />
                <solid android:color="@color/white" />
            </shape>
        </item>

    </ripple>
</inset>

Second add it as a background to the Button其次将它作为背景添加到按钮

<Button        
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="@string/click_me"
    android:background="@drawable/bg_ripple"
    />

You should add this to your CarView:您应该将此添加到您的 CarView:

<CardView
    ...
    style="@style/MyCardViewStyle"
    android:foreground="?attr/selectableItemBackground">
</CardView>

And then, add this in your style:然后,在你的风格中添加:

<style name="MyCardViewStyle">
    <item name="colorControlHighlight">@color/my_awesome_color</item>
</style>

It will give you the awesome color you want to your ripple effect, without having to create yourself your ripple, and the bounds will automatically be detected so you won't need to find if it's even possible to add a padding to the ripple.它将为您的涟漪效果提供您想要的令人敬畏的颜色,而无需自己创建涟漪,并且会自动检测边界,因此您无需查找是否可以为涟漪添加填充。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM