korre / android-tv-epg

Classic TV electronic program guide (EPG) with multidirectional scroll
MIT License
160 stars 75 forks source link

How to stop diagonal scrolling #7

Closed vijaykarora closed 7 years ago

vijaykarora commented 7 years ago

I found the way to stop diagonal scroll. Thanks for your help.

korre commented 7 years ago

Great @amagine!

ursurya commented 6 years ago

hi amagine , Can you please tell how to do it . I also want to stop the diagonal scroll.

vijaykarora commented 6 years ago

Kindly update onScroll and onFling method

`@Override public boolean onScroll(MotionEvent e1, MotionEvent e2, float distanceX, float distanceY) { int dx = (int) distanceX; int dy = (int) distanceY; int x = getScrollX(); int y = getScrollY();

        // Avoid over scrolling
        if (x + dx < 0)
        {
            dx = 0 - x;
        }
        if (y + dy < 0)
        {
            dy = 0 - y;
        }
        if (x + dx > mMaxHorizontalScroll)
        {
            dx = mMaxHorizontalScroll - x;
        }
        if (y + dy > mMaxVerticalScroll)
        {
            dy = mMaxVerticalScroll - y;
        }

        float x1 = e1.getX();
        float y1 = e1.getY();

        float x2 = e2.getX();
        float y2 = e2.getY();

        Direction direction = getDirection(x1, y1, x2, y2);

        switch (direction)
        {
            case up:
            case down:
                scrollBy(0, dy);
                break;
            case left:
            case right:
                scrollBy(dx, 0);
                break;
        }
        return true;
    }

    @Override
    public boolean onFling(MotionEvent e1, MotionEvent e2,
                           float vX, float vY)
    {
        float x1 = e1.getX();
        float y1 = e1.getY();

        float x2 = e2.getX();
        float y2 = e2.getY();

        Direction direction = getDirection(x1, y1, x2, y2);

        switch (direction)
        {
            case up:
            case down:
                mScroller.fling(getScrollX(), getScrollY(), 0, -(int) vY, 0, mMaxHorizontalScroll, 0, mMaxVerticalScroll);
                break;
            case left:
            case right:
                mScroller.fling(getScrollX(), getScrollY(), -(int) vX, 0, 0, mMaxHorizontalScroll, 0, mMaxVerticalScroll);
                break;
        }
        redraw();
        return true;
    }

    @Override
    public boolean onDown(MotionEvent e)
    {
        if (!mScroller.isFinished())
        {
            mScroller.forceFinished(true);
            return true;
        }
        return true;
    }

    /**
     * Given two points in the plane p1=(x1, x2) and p2=(y1, y1), this method
     * returns the direction that an arrow pointing from p1 to p2 would have.
     *
     * @param x1 the x position of the first point
     * @param y1 the y position of the first point
     * @param x2 the x position of the second point
     * @param y2 the y position of the second point
     *
     * @return the direction
     */
    private Direction getDirection(float x1, float y1, float x2, float y2)
    {
        double angle = getAngle(x1, y1, x2, y2);
        return Direction.get(angle);
    }

    /**
     * Finds the angle between two points in the plane (x1,y1) and (x2, y2)
     * The angle is measured with 0/360 being the X-axis to the right, angles
     * increase counter clockwise.
     *
     * @param x1 the x position of the first point
     * @param y1 the y position of the first point
     * @param x2 the x position of the second point
     * @param y2 the y position of the second point
     *
     * @return the angle between two points
     */
    private double getAngle(float x1, float y1, float x2, float y2)
    {

        double rad = Math.atan2(y1 - y2, x2 - x1) + Math.PI;
        return (rad * 180 / Math.PI + 180) % 360;
    }
}

private enum Direction
{
    up,
    down,
    left,
    right;

    /**
     * Returns a direction given an angle.
     * Directions are defined as follows:
     * <p>
     * Up: [45, 135]
     * Right: [0,45] and [315, 360]
     * Down: [225, 315]
     * Left: [135, 225]
     *
     * @param angle an angle from 0 to 360 - e
     *
     * @return the direction of an angle
     */
    public static Direction get(double angle)
    {
        if (inRange(angle, 45, 135))
        {
            return Direction.up;
        }
        else if (inRange(angle, 0, 45) || inRange(angle, 315, 360))
        {
            return Direction.right;
        }
        else if (inRange(angle, 225, 315))
        {
            return Direction.down;
        }
        else
        {
            return Direction.left;
        }
    }

    /**
     * @param angle an angle
     * @param init  the initial bound
     * @param end   the final bound
     *
     * @return returns true if the given angle is in the interval [init, end).
     */
    private static boolean inRange(double angle, float init, float end)
    {
        return (angle >= init) && (angle < end);
    }
}`
ursurya commented 6 years ago

@amagine : Thanks for your immediate reply and sharing the code. I tried something similar to this but the scrolling became laggy. I will cross check my code with yours and will update you..Thank you so much

ursurya commented 6 years ago

Thanks @amagine..its working.i found two differences in my code with yours due to which may be its lagging

vijaykarora commented 6 years ago

Cool @ursurya