Java Code Examples for com.facebook.rebound.Spring#setSpringConfig()

The following examples show how to use com.facebook.rebound.Spring#setSpringConfig() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: SpringHelper.java    From FloatingView with Apache License 2.0 5 votes vote down vote up
public void start(){
    SpringSystem springSystem = SpringSystem.create();
    Spring spring  = springSystem.createSpring();
    if (mConfig == 0){
        spring.setSpringConfig(SpringConfig.fromBouncinessAndSpeed(mConfigValueOne, mConfigValueTwo));
    }else if (mConfig == 1){
        spring.setSpringConfig(SpringConfig.fromOrigamiTensionAndFriction(mConfigValueOne, mConfigValueTwo));
    }
    start(spring);
}
 
Example 2
Source File: ChatHead.java    From springy-heads with Apache License 2.0 4 votes vote down vote up
@Override
public boolean onTouchEvent(@NonNull MotionEvent event) {
    super.onTouchEvent(event);

    if(xPositionSpring==null || yPositionSpring==null) return false;
    //Chathead view will set the correct active springs on touch
    Spring activeHorizontalSpring = xPositionSpring;
    Spring activeVerticalSpring = yPositionSpring;

    int action = event.getAction();
    float rawX = event.getRawX();
    float rawY = event.getRawY();
    float offsetX = rawX - downX;
    float offsetY = rawY - downY;
    boolean showCloseButton = manager.getActiveArrangement().shouldShowCloseButton(this);
    event.offsetLocation(manager.getChatHeadContainer().getViewX(this), manager.getChatHeadContainer().getViewY(this));
    if (action == MotionEvent.ACTION_DOWN) {
        if (velocityTracker == null) {
            velocityTracker = VelocityTracker.obtain();
        } else {
            velocityTracker.clear();

        }
        activeHorizontalSpring.setSpringConfig(SpringConfigsHolder.NOT_DRAGGING);
        activeVerticalSpring.setSpringConfig(SpringConfigsHolder.NOT_DRAGGING);
        setState(ChatHead.State.FREE);
        downX = rawX;
        downY = rawY;
        downTranslationX = (float) activeHorizontalSpring.getCurrentValue();
        downTranslationY = (float) activeVerticalSpring.getCurrentValue();
        scaleSpring.setEndValue(.9f);
        activeHorizontalSpring.setAtRest();
        activeVerticalSpring.setAtRest();
        velocityTracker.addMovement(event);


    } else if (action == MotionEvent.ACTION_MOVE) {
        if (Math.hypot(offsetX, offsetY) > touchSlop) {
            isDragging = true;
            if (showCloseButton) {
                manager.getCloseButton().appear();
            }
        }
        velocityTracker.addMovement(event);

        if (isDragging) {
            manager.getCloseButton().pointTo(rawX, rawY);
            if (manager.getActiveArrangement().canDrag(this)) {
                double distanceCloseButtonFromHead = manager.getDistanceCloseButtonFromHead(rawX, rawY);
                if (distanceCloseButtonFromHead < CLOSE_ATTRACTION_THRESHOLD && showCloseButton) {
                    setState(ChatHead.State.CAPTURED);
                    activeHorizontalSpring.setSpringConfig(SpringConfigsHolder.NOT_DRAGGING);
                    activeVerticalSpring.setSpringConfig(SpringConfigsHolder.NOT_DRAGGING);
                    int[] coords = manager.getChatHeadCoordsForCloseButton(this);
                    activeHorizontalSpring.setEndValue(coords[0]);
                    activeVerticalSpring.setEndValue(coords[1]);
                    manager.getCloseButton().onCapture();

                } else {
                    setState(ChatHead.State.FREE);
                    activeHorizontalSpring.setSpringConfig(SpringConfigsHolder.DRAGGING);
                    activeVerticalSpring.setSpringConfig(SpringConfigsHolder.DRAGGING);
                    activeHorizontalSpring.setCurrentValue(downTranslationX + offsetX);
                    activeVerticalSpring.setCurrentValue(downTranslationY + offsetY);
                    manager.getCloseButton().onRelease();
                }

                velocityTracker.computeCurrentVelocity(1000);
            }

        }

    } else {
        if (action == MotionEvent.ACTION_UP || action == MotionEvent.ACTION_CANCEL) {
            boolean wasDragging = isDragging;
            activeHorizontalSpring.setSpringConfig(SpringConfigsHolder.DRAGGING);
            activeHorizontalSpring.setSpringConfig(SpringConfigsHolder.DRAGGING);
            isDragging = false;
            scaleSpring.setEndValue(1);
            int xVelocity = (int) velocityTracker.getXVelocity();
            int yVelocity = (int) velocityTracker.getYVelocity();
            velocityTracker.recycle();
            velocityTracker = null;
            if(xPositionSpring!=null && yPositionSpring!=null) {
                boolean touchUpHandled = manager.getActiveArrangement().handleTouchUp(this, xVelocity, yVelocity, activeHorizontalSpring, activeVerticalSpring, wasDragging);
            }
        }
    }

    return true;
}
 
Example 3
Source File: MinimizedArrangement.java    From springy-heads with Apache License 2.0 4 votes vote down vote up
@Override
public void onSpringUpdate(ChatHead activeChatHead, boolean isDragging, int maxWidth, int maxHeight, Spring spring, Spring activeHorizontalSpring, Spring activeVerticalSpring, int totalVelocity) {
    /** This method does a bounds Check **/
    double xVelocity = activeHorizontalSpring.getVelocity();
    double yVelocity = activeVerticalSpring.getVelocity();
    if (!isDragging && Math.abs(totalVelocity) < MIN_VELOCITY_TO_POSITION_BACK && activeChatHead == hero) {

        if (Math.abs(totalVelocity) < MAX_VELOCITY_FOR_IDLING && activeChatHead.getState() == ChatHead.State.FREE && hasActivated) {
            setIdleStateX((int) activeHorizontalSpring.getCurrentValue());
            setIdleStateY((int) activeVerticalSpring.getCurrentValue());
        }
        if (spring == activeHorizontalSpring) {

            double xPosition = activeHorizontalSpring.getCurrentValue();
            if (xPosition + manager.getConfig().getHeadWidth() > maxWidth && activeHorizontalSpring.getVelocity() > 0) {
                //outside the right bound
                //System.out.println("outside the right bound !! xPosition = " + xPosition);
                int newPos = maxWidth - manager.getConfig().getHeadWidth();
                activeHorizontalSpring.setSpringConfig(SpringConfigsHolder.NOT_DRAGGING);
                activeHorizontalSpring.setEndValue(newPos);
            } else if (xPosition < 0 && activeHorizontalSpring.getVelocity() < 0) {
                //outside the left bound
                //System.out.println("outside the left bound !! xPosition = " + xPosition);
                activeHorizontalSpring.setSpringConfig(SpringConfigsHolder.NOT_DRAGGING);
                activeHorizontalSpring.setEndValue(0);

            } else {
                //within bound


            }
        } else if (spring == activeVerticalSpring) {
            double yPosition = activeVerticalSpring.getCurrentValue();
            if (yPosition + manager.getConfig().getHeadWidth() > maxHeight && activeVerticalSpring.getVelocity() > 0) {
                //outside the bottom bound
                //System.out.println("outside the bottom bound !! yPosition = " + yPosition);

                activeVerticalSpring.setSpringConfig(SpringConfigsHolder.NOT_DRAGGING);
                activeVerticalSpring.setEndValue(maxHeight - manager.getConfig().getHeadHeight());
            } else if (yPosition < 0 && activeVerticalSpring.getVelocity() < 0) {
                //outside the top bound
                //System.out.println("outside the top bound !! yPosition = " + yPosition);

                activeVerticalSpring.setSpringConfig(SpringConfigsHolder.NOT_DRAGGING);
                activeVerticalSpring.setEndValue(0);
            } else {
                //within bound
            }

        }
    }

    if (!isDragging && activeChatHead == hero) {

        /** Capturing check **/


        int[] coords = manager.getChatHeadCoordsForCloseButton(activeChatHead);
        double distanceCloseButtonFromHead = manager.getDistanceCloseButtonFromHead((float) activeHorizontalSpring.getCurrentValue() + manager.getConfig().getHeadWidth() / 2, (float) activeVerticalSpring.getCurrentValue() + manager.getConfig().getHeadHeight() / 2);

        if (distanceCloseButtonFromHead < activeChatHead.CLOSE_ATTRACTION_THRESHOLD && activeHorizontalSpring.getSpringConfig() == SpringConfigsHolder.DRAGGING && activeVerticalSpring.getSpringConfig() == SpringConfigsHolder.DRAGGING) {
            activeHorizontalSpring.setSpringConfig(SpringConfigsHolder.NOT_DRAGGING);
            activeVerticalSpring.setSpringConfig(SpringConfigsHolder.NOT_DRAGGING);
            activeChatHead.setState(ChatHead.State.CAPTURED);

        }
        if (activeChatHead.getState() == ChatHead.State.CAPTURED && activeHorizontalSpring.getSpringConfig() != SpringConfigsHolder.CAPTURING) {
            activeHorizontalSpring.setAtRest();
            activeVerticalSpring.setAtRest();
            activeHorizontalSpring.setSpringConfig(SpringConfigsHolder.CAPTURING);
            activeVerticalSpring.setSpringConfig(SpringConfigsHolder.CAPTURING);
            activeHorizontalSpring.setEndValue(coords[0]);
            activeVerticalSpring.setEndValue(coords[1]);

        }
        if (activeChatHead.getState() == ChatHead.State.CAPTURED && activeVerticalSpring.isAtRest()) {
            manager.getCloseButton().disappear(false, true);
            manager.captureChatHeads(activeChatHead);
        }
        if (!activeVerticalSpring.isAtRest() && !isTransitioning) {
            manager.getCloseButton().appear();
        } else {
            manager.getCloseButton().disappear(true, true);
        }
    }


}
 
Example 4
Source File: MaximizedArrangement.java    From springy-heads with Apache License 2.0 4 votes vote down vote up
@Override
public void onSpringUpdate(ChatHead activeChatHead, boolean isDragging, int maxWidth, int maxHeight, Spring spring, Spring activeHorizontalSpring, Spring activeVerticalSpring, int totalVelocity) {
    /** Bounds Check **/
    if (spring == activeHorizontalSpring && !isDragging) {
        double xPosition = activeHorizontalSpring.getCurrentValue();
        if (xPosition + manager.getConfig().getHeadWidth() > maxWidth && activeHorizontalSpring.getSpringConfig() != SpringConfigsHolder.NOT_DRAGGING && !activeHorizontalSpring.isOvershooting()) {
            positionToOriginal(activeChatHead, activeHorizontalSpring, activeVerticalSpring);
        }
        if (xPosition < 0 && activeHorizontalSpring.getSpringConfig() != SpringConfigsHolder.NOT_DRAGGING && !activeHorizontalSpring.isOvershooting()) {
            positionToOriginal(activeChatHead, activeHorizontalSpring, activeVerticalSpring);
        }
    } else if (spring == activeVerticalSpring && !isDragging) {
        double yPosition = activeVerticalSpring.getCurrentValue();

        if (yPosition + manager.getConfig().getHeadHeight() > maxHeight && activeHorizontalSpring.getSpringConfig() != SpringConfigsHolder.NOT_DRAGGING && !activeHorizontalSpring.isOvershooting()) {
            positionToOriginal(activeChatHead, activeHorizontalSpring, activeVerticalSpring);
        }
        if (yPosition < 0 && activeHorizontalSpring.getSpringConfig() != SpringConfigsHolder.NOT_DRAGGING && !activeHorizontalSpring.isOvershooting()) {
            positionToOriginal(activeChatHead, activeHorizontalSpring, activeVerticalSpring);
        }

    }

    /** position it back **/
    if (!isDragging && totalVelocity < MIN_VELOCITY_TO_POSITION_BACK && activeHorizontalSpring.getSpringConfig() == SpringConfigsHolder.DRAGGING) {
        positionToOriginal(activeChatHead, activeHorizontalSpring, activeVerticalSpring);

    }

    if (activeChatHead == currentChatHead)

        showOrHideView(activeChatHead);

    if (!isDragging) {
        /** Capturing check **/
        int[] coords = manager.getChatHeadCoordsForCloseButton(activeChatHead);
        double distanceCloseButtonFromHead = manager.getDistanceCloseButtonFromHead((float) activeHorizontalSpring.getCurrentValue() + manager.getConfig().getHeadWidth() / 2, (float) activeVerticalSpring.getCurrentValue() + manager.getConfig().getHeadHeight() / 2);

        if (distanceCloseButtonFromHead < activeChatHead.CLOSE_ATTRACTION_THRESHOLD && activeHorizontalSpring.getSpringConfig() == SpringConfigsHolder.DRAGGING && activeVerticalSpring.getSpringConfig() == SpringConfigsHolder.DRAGGING && !activeChatHead.isSticky()) {

            activeHorizontalSpring.setSpringConfig(SpringConfigsHolder.NOT_DRAGGING);
            activeVerticalSpring.setSpringConfig(SpringConfigsHolder.NOT_DRAGGING);
            activeChatHead.setState(ChatHead.State.CAPTURED);
        }
        if (activeChatHead.getState() == ChatHead.State.CAPTURED && activeHorizontalSpring.getSpringConfig() != SpringConfigsHolder.CAPTURING) {
            activeHorizontalSpring.setAtRest();
            activeVerticalSpring.setAtRest();
            activeHorizontalSpring.setSpringConfig(SpringConfigsHolder.CAPTURING);
            activeVerticalSpring.setSpringConfig(SpringConfigsHolder.CAPTURING);
            activeHorizontalSpring.setEndValue(coords[0]);
            activeVerticalSpring.setEndValue(coords[1]);

        }
        if (activeChatHead.getState() == ChatHead.State.CAPTURED && activeVerticalSpring.isAtRest()) {
            manager.getCloseButton().disappear(false, true);
            manager.captureChatHeads(activeChatHead);
        }
        if (!activeVerticalSpring.isAtRest() && !isTransitioning) {
            manager.getCloseButton().appear();
        } else {
            manager.getCloseButton().disappear(true, true);
        }
    }
}
 
Example 5
Source File: Magnet.java    From Magnet with MIT License 4 votes vote down vote up
protected Spring createXSpring(SpringSystem springSystem, SpringConfig config) {
  Spring spring = springSystem.createSpring();
  spring.setSpringConfig(config);
  spring.setRestSpeedThreshold(restVelocity);
  return spring;
}
 
Example 6
Source File: Magnet.java    From Magnet with MIT License 4 votes vote down vote up
protected Spring createYSpring(SpringSystem springSystem, SpringConfig config) {
  Spring spring = springSystem.createSpring();
  spring.setSpringConfig(config);
  spring.setRestSpeedThreshold(restVelocity);
  return spring;
}