Android L — Play Ripple Effect on View

一个人想着一个人 提交于 2019-12-22 13:59:06

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!