Android L ripple touch effect on shape?

前端 未结 4 1494
抹茶落季
抹茶落季 2020-12-04 19:25

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

<
相关标签:
4条回答
  • 2020-12-04 19:43

    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

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

    0 讨论(0)
  • 2020-12-04 19:54

    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>
    
    0 讨论(0)
  • 2020-12-04 19:57

    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>
    
    0 讨论(0)
  • 2020-12-04 20:09
    <RelativeLayout
     android:foreground="?android:attr/selectableItemBackground">
     android:layout_width="match_parent"
     android:layout_height="wrap_content"
    </RelativeLayout>
    

    0 讨论(0)
提交回复
热议问题