Android L波纹触控效果如何?

时间:2014-07-02 09:23:45

标签: android android-5.0-lollipop

我有一个带有高程的圆角矩形,可以投射阴影,就像这里的例子一样:http://developer.android.com/preview/material/views-shadows.html#shadows

这是我的形状:

<shape xmlns:android="http://schemas.android.com/apk/res/android"
    android:shape="rectangle">
    <solid android:color="@android:color/white" />
    <corners android:radius="6dp" />
</shape>

我希望获得其他所有内容的漂亮“涟漪”触摸效果,但是如果将其设置为视图的背景,则不会给出触摸反馈。形状保持白色。

所以,我把它变成了layer-list

<layer-list xmlns:android="http://schemas.android.com/apk/res/android">
    <item>
        <shape xmlns:android="http://schemas.android.com/apk/res/android"
            android:shape="rectangle">
            <solid android:color="@android:color/white" />
            <corners android:radius="6dp" />
        </shape>
    </item>
    <item android:drawable="?android:selectableItemBackground" />
</layer-list>

现在我点击它时会得到很好的触摸反馈,但是有问题。

圆角矩形的轮廓丢失了。它仍然可以将白色圆角矩形绘制得很精细,但阴影会被渲染为非圆角矩形(方形边缘),并且波纹会一直延伸到角落之外:

Bad corners

在这里看起来并不太可怕,但在设备上它非常丑陋且令人不快。

有没有办法解决这个问题?第一个链接的Outline部分似乎是我想要的,但我无法弄清楚如何实现它。

5 个答案:

答案 0 :(得分:75)

尝试一下:

ripple.xml

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

background.xml

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

或许这篇文章可以帮助您:Android L FAB Button shadow

我在那里解释过,如何实现新的FAB按钮,我也使用了大纲。

答案 1 :(得分:2)

click here for complete source code

Create button.xml in drawable folder

<selector xmlns:android="http://schemas.android.com/apk/res/android">
    <item android:state_pressed="true" android:drawable="@color/colorPrimaryDark"/>
    <item android:drawable="@color/colorPrimary"/>
</selector>

Create button.xml in drawable-v21 folder

<ripple xmlns:android="http://schemas.android.com/apk/res/android"
    android:color="?android:colorControlHighlight">
    <item android:drawable="@color/colorPrimary" />
</ripple>

you can set button.xml as background of your view.

<TextView android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_marginTop="8dp"
    android:background="@drawable/button"
    android:clickable="true"
    android:gravity="center"
    android:padding="16dp"
    android:text="Android Ripple Effect Custom"
    android:textColor="#fff"
    android:textSize="18sp" />

if you want custom color for ripple effect instead of default gray, then you can archive it by adding colorControlHighlight in your style.

<resources>

    <!-- Base application theme. -->
    <style name="AppTheme" parent="Theme.AppCompat.Light.DarkActionBar">
        <!-- Customize your theme here. -->
        <item name="colorPrimary">@color/colorPrimary</item>
        <item name="colorPrimaryDark">@color/colorPrimaryDark</item>
        <item name="colorAccent">@color/colorAccent</item>
        <item name="colorControlHighlight">@color/colorPrimaryDark</item>
    </style>

</resources>

答案 2 :(得分:2)

<RelativeLayout
 android:foreground="?android:attr/selectableItemBackground">

</RelativeLayout>

答案 3 :(得分:1)

android studio中的

波纹触摸效果按钮<​​/ p>

       <com.xgc1986.ripplebutton.widget.RippleButton
        android:id="@+id/Summit"
        android:layout_width="260dp"
        android:layout_height="50dp"
        android:text="Login"
        android:textColor="@color/white"
        android:textStyle="bold"
        app:buttonColor="@color/Button"
        app:rippleColor="@color/white" />

如需更多参考,请点击此处http://androiddhina.blogspot.in/2015/04/android-ripple-touch-effect-example.html

答案 4 :(得分:1)

这是波纹和图层列表。

<ripple xmlns:android="http://schemas.android.com/apk/res/android"
android:color="@color/colorRipple">
<item>
    <layer-list>
        <item>
            <shape android:shape="rectangle">
                <solid android:color="#349F37" />
                <corners android:radius="6dp" />
                <stroke
                    android:width="2dp"
                    android:color="#50E751" />
            </shape>
        </item>
    </layer-list>
</item>

</ripple>