Android – add a ripple background effect on a white background

I want to add ripple effect to my recycle bin view item. Android: background = "? Android: attr / selectableitembackground" I can receive ripple effect, but the problem is that I want to generate ripple effect on white background. This is my XML

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/layout"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:gravity="center"
android:background="?android:attr/selectableItemBackground"
android:background="@color/white"
android:layout_marginBottom="1dp"
xmlns:tools="http://schemas.android.com/tools"
tools:background="@color/white"
android:padding="6dp">...
.....soME MORE CODE....
</LinearLayout>

I can't have two Android backgrounds, so how do I achieve ripple effects on a white background

resolvent:

You can use the rippledrawable class for this. Here is the code:

RippleDrawable rippleDrawable = (RippleDrawable)view.getBackground(); // It will assume bg is a RippleDrawable

int[][] states = new int[][] { new int[] { android.R.attr.state_enabled} };
int[] colors = new int[] { Color.BLUE }; // sets the ripple color to blue

ColorStateList colorStateList = new ColorStateList(states, colors);
rippleDrawable.setColor(colorStateList);

If you are satisfied with XML, you can use the following code:

<?xml version="1.0" encoding="utf-8"?>
<ripple
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:color="#FF000000"> 

    <!-- the normal bg color -->
    <item>
        <color android:color="#FFDDDDAA" />
    </item>

    <!-- make sure the ripple doesn't exceed the bounds -->
    <item android:id="@android:id/mask">
        <shape android:shape="rectangle">
            <solid android:color="?android:colorAccent" />
        </shape>
    </item>
</ripple>

The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>