Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to detect swipe event without finger lift

Tags:

android

I want to increment / decrement a number on continuous swipe up / swipe down events. I could implement simple swipe events using the GestureDetector and OnSwipeTouchListener.

However, this swipe event is sensed only when the finger is lifted after the swipe. So for incrementing the number to +5, I've to make 5 individual swipe up's on the screen.

I want to apply 'slider' like action such that the number gets changed while user is swiping up or down anywhere on the screen. (I don't want to use the 'Slider' widget).

Is this possible?

Any help would be really grateful. Thanks!

like image 360
zeuschyant8 Avatar asked Dec 11 '22 11:12

zeuschyant8


2 Answers

Use the OnTouchListener:

private float baseX, baseY;

OnTouchListener listener = new OnTouchListener(){
    public boolean onTouch (View v, MotionEvent event)
    {
        switch(event.getAction())
        {
            case MotionEvent.ACTION_DOWN:
                //TOUCH STARTED
                baseX = event.getX();
                baseY = event.getY();
                return true;
            case MotionEvent.ACTION_MOVE:
                //FINGER IS MOVING
                //Do your calculations here, using the x and y positions relative to the starting values you get in ACTION_DOWN
                return true;
            case MotionEvent.ACTION_CANCEL:
            case MotionEvent.ACTION_UP:
                //TOUCH COMPLETED
                return true;
        }
    }
}
like image 56
LuigiPower Avatar answered Dec 21 '22 10:12

LuigiPower


@1up, thanks a lot for the solution!

An enhancement I could add is even if the swipe direction is reversed without lifting the finger, it would work well.

Using the skeleton you gave, posting my code hoping it would help somebody in future...

public class OnSwipeListener implements OnTouchListener
{
    @Override
    public boolean onTouch(View v, MotionEvent event) 
    {
    boolean result = false;

    float currentX      = 0.0f;
    float currentY      = 0.0f;

    float current_diffX = 0.0f;
    float current_diffY = 0.0f;

    switch(event.getAction())
    {
        case MotionEvent.ACTION_DOWN:
            //Touch detected, record base X-Y coordinates
            baseX = event.getX();
            baseY = event.getY();

            //As the event is consumed, return true
            result = true;  
            break;

        case MotionEvent.ACTION_MOVE:
            //Swipe started, get current X-Y coordinates and compute those with the base ones
            currentX = event.getX();
            currentY = event.getY();

            current_diffX = currentX - baseX;
            current_diffY = currentY - baseY;

            //...................................................Determine horizontal swipe direction
            if(h_swipe == LEFT_SWIPE)
            {
                if( currentX > previousX )
                {
                    //If here, then horizontal swipe has been reversed
                    h_swipe = RIGHT_SWIPE;

                    //Overwrite base coordinate
                    baseX = previousX;

                    //Recalculate Difference
                    current_diffX = currentX - baseX;
                }
                else
                {
                    //NOP - Intentionally kept empty
                }
            }
            else if(h_swipe == RIGHT_SWIPE)
            {
                if( currentX < previousX )
                {
                    //If here, then horizontal swipe has been reversed
                    h_swipe = LEFT_SWIPE;

                    //Overwrite base coordinate
                    baseX = previousX;

                    //Recalculate Difference
                    current_diffX = currentX - baseX;
                }
                else
                {
                    //NOP - Intentionally kept empty
                }                   
            }
            else
            {
                //If here, then it's a fresh swipe event, so compare with base coordinates
                if( currentX < baseX )
                {
                    h_swipe = LEFT_SWIPE;
                }
                else if( currentX > baseX )
                {
                    h_swipe = RIGHT_SWIPE;
                }
                else
                {
                    //NOP - Intentionally kept empty
                }
            }

            //...................................................Determine vertical swipe direction 
            if(v_swipe == UP_SWIPE)
            {
                if(currentY > previousY)
                {
                    //If here, then vertical swipe has been reversed
                    v_swipe = DOWN_SWIPE;

                    //Overwrite base coordinate
                    baseY = previousY;

                    //Recalculate coordinate difference
                    current_diffY = currentY - baseY;
                }
                else
                {
                    //NOP - Intentionally kept empty
                }
            }
            else if(v_swipe == DOWN_SWIPE)
            {
                if(currentY < previousY)
                {
                    //If here, then vertical swipe has been reversed
                    v_swipe = UP_SWIPE;

                    //Overwrite base coordinate
                    baseY = previousY;

                    //Recalculate coordinate difference
                    current_diffY = currentY - baseY;
                }
                else
                {
                    //NOP - Intentionally kept empty
                }
            }
            else
            {
                //If here, then it's a fresh swipe event, so compare with base coordinates
                if( currentY < baseY )
                {
                    v_swipe = UP_SWIPE;
                }
                else if( currentY > baseY )
                {
                    v_swipe = DOWN_SWIPE;
                }
                else
                {
                    //NOP - Intentionally kept empty
                }
            }

            //Record current coordinates for future comparisons
            previousX = currentX;
            previousY = currentY;

            //................................Determine the prominent swipe (horizontal/vertical)
            if(Math.abs(current_diffX) > Math.abs(current_diffY))
            {
                //It's a horizontal swipe
                if (Math.abs(current_diffX) > SWIPE_THRESHOLD) 
                {
                    if (current_diffX > 0) 
                    {
                        onRightSwipe();
                    } 
                    else 
                    {
                        onLeftSwipe();
                    }
                }
                else
                {
                    //Not enough swipe movement, ignore.
                    //NOP - Intentionally kept empty
                }
            }
            else
            {
                //It's a vertical swipe
                if (Math.abs(current_diffY) > SWIPE_THRESHOLD) 
                {
                    if (current_diffY > 0) 
                    {
                        onDownSwipe();
                    } 
                    else 
                    {
                        onUpSwipe();
                    }
                }
                else
                {
                    //Not enough swipe movement, ignore.
                    //NOP - Intentionally kept empty
                }                   
            }

            //As the event is consumed, return true
            result = true;
            break;

        case MotionEvent.ACTION_UP:
            //Swipe ended, clear variables, if necessary
            h_swipe = NO_H_SWIPE;
            v_swipe = NO_V_SWIPE;

            baseX = 0.0f;
            baseY = 0.0f;

            previousX = 0.0f;
            previousY = 0.0f;

            //As the event is consumed, return true
            result = true;              
            break;

        default:

            //Do not consume event
            result = false;
            break;
    }

    return result;
}


public void onUpSwipe()
{

}

public void onDownSwipe()
{

}

public void onRightSwipe()
{

}

public void onLeftSwipe()
{

}
}
like image 36
zeuschyant8 Avatar answered Dec 21 '22 10:12

zeuschyant8