0

i need to calculate the center of a MotionEvent, eg when a user touch the screen with his finger, i want to know what is the point in x,y that is the center of this finger touch, i currently use motionEvent.getX() and motionEvent.getY() but it seem to get me some point that is on the very bottom of the finger touch which is bad for me.. so how is it done?

Ofek Ron
  • 8,354
  • 13
  • 55
  • 103

2 Answers2

0

Try it with something like this in your OnTouchListener:

    @Override
    public boolean onTouch(View v, MotionEvent event)
    {
        float x = event.getX();
        float y = event.getY();

        if (event.getAction() == MotionEvent.ACTION_DOWN)
        {
            startX = x;
            startY = y;
        }
        else if (event.getAction() == MotionEvent.ACTION_UP)
        {
            centerX = (startX + x) / 2;
            centerY = (startY + Y) / 2;
        }

        return false;
    }

Where startX, startY, centerX, centerY are field variables that can be accessed from the listener.

npace
  • 4,218
  • 1
  • 25
  • 35
0

Use

public static void midpoint(MotionEvent event, Point point) {
    float x1 = event.getX(0);
    float y1 = event.getY(0);
    float x2 = event.getX(1);
    float y2 = event.getY(1);
    midpoint(x1, y1, x2, y2, point);
}

public static void midpoint(float x1, float y1, float x2, float y2, Point point) {
    point.x = (x1 + x2) / 2.0f;
    point.y = (y1 + y2) / 2.0f;
}

Call midpoint(MotionEvent event, Point point) from onTouchEvent of your view.

Khawar Raza
  • 15,870
  • 24
  • 70
  • 127