问题
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:
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 */);
来源:https://stackoverflow.com/questions/25407291/android-l-play-ripple-effect-on-view