如何在视图内的特定位置触发Android Lollipop上的涟漪效应,而不会触发触摸事件? [英] How to trigger ripple effect on Android Lollipop, in specific location within the view, without triggering touches events?

查看:100
本文介绍了如何在视图内的特定位置触发Android Lollipop上的涟漪效应,而不会触发触摸事件?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是一个简短的问题:

假设我有一个以RippleDrawable为背景的View.

Suppose I have a View with the RippleDrawable as background.

是否有一种简便的方法可以在不触发任何触摸或点击事件的情况下从特定位置触发纹波?

Is there an easy way to trigger the ripple from a specific position without triggering any touch or click events?

推荐答案

是的!为了以编程方式触发纹波,必须使用setState()设置RippleDrawable的状态.呼叫setVisible()不能起作用!

Yes there is! In order to trigger a ripple programatically you have to set the state of the RippleDrawable with setState(). Calling setVisible() does NOT work!

要显示波纹,必须同时将状态设置为按下并启用:

To show the ripple you have to set the state to pressed and enabled at the same time:

rippleDrawable.setState(new int[] { android.R.attr.state_pressed, android.R.attr.state_enabled });

只要设置了这些状态,就会显示纹波.当您想再次隐藏波纹时,将状态设置为空的int[]:

The ripple will be shown as long as those states are set. When you want to hide the ripple again set the state to an empty int[]:

rippleDrawable.setState(new int[] {  });

您可以通过调用setHotspot()设置发出波纹的点.

You can set the point from which the ripple emanates by calling setHotspot().

我已经进行了大量调试,并上下研究了RippleDrawable的源代码,直到我意识到波动实际上是在onStateChange()中触发的.调用setVisible()没有任何作用,也永远不会引起任何波纹.

I have debugged a lot and studied the source code of RippleDrawable up and down until I realised that the ripple is actually triggered in onStateChange(). Calling setVisible() has no effect and never causes any ripple to actually appear.

RippleDrawable的源代码的相关部分是这样的:

The relevant part of the source code of RippleDrawable is this:

@Override
protected boolean onStateChange(int[] stateSet) {
    final boolean changed = super.onStateChange(stateSet);

    boolean enabled = false;
    boolean pressed = false;
    boolean focused = false;

    for (int state : stateSet) {
        if (state == R.attr.state_enabled) {
            enabled = true;
        }
        if (state == R.attr.state_focused) {
            focused = true;
        }
        if (state == R.attr.state_pressed) {
            pressed = true;
        }
    }

    setRippleActive(enabled && pressed);
    setBackgroundActive(focused || (enabled && pressed));

    return changed;
}

如您所见,如果同时设置了启用和按下属性,则波纹和背景都将被激活,并且波纹将被显示. 此外,只要您设置了聚焦状态,背景也会被激活.有了它,您可以触发波纹并让背景独立变色.

As you can see if both the enabled and pressed attribute are set then both the ripple and background will be activated and the ripple will be displayed. Additionally as long as you set the focused state the background will be activated as well. With this you can trigger the ripple and have the background change color independently.

如果您有兴趣,可以查看RippleDrawable的完整源代码

If you are interested you can view the entire source code of RippleDrawable here.

这篇关于如何在视图内的特定位置触发Android Lollipop上的涟漪效应,而不会触发触摸事件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆