【问题标题】:convert coordinates from container's coordinate space to child one将坐标从容器的坐标空间转换为子坐标
【发布时间】:2016-10-04 20:28:10
【问题描述】:

我有处理所有触摸的容器,并通过调用由子视图实现的 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;
}

通过在孩子的画布上围绕触摸点绘制矩形,我可以看到我的坐标转换不正确。

【问题讨论】:

  • 你能发布预期坐标和观察到的坐标吗

标签: android coordinates touches


【解决方案1】:

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2011-09-02
    • 1970-01-01
    • 2018-05-25
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多