RippleDrawable - Show Ripple Effect programmatically

佐手、 提交于 2019-12-01 03:42:41

RippleDrawable responds to pressed and focused states, with the former giving the ripple animation that you're looking for. You can toggle pressed state on the host view using View.setPressed(boolean).

Runnable pressRunnable = new Runnable() {
    @Override
    public void run() {
        button.setPressed(true);
        button.postOnAnimationDelayed(unpressRunnable, 250);
    }
};

Runnable unpressRunnable = new Runnable() {
    @Override
    public void run() {
        button.setPressed(false);
        button.postOnAnimationDelayed(pressRunnable, 250);
    }
};

Keep in mind that the user touching the Button toggles the pressed state as well, so you might want to remove or re-post your runnables in an OnTouchListener.

If desired, you can control the ripple position using Drawable.setHotspot(float, float) or, on API 22+, View.dispatchDrawableHotspotChanged(float, float) and passing view-relative (x, y) coordinates.

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