本文介绍了以ACTION_OUTSIDE方式使INVISIBLE View句柄触摸的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想创建透明的系统覆盖窗口,该窗口具有多个圆形视图:

I want to create transparent system overlay window, which has several round views:

绿色视图为VISIBLE,红色视图为INVISIBLE.问题是我需要红色视图才能将触摸传递到基础窗口,但是默认情况下不需要.

Green view is VISIBLE, red one is INVISIBLE. The thing is that I need red view to pass touches to the underlying window, but by default it doesn't.

我尝试将红色视图的可见性设置为GONE,但是包含视图的整体大小发生了变化.由于需要将包含的视图捕捉到屏幕的右边缘,因此这意味着绿色视图的位置会发生变化,因此我不希望这样做.

I tried setting visibility of red view to GONE, but then overall size of containing view changes. Since I need the containing view to be snapped to the right edge of the screen, this means that position of green view changes, and I don't want this.

有什么方法可以覆盖INVISIBLE状态下的默认触摸处理?

Is there any way to override default touch handling in INVISIBLE state?

下面是一个实际的屏幕截图,可以使我的问题更加清楚:

Here's an actual screenshot to make my question more clear:

此外,我必须重写dispatchTouchEvent()才能正确调度触摸事件:

Also, I had to override dispatchTouchEvent() to dispatch touch events correctly:

@Override
public boolean dispatchTouchEvent(MotionEvent ev) {
    Circle target=null;
    int targetIndex=-1;
    for (int i=0; i<getChildCount(); i++) {
        Circle child=(Circle) getChildAt(i);
        if (child.viewMetrics.containsPoint(ev.getX(), ev.getY()) && (ev.getAction()==MotionEvent.ACTION_MOVE || child.isVisible())) {
            target=child;
            targetIndex=i;
            break;
        }
    }
    onMotionEvent(ev, target, targetIndex);
    Log.d(">| CircularLayout", "action: "+ev.getAction()+", target: "+target);
    return target!=null && target.dispatchTouchEvent(ev);
}

推荐答案

我最终要做的是计算两组视图尺寸:最大可能值(红色区域的大小)和当前(绿色区域).当隐藏或显示子视图时,我将窗口LayoutParamswidthheight设置为当前尺寸,然后将最大尺寸和当前尺寸之间的差异设置为LayoutParamsyx参数.

What I ended up doing is calculating two sets of view dimensions: maximum possible (size of the red area) and current (green area). When child view is hidden or shown I set width and height of window LayoutParams to current dimensions and then set difference between maximum and current dimensions to y and x parameters of LayoutParams.

由于某些原因,应用修改后的LayoutParams会触发两次测量/布局遍历(这又导致视图反弹"),但这似乎是全新的故事.

For some reason applying modified LayoutParams triggers measure/layout pass twice (which in turn causes the view to "bounce"), but that seems to be the whole new story.

这篇关于以ACTION_OUTSIDE方式使INVISIBLE View句柄触摸的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-02 17:22