Android L 波纹触摸对形状的影响?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/24527402/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-20 08:22:15  来源:igfitidea点击:

Android L ripple touch effect on shape?

androidandroid-5.0-lollipop

提问by Steven Schoen

I have a rounded rectangle with elevation that casts a shadow, just like in the example here: http://developer.android.com/preview/material/views-shadows.html#shadows

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

Here is my shape:

这是我的形状:

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

I want to get the nice "ripple" touch effect that everything else has, but when that's set as the view's background, no touch feedback is given. The shape stays white.

我想获得其他所有东西都具有的漂亮的“涟漪”触摸效果,但是当它设置为视图的背景时,不会给出触摸反馈。形状保持白色。

So, I made it into a layer-list:

所以,我把它变成了一个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>

Now I get nice touch feedback when I tap it, but there's a problem.

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

The outline of the rounded rectangle shape was lost. It still draws the white rounded rectangle fine, but the shadow is cast as a non-rounded rectangle (square edges), and the ripple goes all the way out past the corners:

圆角矩形的轮廓丢失了。它仍然可以很好地绘制白色圆角矩形,但阴影被投射为非圆角矩形(方形边缘),并且波纹一直穿过角落:

Bad corners

坏角

It doesn't look too horrible on here, but on the device it's pretty ugly and off-putting.

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

Is there a way to fix this? The Outlinesection of the first link seems to be what I want, but I can't figure out how to implement it.

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

回答by yannickpulver

Try that one:

试试那个:

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>

Or maybe this post will help you: Android L FAB Button shadow

或者这篇文章对你有帮助:Android L FAB Button shadow

I explained there, how to implement the new FAB button, I also used the outline for that.

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

回答by nirav kalola

click herefor complete source code

单击此处获取完整源代码

Create button.xml in drawable folder

在 drawable 文件夹中创建 button.xml

<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

在 drawable-v21 文件夹中创建 button.xml

<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.

您可以将 button.xml 设置为视图的背景。

<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.

如果您想要波纹效果的自定义颜色而不是默认的灰色,那么您可以通过在您的样式中添加 colorControlHighlight 来存档它。

<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>

回答by vishal gupta

<RelativeLayout
 android:foreground="?android:attr/selectableItemBackground">
 android:layout_width="match_parent"
 android:layout_height="wrap_content"
</RelativeLayout>

enter image description here

在此处输入图片说明

回答by kelvincer

This is with ripple and layer-list.

这是带有涟漪和图层列表的。

<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>

回答by Dhina k

ripple touch effect button in android studio

android studio中的波纹触摸效果按钮

       <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" />

For more reference click here http://androiddhina.blogspot.in/2015/04/android-ripple-touch-effect-example.html

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