I want to judge the distance between two points when I touch the screen and move my two fingers.I don’t konw my calculation method is right or wrong.If you have the true method,please tell me.Thanks very much.
public class MyMultitouch implements OnTouchListener, OnGestureListener{
private GestureDetector mGestureDetector;
private View view;
private LinearLayout _first_linearlayout;
private LinearLayout _middle_linearlayout;
private float beforeLenght1,beforeLenght2;
private float afterLenght1,afterLenght2;
private float gapLenght,gapLenght1,gapLenght2;
public MyMultitouch(View view){
this.mGestureDetector = new GestureDetector(this);
this.view = view;
mGestureDetector.setIsLongpressEnabled(true);
}
@Override
public boolean onDown(MotionEvent e) {
// TODO Auto-generated method stub
return false;
}
public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX,
float velocityY) {
float moveX1 = e1.getX(1) - e1.getX(0);
float moveY1 = e1.getY(1) - e1.getY(0);
float moveX2 = e2.getX(1) - e2.getX(0);
float moveY2 = e2.getY(1) - e2.getY(0);
switch (e1.getAction()) {
case MotionEvent.ACTION_DOWN:
beforeLenght1 = (float) Math.sqrt((moveX1 * moveX1)
+ (moveY1 * moveY1));
beforeLenght2 = (float) Math.sqrt((moveX2 * moveX2)
+ (moveY2 * moveY2));
break;
case MotionEvent.ACTION_MOVE:
afterLenght1 = (float) Math.sqrt((moveX1 * moveX1)
+ (moveY1 * moveY1));
afterLenght2 = (float) Math.sqrt((moveX2 * moveX2)
+ (moveY2 * moveY2));
gapLenght1 = afterLenght1 - beforeLenght1;
gapLenght2 = afterLenght2 - beforeLenght2;
gapLenght = gapLenght2 - gapLenght1;
}
if (gapLenght > 0) {
_first_linearlayout = (LinearLayout)view.findViewById(R.id.first_linearlayout);
_middle_linearlayout = (LinearLayout)view.findViewById(R.id.middle_linearlayout);
_first_linearlayout.setVisibility(View.GONE);
_middle_linearlayout.setVisibility(View.GONE);
}
if (gapLenght < 0) {
_first_linearlayout = (LinearLayout)view.findViewById(R.id.first_linearlayout);
_middle_linearlayout = (LinearLayout)view.findViewById(R.id.middle_linearlayout);
_first_linearlayout.setVisibility(View.VISIBLE);
_middle_linearlayout.setVisibility(View.VISIBLE);
}
return true;
}
@Override
public void onLongPress(MotionEvent e) {
// TODO Auto-generated method stub
}
@Override
public boolean onScroll(MotionEvent e1, MotionEvent e2, float distanceX,
float distanceY) {
// TODO Auto-generated method stub
return false;
}
@Override
public void onShowPress(MotionEvent e) {
// TODO Auto-generated method stub
}
@Override
public boolean onSingleTapUp(MotionEvent e) {
// TODO Auto-generated method stub
return false;
}
@Override
public boolean onTouch(View arg0, MotionEvent arg1) {
// TODO Auto-generated method stub
return mGestureDetector.onTouchEvent(arg1);
}
}
`
You could use spacing method in this section of the How to use Multi-touch in Android 2 article.