简体   繁体   中英

How to know the x,y positions of my mouse when I drag around a mapView in the Android emulator?

All I achieve now is using onTouch(View v, MotionEvent event) event to get the first touch.

But I want to know the x,y values when I continuously drag the mapView.

If anyone knows the answer it would be greatly appreciated.

Its pretty straight forward. Here is some code that returns an x & y position of a touch and then returns an angle to rotate an image based on the touch position.

//TouchEvent handler
@Override
public boolean onTouchEvent(MotionEvent event) {
    Log.d(TAG, "onTouchEvent called...");
    x = event.getX();
    y = event.getY();

    switch (event.getAction()) {
    case MotionEvent.ACTION_MOVE:

        float dx = x - mPreviousX;
        float dy = y - mPreviousY;

        //Reverse direction of rotation if above midline
        if (y > getHeight() / 2) {
            dx = dx * -1;
        }

        //Reverse direction of rotation if of the midline
        if (y < getWidth() / 2) {
            dy = dy * -1;
        }

        Main.mAngle += (dx + dy) * TOUCH_SCALE_FACTOR;

        TextView txtView = (TextView) ((Activity)context).findViewById(R.id.mangle);
        txtView.setText("Angle: " + String.valueOf(Main.mAngle));

        requestRender();
    }

    mPreviousX = x;
    mPreviousY = y;

    return true;
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM