关于Android:当我使用onTouchEvent()时如何使用Log获取位置(x,y)?

时间:2011-07-26 04:28:58

标签: android logging ontouchevent

这是我的代码:

 public boolean onTouchEvent(MotionEvent cv){

    int x=(int)cv.getX();
    int y=(int)cv.getY();

    // here I want to use Log to gain the position(x,y) that I touched.But how?

    if( 0<=y && y<=py ){
        if( (px-(py-y)*Math.tan(30))<=x && x<=(px+(py-y)*Math.tan(30)) ){
            Log.i(VIEW_LOG_TAG, "yes");
        }
    }

    if( px<=x && x<=(px+py*Math.tan(15)) ){
        if( y<=(Math.tan(15)*(px-x)+py) ){

            Log.i(VIEW_LOG_TAG, "no");
        }
    }

    return super.onTouchEvent(cv);
}

1 个答案:

答案 0 :(得分:1)

记录为... android.util.Log ?,如果是这样的话:

只需添加:

Log.v(String TAG, String MSG);

像:

int x=(int)cv.getX();
int y=(int)cv.getY();

// here I want to use Log to gain the position(x,y) that I touched.But how?
Log.v("X", "pos x = " + x);
Log.v("Y", "pos y = " + y);

if( 0<=y && y<=py ){
    if( (px-(py-y)*Math.tan(30))<=x && x<=(px+(py-y)*Math.tan(30)) ){
        Log.i(VIEW_LOG_TAG, "yes");
    }
}

if( px<=x && x<=(px+py*Math.tan(15)) ){
    if( y<=(Math.tan(15)*(px-x)+py) ){

        Log.i(VIEW_LOG_TAG, "no");
    }
}

return super.onTouchEvent(cv);
}