-1

I am making multitouch based android app. In the app I am finding distance between two touch coordinates.The Distance between two touch coordinates are same on different devices, but devices show different distance.

Devices A shows 89.0123.

Device B shows 110.84940.

How to get same distance on different devices?

public boolean onTouchEvent(MotionEvent event)
{
 int maskedAction=event.getActionMasked();
 switch(maskedAction)
 /// some code
 ///case MotionEvent.ACTION_POINTER_DOWN;
 /// 
 PointF init_p1,init_p2;
 init_p1.x=event.getX(0);
 init_p1.y=event.getY(0);
 init_p2.x=event.getX(1);
 init_p2.y=event.getY(1);
 /////// some code
 float X_diff=init_p1.x-init_p2.x;
 float Y_diff=init_p1.y-init_p2.y;
 double xdiffsq= Math.pow(X_diff,2);
 double ydiffsq= Math.pow(Y_diff,2);
 distance=Math.sqrt(xdiffsq+ydiffsq);
 Log.d(TAG,String.valueOf(distance));
///some code
}
phani_rohith
  • 136
  • 5

1 Answers1

1

If by "the same distance" you mean the same physical distance, then it's because your devices have different screen densities. You need to convert your distance (which is currently given is pixels) to density-independent units.

You may use this method:

static float pxToDp(Context context, float px) {
    return TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, px, context.getResources().getDisplayMetrics());
}

It should return the same DP values for different screens.

SqueezyMo
  • 1,606
  • 2
  • 21
  • 34