如何检测是否在屏幕的左侧或右侧执行了运动事件手势?

时间:2016-12-20 12:25:40

标签: java android

与MX播放器的情况一样,左手垂直滑动手势控制亮度,右手侧垂直手势控制音量,这个概念我需要在我的应用中应用。那我该怎么做呢?

1 个答案:

答案 0 :(得分:1)

您可以像这样收听onTouchEvent:

public class MainActivity extends Activity {
       float x1 = 0;
       float y1 = 0;
       float x2 ;
       float y2 ;
    ...

    @Override
    public boolean onTouchEvent(MotionEvent event){
    int action = MotionEventCompat.getActionMasked(event);

            switch(action) {
                case (MotionEvent.ACTION_DOWN) :
                    x1 = event.getX();
                    y1 = event.getY();
                    return true;
                case (MotionEvent.ACTION_UP) :
                    x2 = event.getX();
                    y2 = event.getY();

                    //if left to right sweep event on screen
                    if (x1 < x2)
                    {
                        Toast.makeText(this, "Left to Right Swap Performed", Toast.LENGTH_LONG).show();
                    }

                    // if right to left sweep event on screen
                    if (x1 > x2)
                    {
                        Toast.makeText(this, "Right to Left Swap Performed", Toast.LENGTH_LONG).show();
                    }

                    // if UP to Down sweep event on screen
                    if (y1 < y2)
                    {
                        Toast.makeText(this, "UP to Down Swap Performed", Toast.LENGTH_LONG).show();
                    }

                    //if Down to UP sweep event on screen
                    if (y1 > y2)
                    {
                        Toast.makeText(this, "Down to UP Swap Performed", Toast.LENGTH_LONG).show();
                    }
                    return true;