将坐标从容器的坐标空间转换为子坐标空间



我有一个容器,它处理所有的触摸和遍历(必要时)一些事件通过调用onTouch它的子视图实现的子。问题是容器在它自己的坐标系中接收触摸,子容器必须把它转换成子容器的CS。以下是容器代码:

@Override
public boolean onTouchEvent(MotionEvent event) {
//handle some gestures
    ....
    //traverse motion event so container's children can handle it
    if(numFingers==1)
        content.onTouch(content,event);
    return true;
}

孩子的代码:

public boolean onTouch(View v, MotionEvent event) {
    //get child's tranformation
    Matrix m=this.getMatrix();
    float[] coords=new float[2];
    //get touch coords
    coords[0]=event.getX();
    coords[1]=event.getY();
    //translate it to child's coordinates
    m.mapPoints(coords);
    PointF p =new PointF(coords[0],coords[1]);
    Piece piece=getPieceUnderPoint(p);
    if (piece!=null)
        Log.d("game field3",piece.i+","+piece.j);
    return true;
}

我可以看到我的坐标是不正确的翻译绘制矩形在孩子的画布上的触摸点。

解决方案是,我必须在将其应用于触摸坐标之前反转变换矩阵m.invert (m);

最新更新