Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/181.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
Android 在缩放发生后获取单击的(x,y)坐标_Android_Touch_Android Custom View_Zooming_Panning - Fatal编程技术网

Android 在缩放发生后获取单击的(x,y)坐标

Android 在缩放发生后获取单击的(x,y)坐标,android,touch,android-custom-view,zooming,panning,Android,Touch,Android Custom View,Zooming,Panning,我有一个自定义视图,它本质上是一个绘制在画布上的网格,为此我实现了平移和缩放。这些工作很好,但我也希望能够找到点击的网格坐标 为了做到这一点,我必须补偿视图的平移量以及缩放量。这就是我遇到的问题 为了补偿平移的量,我记录了在PointF dspl中发生的所有平移(或位移)。在自定义视图的onTouchEvent(MotionEvent事件)方法中,我打开event.getAction()&MotionEvent.ACTION\u掩码,在MotionEvent.ACTION\u DOWN的情况下,

我有一个自定义视图,它本质上是一个绘制在画布上的网格,为此我实现了平移和缩放。这些工作很好,但我也希望能够找到点击的网格坐标

为了做到这一点,我必须补偿视图的平移量以及缩放量。这就是我遇到的问题

为了补偿平移的量,我记录了在
PointF dspl
中发生的所有平移(或位移)。在自定义视图的
onTouchEvent(MotionEvent事件)
方法中,我打开
event.getAction()&MotionEvent.ACTION\u掩码
,在
MotionEvent.ACTION\u DOWN
的情况下,设置事件的起始点、起始位移和一些其他自解释的内容:

case MotionEvent.ACTION_DOWN:
    //Remember where we started

    start.set(event.getX(), event.getY());   //set starting point of event
    last.set(event.getX(),event.getY());     //set coordinates of last point touched
    startDspl.set(dspl.x,dspl.y);            //set starting displacement to current displacement

    //save the id of this pointer
    activePointerId = event.getPointerId(0);

    savedMatrix.set(matrix);                 //save the current matrix
    mode = DRAG;

    break;
然后在案例
MotionEvent.ACTION\u UP
中,我执行以下操作:

 case MotionEvent.ACTION_UP:
   mode = NONE;
   activePointerId = INVALID_POINTER_ID;

   distance = Math.sqrt( (start.x - last.x)*(start.x - last.x) + 
                         (start.y - last.y)*(start.y - last.y));

   if( distance < 5){
        //need to translate x and y due to panning and zooming

        int [] coord = getClickCoordinates(start.x,start.y);
        Toast.makeText(context, " x = " + coord[0] + ", y = " + coord[1], 
        Toast.LENGTH_SHORT).show();
    }
(此处
scaleFactor
是视图的整体缩放量)。这种方法不适用于平移,正如我预期的那样,我不知道如何修改它以正确地考虑缩放

我非常感谢任何帮助,因为我已经为此奋斗了很长一段时间,是的,我知道这个问题,这和我想做的有点不同

谢谢

这个问题得到了回答。这是一个类似问题的一部分。
public int[] getClickCoordinates(float clickX, float clickY) {

    float x = (clickX - startDspl.x)/(cellWidth*scaleFactor);
    float y = nRows - (clickY - startDspl.y)/(cellHeight*scaleFactor);

    return new int[] { (int) x, (int) y };
}