Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/339.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/blackberry/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 将MotionEvent值存储在ArrayList中_Java_Android_Canvas_View_Motionevent - Fatal编程技术网

Java 将MotionEvent值存储在ArrayList中

Java 将MotionEvent值存储在ArrayList中,java,android,canvas,view,motionevent,Java,Android,Canvas,View,Motionevent,大家好, 因此,我在自定义视图类中使用onDraw在RelativeLayout+TableLayout上绘制形状,一切正常 在MotionEvent的ACTION\u MOVE中,我将X&Y传递给我的CustomView,并调用一个方法,分别为每组坐标/形状返回唯一的ID值 由于将手指从一个形状拖到另一个形状:多个值将按预期返回控制台,我希望将这些值存储在数组中以备将来使用。我遇到的问题是,当我尝试存储值时,在控制台显示4的情况下,它只存储一个值 控制台输出如下所示: 06-22 07:28:

大家好,

因此,我在自定义视图类中使用onDraw在RelativeLayout+TableLayout上绘制形状,一切正常

MotionEvent
ACTION\u MOVE
中,我将
X&Y
传递给我的CustomView,并调用一个方法,分别为每组坐标/形状返回唯一的ID值

由于将手指从一个形状拖到另一个形状:多个值将按预期返回控制台,我希望将这些值存储在
数组中
以备将来使用。我遇到的问题是,当我尝试存储值时,在控制台显示4的情况下,它只存储一个值

控制台输出如下所示:

06-22 07:28:56.173 zar.myapp I/System.out:值:354

06-22 07:28:56.193 zar.myapp I/System.out:值:858

06-22 07:28:56.213 zar.myapp I/System.out:值:989

06-22 07:28:56.213 zar.myapp I/System.out:值:789

代码:

DotView类:在我的
main活动中
我在其中构建我的表格网格,我调用:
DotView.setId(I)
为每个单元格分配唯一ID(已验证)。(我只分享了相关代码):

getChildForTouch

private DotView getChildForTouch(TableLayout table, float x, float y) {
                    final int childWidth = ((TableRow) table.getChildAt(0))
                            .getChildAt(0).getWidth();
                    final int childHeight = ((TableRow) table.getChildAt(0))
                            .getChildAt(0).getHeight();


                    // find out the row of the child
                    int row = 0;
                    do {
                        if (y > childHeight) {
                            row++;
                            y -= childHeight;
                        } else {
                            break;
                        }
                    } while (y > childHeight);
                    int column = 0;
                    do {
                        if (x > childWidth) {
                            column++;
                            x -= childWidth;
                        } else {
                            break;
                        }
                    } while (x > childWidth);
                    return (DotView) ((TableRow) table.getChildAt(row))
                            .getChildAt(column);
                }
可根据要求提供更多代码

提前谢谢

我遇到的问题是,当我试图存储这些值时,它只会 在控制台显示4的情况下存储一个值

我假设您正在onTouch()回调中创建XYcoords列表,在这种情况下,它的大小为1,对应于当前的触摸事件。当用户将手指移动到touch()上时,另一个触摸事件将再次调用它,此时您将创建一个带有一个条目的新的XYcoords列表,以此类推

要解决此问题,请将XYcoords列表初始化移动到onTouch()回调的外部,就像类中的字段一样。这样,您就不会在每次触摸事件发生时都重新创建它

作为旁注,我假设使用当前代码,您正试图将用户移动手指时触摸的点视图存储在列表中。在这种情况下,您很可能不想简单地在MotionEvent.ACTION\中执行XYcoords.add(endView.getId())操作,因为最终会有数百个重复条目(因为在单个点视图中会触发许多触摸事件)。相反,不允许重复:

if (XYcoords.size() != 0) {
   // check to see if we aren't trying to add the same DotView reference
   // (meaning we are still inside the same DotView)
   int lastId = XYcoords.get(XYcoords.size() - 1);
   if (lastId != endView.getId()) {
      // not the same DotView so add it
      XYcoords.add(endView.getId());
   } 
} else {
   XYcoords.add(endView.getId()); // first event, must add it
}
对于MotionEvent.ACTION\u UP,您需要使用(或将信息存储在XYcoords中的其他地方)XYcoords中的数据,并清除它,以便下次用户开始触摸点视图时,您有一个新的历史记录(XYcoords中没有项目)

我遇到的问题是,当我试图存储这些值时,它只会 在控制台显示4的情况下存储一个值

我假设您正在onTouch()回调中创建XYcoords列表,在这种情况下,它的大小为1,对应于当前的触摸事件。当用户将手指移动到touch()上时,另一个触摸事件将再次调用它,此时您将创建一个带有一个条目的新的XYcoords列表,以此类推

要解决此问题,请将XYcoords列表初始化移动到onTouch()回调的外部,就像类中的字段一样。这样,您就不会在每次触摸事件发生时都重新创建它

作为旁注,我假设使用当前代码,您正试图将用户移动手指时触摸的点视图存储在列表中。在这种情况下,您很可能不想简单地在MotionEvent.ACTION\中执行XYcoords.add(endView.getId())操作,因为最终会有数百个重复条目(因为在单个点视图中会触发许多触摸事件)。相反,不允许重复:

if (XYcoords.size() != 0) {
   // check to see if we aren't trying to add the same DotView reference
   // (meaning we are still inside the same DotView)
   int lastId = XYcoords.get(XYcoords.size() - 1);
   if (lastId != endView.getId()) {
      // not the same DotView so add it
      XYcoords.add(endView.getId());
   } 
} else {
   XYcoords.add(endView.getId()); // first event, must add it
}

对于MotionEvent.ACTION\u UP,您需要使用(或将信息存储在XYcoords中的其他地方)XYcoords中的数据,然后清除它
,这样下次用户开始触摸点视图时,您就有了一个新的历史记录(XYcoords中没有项目)。

endView.getId()
将返回视图ID,或者是生成该值的方法?如果它是计算单个值的方法,请确保它返回不同的值。(在将其添加到数组之前对其进行日志记录)自定义方法
endView.getId()
使用X和Y来获取分配给画布上绘制的每个形状的唯一ID。是的,它返回不同的值;请看我上面的输出。数组中包含的一个值,是您生成的吗(是在控制台中打印的)?是的,先生,我认为它总是最后一个值:789(在本例中)-但我不确定。我在想也许我可以尝试将原始的
X+Y
coords存储到一个数组中,然后执行
for
循环,并将存储的X+Y传递给我的自定义视图类?idkYes,我只是好奇它是否能与字符串值一起工作。
endView.getId()
将返回视图ID,或者是生成该值的方法?如果它是计算单个值的方法,请确保它返回不同的值。(在将其添加到数组之前对其进行日志记录)自定义方法
endView.getId()
使用X和Y来获取分配给画布上绘制的每个形状的唯一ID。是的,它返回不同的值;请看我上面的输出。数组中包含的一个值,是您生成的吗(是在控制台中打印的)?是的,先生,我认为它总是最后一个值:789(在本例中)-但我不确定。我在想也许我可以尝试将原始的
X+Y
coords存储到一个数组中,然后执行
for
循环,并将存储的X+Y传递给我的自定义视图类?idkYes,我只是好奇它是否能与字符串值一起工作。
if (XYcoords.size() != 0) {
   // check to see if we aren't trying to add the same DotView reference
   // (meaning we are still inside the same DotView)
   int lastId = XYcoords.get(XYcoords.size() - 1);
   if (lastId != endView.getId()) {
      // not the same DotView so add it
      XYcoords.add(endView.getId());
   } 
} else {
   XYcoords.add(endView.getId()); // first event, must add it
}