关于多点触控的一些问题

时间:2011-11-15 09:35:44

标签: android multi-touch

当我触摸屏幕并移动我的两根手指时,我想判断两点之间的距离。我不知道我的计算方法是对还是错。如果你有真正的方法,请告诉我。谢谢非常。

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);
}
}

`

1 个答案:

答案 0 :(得分:0)

您可以使用in this section文章的间距方法How to use Multi-touch in Android 2