触摸甚至我的代码总是右侧位置

时间:2013-02-18 11:06:52

标签: android

我正在开发一个Android应用程序,我想知道是否可以通过固定一个轴来检测移动方向。例如,我想把手机放在桌子上并在我移动它时检测方向(左,右,上和下)。距离没有必要,我只是想知道准确的方向。

谢谢!

我的代码是:

arg1.setOnTouchListener(new OnTouchListener() {

                @Override
                public boolean onTouch(View arg0, MotionEvent event) {

                    float startX = 0;
                    float startY;
                    float endX;
                    float endY;
                    switch (event.getAction()) {
                    case MotionEvent.ACTION_DOWN:
                        startX = (int) event.getX();
                        startY = (int) event.getY();
                        System.out.println("startX" + startX);
                        System.out.println("startY" + startY);
                        break;
                    case MotionEvent.ACTION_MOVE:

                        endX = event.getX();
                        endY = (int) event.getY();
                        System.out.println("endX" + endX);
                        System.out.println("endY" + endY);
                        float sub = endX - startX;
                        System.out.println("sub" + sub);
                        if ((endX - startX) < 100) {
                            arg0.setBackgroundColor(Color.GREEN);

                        }
                        if ((endX - startX) > 100) {
                            try {
                                TextView ph_tv = (TextView) arg0
                                        .findViewById(R.id.textnum);
                                String Pho_no = ph_tv.getText().toString();
                                arg0.setBackgroundColor(Color.RED);
                                Intent intent = new Intent(
                                        Intent.ACTION_CALL, Uri
                                                .parse("tel:" + Pho_no));
                                intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
                                startActivity(intent);
                                lv.setBackgroundColor(Color.BLACK);

                            } catch (ActivityNotFoundException e) {
                                Toast.makeText(
                                        getApplicationContext(),
                                        "Error in your phone call"
                                                + e.getMessage(),
                                        Toast.LENGTH_LONG).show();

                            }
                            section(path_name, lv1);

                        }
                        break;

                    case MotionEvent.ACTION_UP:
                        endX = (int) event.getX();
                        endY = (int) event.getY();
                    }
                    return true;
                }

            });

2 个答案:

答案 0 :(得分:2)

如果你想检测手指的移动方向,我认为在你的情况下使用onTouchListener不是一个好的解决方案。我建议你使用手势监听器 这是左右的例子

class MyGestureDetector extends SimpleOnGestureListener {
        @Override
        public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX, float velocityY) {
            try {
                if (Math.abs(e1.getY() - e2.getY()) > SWIPE_MAX_OFF_PATH)
                    return false;
                // right to left swipe
                if(e1.getX() - e2.getX() > SWIPE_MIN_DISTANCE && Math.abs(velocityX) > SWIPE_THRESHOLD_VELOCITY) {
                    Toast.makeText(SelectFilterActivity.this, "Left Swipe", Toast.LENGTH_SHORT).show();
                }  else if (e2.getX() - e1.getX() > SWIPE_MIN_DISTANCE && Math.abs(velocityX) > SWIPE_THRESHOLD_VELOCITY) {
                    Toast.makeText(SelectFilterActivity.this, "Right Swipe", Toast.LENGTH_SHORT).show();
                }
            } catch (Exception e) {
                // nothing
            }
            return false;
        }

    }

你可以通过计算Y距离

来做同样的上下行动

如果您对我的回答有任何其他疑问,或者如果您认为我的答案不够明确,请随时在评论中留下问题:)

我希望我的回答可以帮到你!

答案 1 :(得分:1)

private final int mDragInterwell = 5;

private Point mStartPoint;

private final 

@Override
public boolean onTouch(View arg0, MotionEvent event) {

    switch (event.getAction()) {
    case MotionEvent.ACTION_DOWN:
        mStartPoint = new Point(event.getX(), event.getY());
        break;
    case MotionEvent.ACTION_MOVE:

        final Point current = new Point(event.getX(), event.getY());
        Log.d(getClass().getName(), decideMovement(current));
        break;

    case MotionEvent.ACTION_UP:
       break;
    }
    return true;
}

private String decideMovement(final Point point)
{
    String direction = "invalid";
    boolean changed  = false;
    if( ( mStartPoint.x - point.x ) > mDragInterwell )
    {
        direction = "left";
        changed   = true;
    }else if(  ( mStartPoint.x - point.x ) > -mDragInterwell  )
    {
        changed   = true;
        direction = "right";
    }else if(  ( mStartPoint.y - point.y ) > mDragInterwell  )
    {
        changed   = true;
        direction = "down";
    }else if(  ( mStartPoint.y - point.y ) > -mDragInterwell  )
    {
        changed   = true;
        direction = "up";
    }
    if(changed)
    {
        mStartPoint = point;
    }
    return direction;
}

希望这会有所帮助..