Android L — Play Ripple Effect on View

2019-05-18 09:05发布

I am trying to play a ripple effect (from Android L) on a view at a certain time (not on the view being touched). To be specific, when the user successfully changes some text, I want a certain view to play a green ripple effect to show success. Is there any way to do this?

I have tried putting a RippleDrawable in an Animation, putting the RippleDrawable as the background for my "success view." But, I can't figure out how to play the ripple animation as I have described.

P.S. My project is Android L only right now, so I am not worried about backwards compatibility.

1条回答
2楼-- · 2019-05-18 09:48

Since your mask is just a solid rectangle, you can simplify your ripple XML to:

<?xml version="1.0" encoding="utf-8"?>
<ripple xmlns:android="http://schemas.android.com/apk/res/android"
    android:color="?android:colorControlHighlight">
    <item android:id="@android:id/mask" android:drawable="@android:color/white" />
    <item android:drawable="@drawable/file_item_selector"/>
</ripple>

You can manually trigger a ripple effect by forcing a view in and out of the pressed state.

myView.setPressed(true);

// For a quick ripple, you can immediately set false.
myView.setPressed(false);

// Or for a long ripple, you can post to a handler.
myView.postDelayed(new Runnable() {
    public void run() {
        myView.setPressed(false);
    }
}, 500 /* delay in milliseconds */);
查看更多
登录 后发表回答