Git Product home page Git Product logo

Comments (2)

hyuwah avatar hyuwah commented on July 30, 2024

Hi @cryinrain69 , you can use the updated 0.3.0 version of this library
It adds DraggableUtils class so it can be used on java class

See the updated usage / example on Readme

Thanks for the feedback and your interest in using this library 🙏

from draggableview.

chiragthummar avatar chiragthummar commented on July 30, 2024
 float widgetInitialX = 0f;
    float widgetDX = 0f;
    float widgetInitialY = 0f;
    float widgetDY = 0f;

    public void makeDraggable(View view, Draggable.STICKY stickyAxis, boolean animated, DraggableListener mDraggableListener) {
        view.setOnTouchListener(new View.OnTouchListener() {
            @Override
            public boolean onTouch(View v, MotionEvent event) {

                View viewParent = (View) v.getParent();
                float parentHeight = viewParent.getHeight();
                float parentWidth = viewParent.getWidth();
                float xMax = parentWidth - v.getWidth();
                float xMiddle = parentWidth / 2;
                float yMax = parentHeight - v.getHeight();
                float yMiddle = parentHeight / 2;


                switch (event.getActionMasked()) {
                    case MotionEvent.ACTION_DOWN:
                        widgetDX = v.getX() - event.getRawX();
                        widgetDY = v.getY() - event.getRawY();
                        widgetInitialX = v.getX();
                        widgetInitialY = v.getY();
                        break;
                    case MotionEvent.ACTION_MOVE:
                        float newX = event.getRawX() + widgetDX;
                        newX = Math.max(0F, newX);
                        newX = Math.min(xMax, newX);
                        v.setX(newX);

                        float newY = event.getRawY() + widgetDY;
                        newY = Math.max(0F, newY);
                        newY = Math.min(yMax, newY);
                        v.setY(newY);
                        mDraggableListener.onPositionChanged(v);
                        break;
                    case MotionEvent.ACTION_UP:

                        switch (stickyAxis) {
                            case AXIS_X:
                                if (event.getRawX() >= xMiddle) {
                                    if (animated)
                                        v.animate().x(xMax).setDuration(DURATION_MILLIS).setUpdateListener(animation -> mDraggableListener.onPositionChanged(v)).start();
                                    else
                                        v.setX(xMax);
                                } else {
                                    if (animated)
                                        v.animate().x(0F).setDuration(DURATION_MILLIS).setUpdateListener(animation -> mDraggableListener.onPositionChanged(v)).start();
                                    else
                                        v.setX(0F);
                                }
                                break;
                            case AXIS_Y:

                                if (event.getRawY() >= yMiddle) {
                                    if (animated)
                                        v.animate().y(yMax).setDuration(DURATION_MILLIS).setUpdateListener(animation -> mDraggableListener.onPositionChanged(v)).start();
                                    else
                                        v.setY(yMax);
                                } else {
                                    if (animated)
                                        v.animate().y(0F).setDuration(DURATION_MILLIS).setUpdateListener(animation -> mDraggableListener.onPositionChanged(v)).start();
                                    else
                                        v.setY(0F);
                                }
                                break;
                            case AXIS_XY:
                                if (event.getRawX() >= xMiddle) {
                                    if (animated)
                                        v.animate().x(xMax).setDuration(DURATION_MILLIS).setUpdateListener(animation -> mDraggableListener.onPositionChanged(v)).start();
                                    else
                                        v.setX(xMax);
                                } else {
                                    if (animated)
                                        v.animate().x(0F).setDuration(DURATION_MILLIS).setUpdateListener(animation -> mDraggableListener.onPositionChanged(v)).start();
                                    else
                                        v.setX(0F);
                                }

                                if (event.getRawY() >= yMiddle) {
                                    if (animated)
                                        v.animate().y(yMax).setDuration(DURATION_MILLIS).setUpdateListener(animation -> mDraggableListener.onPositionChanged(v)).start();
                                    else
                                        v.setY(yMax);
                                } else {
                                    if (animated)
                                        v.animate().y(0F).setDuration(DURATION_MILLIS).setUpdateListener(animation -> mDraggableListener.onPositionChanged(v)).start();
                                    else
                                        v.setY(0F);
                                }
                                break;
                        }
                        
                        break;
                    default:
                        return false;
                }
                if (Math.abs(v.getX() - widgetInitialX) <= 16 && Math.abs(v.getY() - widgetInitialY) <= 16) {
                    v.performClick();
                }
                return true;
            }
        });
    }

from draggableview.

Related Issues (14)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.