如何检测大量视图上的触摸移动?

How to detect touch move over a large number of views?

我的要求:我需要通过 Android 应用程序通过 BluetoothLE 传递帧来为 7*16 LED 显示屏设置动画。我已经在应用程序上创建了显示设计,并向它们添加了带有渐变可绘制背景的空视图。当我触摸它们时,这些视图的颜色需要改变。为每个视图添加触摸侦听器对我的情况没有帮助。

我取得的成就:我以编程方式添加了大量视图(100+),并为每个视图设置了标签。我已经为添加了这些视图的父视图设置了一个 OnTouch 事件处理程序。 通过跟踪触摸事件的绝对坐标(x 和 y)并与我在触摸事件处理程序中循环的几个单独视图的绝对边界进行比较,我能够检测到悬停,如触摸移动(越界到内)边界)超过 3-4 个视图。

我参考了

的解决方案

我被卡住的地方:但是,当我尝试增加循环大小以覆盖所有添加的视图时,应用程序响应变慢并且悬停检测在大多数意见。我知道发生这种情况是因为 OnTouch 事件处理程序中的大量计算是我不应该做的。

我需要什么:我需要在性能方面改进此解决方案或实现我的目标的替代方法。

代码片段

void DrawScreen()
        {
            for (int column = 0; column < 8; column++)
            {
                for (int row = 0; row < 17; row++)
                {
                    relativeLayout.AddView(DrawRect(row, column));
                }
            }
        }

View DrawRect(int row, int column)
        {
            View customView = new View(Context);
            GradientDrawable shape = new GradientDrawable();
            shape.SetShape(ShapeType.Rectangle);
            shape.SetCornerRadii(new float[] { 10, 10, 10, 10, 10, 10, 10, 10 });
            shape.SetColor(Color.ParseColor("#3F0000"));
            RelativeLayout.LayoutParams param = new RelativeLayout.LayoutParams(ViewGroup.LayoutParams.WrapContent, ViewGroup.LayoutParams.WrapContent);
            param.LeftMargin = ((column-1) * (width + h_spacing)) + h_spacing;
            param.Width = width;
            param.Height = height;
            param.TopMargin = ((row-1) * (height + v_spacing)) + v_spacing;
            customView.Background = shape;
            customView.LayoutParameters = param;
            customView.Tag = (8 - column).ToString() + "," + (17 - row).ToString();           
            return customView;
        }

private void RelativeLayout_Touch(object sender, View.TouchEventArgs e)
    {
        if(e.Event.Action == MotionEventActions.Up)
        {
            out_of_bounds = true;
            view_in_bound = null;
        }
        else
        {
            for (int row = 1; row < 8; row++)
            {
                for (int column = 1; column < 17; column++)
                {
                    View view = relativeLayout.FindViewWithTag(row.ToString() + "," + column.ToString());

                    if (CheckInterSection(view, e.Event.RawX, e.Event.RawY))
                    {
                        if (out_of_bounds == true)
                        {
                            view_in_bound = view;
                            out_of_bounds = false;
                            Log.Debug("Touch", "Inside");
                            //ToggleViewState(view);                                   
                        }
                        else
                        {
                            Log.Debug("Touch", "Still Inside");
                        }
                    }
                    else
                    {
                        if (view == view_in_bound)
                        {
                            out_of_bounds = true;
                            view_in_bound = null;
                            Log.Debug("Touch", "Outside");
                        }                                
                    }
                }
            }
        }
    }

bool CheckInterSection(View view, float rawX, float rawY)
        {
            int[] location = new int[2];
            view.GetLocationOnScreen(location);           
            int x = location[0] - h_spacing/2;
            int y = location[1] - v_spacing/2;
            int width = (view.Width + h_spacing/2);
            int height = (view.Height + v_spacing/2);
            return (!(rawX < x || rawX > (x + width) || rawY < y || rawY > (y + height)));
        }

我尝试使用轨迹角度进一步减小循环大小,但性能从未达到我的预期,并且经常错过视图上的触摸事件。

然后我意识到我走错了方向并找到了一个更简单的解决方案。由于我的视图是以编程方式添加的并且大小相同,所以我知道布局中每个视图的坐标和边界。所以我将布局分成一个网格,并根据触摸坐标,我能够确定触摸在哪个部分。以下是我的解决方案,它运行良好。但是,我会等一会儿,直到我将其标记为解决方案,因为有人可以更好地实施我的技术或替代解决方案。

void DrawScreen()
        {            
            for (int column = 1; column < 17; column++)
            {
                for (int row = 1; row < 8; row++)
                {
                    relativeLayout.AddView(DrawRect(row, column));
                }
            }
        }

View DrawRect(int row, int column)
        {
            View customView = new View(Context);
            if (!CheckBit(row - 1, column - 1))
            {
                customView.SetBackgroundResource(Resource.Drawable.off_rounded_btn);
            }
            else
            {
                customView.SetBackgroundResource(Resource.Drawable.rounded_btn);
            }
            RelativeLayout.LayoutParams param = new RelativeLayout.LayoutParams(ViewGroup.LayoutParams.WrapContent, ViewGroup.LayoutParams.WrapContent);
            param.LeftMargin = ((column-1) * (width + h_spacing)) + h_spacing;
            param.Width = width;
            param.Height = height;
            param.TopMargin = ((row-1) * (height + v_spacing)) + v_spacing;
            customView.LayoutParameters = param;
            customView.Tag = row.ToString() + "," + column.ToString();           
            return customView;
        }

void RelativeLayout_Touch(object sender, View.TouchEventArgs e)
        {
            if (e.Event.Action == MotionEventActions.Up)
            {
                view_in_bound = null;
            }
            else
            {
                int row = CheckTouchArea(e.Event.RawX, e.Event.RawY)[0];
                if (row != 0)
                {
                    int column = CheckTouchArea(e.Event.RawX, e.Event.RawY)[1];
                    check_view = GetView(row, column);
                    if (check_view != view_in_bound)
                    {
                        ChangeViewState(check_view, Touch_CheckBit(row - 1, column - 1), row - 1, column - 1);
                        view_in_bound = check_view;
                    }
                }   
            }
        }

int[] CheckTouchArea(float rawX, float rawY)
        {
            int[] tag = new int[2];
            int[] location = new int[2];
            relativeLayout.GetLocationOnScreen(location);
            float x = location[0] + h_padding / 2;
            int y = location[1] + v_padding / 2;
            float width = relativeLayout.Width - h_padding;
            int height = relativeLayout.Height - v_padding;
            if ((!(rawX < x || rawX > (x + width) || rawY < y || rawY > (y + height))))
            {
                int column = (int)Math.Ceiling((rawX - x) * 16 / width);
                int row = (int)(Math.Ceiling((rawY - y) * 7 / height));
                tag[0] = row;
                tag[1] = column;
            }            
            return tag;
        }