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

The following examples show how to use com.facebook.rebound.Spring#setEndValue() . 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: SwitchButton.java    From FimiX8-RE with MIT License 6 votes vote down vote up
private void takeEffect(boolean animate) {
    double d = 1.0d;
    if (animate) {
        Spring spring = this.spring;
        if (!this.toggleOn) {
            d = 0.0d;
        }
        spring.setEndValue(d);
        return;
    }
    double d2;
    Spring spring2 = this.spring;
    if (this.toggleOn) {
        d2 = 1.0d;
    } else {
        d2 = 0.0d;
    }
    spring2.setCurrentValue(d2);
    if (!this.toggleOn) {
        d = 0.0d;
    }
    calculateEffect(d);
}
 
Example 2
Source File: SpringFloatingActionMenu.java    From SpringFloatingActionMenu with Apache License 2.0 6 votes vote down vote up
private void applyBloomOpenAnimation() {
    final SpringSystem springSystem = SpringSystem.create();

    for (int i = 0; i < mMenuItemCount; i++) {
        // create the springs that control movement
        final Spring springX = springSystem.createSpring();
        final Spring springY = springSystem.createSpring();

        MenuItemView menuItemView = mMenuItemViews.get(i);
        springX.addListener(new MapPerformer(menuItemView, View.X, mFAB.getLeft(), menuItemView.getLeft()));
        springY.addListener(new MapPerformer(menuItemView, View.Y, mFAB.getTop(), menuItemView.getTop()));
        DestroySelfSpringListener destroySelfSpringListener = new DestroySelfSpringListener(this,mContainerView,true);
        springX.addListener(destroySelfSpringListener);
        springY.addListener(destroySelfSpringListener);
        springX.setEndValue(1);
        springY.setEndValue(1);
    }
}
 
Example 3
Source File: SpringFloatingActionMenu.java    From SpringFloatingActionMenu with Apache License 2.0 6 votes vote down vote up
private void applyBloomCloseAnimation() {
    final SpringSystem springSystem = SpringSystem.create();

    for (int i = 0; i < mMenuItemCount; i++) {
        // create the springs that control movement
        final Spring springX = springSystem.createSpring();
        final Spring springY = springSystem.createSpring();

        MenuItemView menuItemView = mMenuItemViews.get(i);
        springX.addListener(new MapPerformer(menuItemView, View.X, menuItemView.getLeft(), mFAB.getLeft()));
        springY.addListener(new MapPerformer(menuItemView, View.Y, menuItemView.getTop(), mFAB.getTop()));
        DestroySelfSpringListener destroySelfSpringListener = new DestroySelfSpringListener(this,mContainerView,false);
        springX.addListener(destroySelfSpringListener);
        springY.addListener(destroySelfSpringListener);
        springX.setEndValue(1);
        springY.setEndValue(1);
    }
}
 
Example 4
Source File: ExplosionFragment.java    From Backboard with Apache License 2.0 5 votes vote down vote up
private static void createCircle(Context context, ViewGroup rootView,
                                 SpringSystem springSystem,
                                 SpringConfig coasting,
                                 SpringConfig gravity,
                                 int diameter,
                                 Drawable backgroundDrawable) {

	final Spring xSpring = springSystem.createSpring().setSpringConfig(coasting);
	final Spring ySpring = springSystem.createSpring().setSpringConfig(gravity);

	// create view
	View view = new View(context);

	RelativeLayout.LayoutParams params = new RelativeLayout.LayoutParams(diameter, diameter);
	params.addRule(RelativeLayout.CENTER_IN_PARENT);
	view.setLayoutParams(params);
	view.setBackgroundDrawable(backgroundDrawable);

	rootView.addView(view);

	// generate random direction and magnitude
	double magnitude = Math.random() * 1000 + 3000;
	double angle = Math.random() * Math.PI / 2 + Math.PI / 4;

	xSpring.setVelocity(magnitude * Math.cos(angle));
	ySpring.setVelocity(-magnitude * Math.sin(angle));

	int maxX = rootView.getMeasuredWidth() / 2 + diameter;
	xSpring.addListener(new Destroyer(rootView, view, -maxX, maxX));

	int maxY = rootView.getMeasuredHeight() / 2 + diameter;
	ySpring.addListener(new Destroyer(rootView, view, -maxY, maxY));

	xSpring.addListener(new Performer(view, View.TRANSLATION_X));
	ySpring.addListener(new Performer(view, View.TRANSLATION_Y));

	// set a different end value to cause the animation to play
	xSpring.setEndValue(2);
	ySpring.setEndValue(9001);
}
 
Example 5
Source File: FlowerFragment.java    From Backboard with Apache License 2.0 4 votes vote down vote up
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
                         Bundle savedInstanceState) {

	mRootView = (RelativeLayout) inflater.inflate(R.layout.fragment_flower, container, false);

	mCircles = new View[6];
	mCircle = mRootView.findViewById(R.id.circle);

	final float diameter = TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, DIAMETER,
			getResources().getDisplayMetrics());

	final TypedArray circles = getResources().obtainTypedArray(R.array.circles);

	// layout params
	final RelativeLayout.LayoutParams params = new RelativeLayout.LayoutParams((int) diameter,
			(int) diameter);
	params.addRule(RelativeLayout.CENTER_IN_PARENT);

	// create the circle views
	int colorIndex = 0;
	for (int i = 0; i < mCircles.length; i++) {
		mCircles[i] = new View(getActivity());

		mCircles[i].setLayoutParams(params);

		mCircles[i].setBackgroundDrawable(getResources().getDrawable(
				circles.getResourceId(colorIndex, -1)));

		colorIndex++;
		if (colorIndex >= circles.length()) {
			colorIndex = 0;
		}

		mRootView.addView(mCircles[i], 0);
	}

	circles.recycle();

	/* Animations! */

	final SpringSystem springSystem = SpringSystem.create();

	// create spring
	final Spring spring = springSystem.createSpring();

	// add listeners along arc
	final double arc = 2 * Math.PI / mCircles.length;

	for (int i = 0; i < mCircles.length; i++) {
		View view = mCircles[i];

		// map spring to a line segment from the center to the edge of the ring
		spring.addListener(new MapPerformer(view, View.TRANSLATION_X, 0, 1,
				0, (float) (RING_DIAMETER * Math.cos(i * arc))));

		spring.addListener(new MapPerformer(view, View.TRANSLATION_Y, 0, 1,
				0, (float) (RING_DIAMETER * Math.sin(i * arc))));

		spring.setEndValue(CLOSED);
	}

	final ToggleImitator imitator = new ToggleImitator(spring, CLOSED, OPEN);

	// move circle using finger, snap when near another circle, and bloom when touched
	new Actor.Builder(SpringSystem.create(), mCircle)
			.addMotion(new SnapImitator(MotionProperty.X), View.TRANSLATION_X)
			.addMotion(new SnapImitator(MotionProperty.Y), View.TRANSLATION_Y)
			.onTouchListener(imitator)
			.build();

	return mRootView;
}
 
Example 6
Source File: BloomFragment.java    From Backboard with Apache License 2.0 4 votes vote down vote up
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
                         Bundle savedInstanceState) {
	mRootView = (RelativeLayout) inflater.inflate(R.layout.fragment_bloom, container, false);

	mCircles = new View[6];

	float diameter = TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, DIAMETER,
			getResources().getDisplayMetrics());

	final TypedArray circles = getResources().obtainTypedArray(R.array.circles);

	// layout params
	RelativeLayout.LayoutParams params = new RelativeLayout.LayoutParams((int) diameter,
			(int) diameter);
	params.addRule(RelativeLayout.CENTER_IN_PARENT);

	// create the circle views
	int colorIndex = 0;
	for (int i = 0; i < mCircles.length; i++) {
		mCircles[i] = new View(getActivity());

		mCircles[i].setLayoutParams(params);

		mCircles[i].setBackgroundDrawable(getResources().getDrawable(circles
				.getResourceId(colorIndex, -1)));

		colorIndex++;
		if (colorIndex >= circles.length()) {
			colorIndex = 0;
		}

		mRootView.addView(mCircles[i]);
	}

	circles.recycle();

	/* Animations! */

	final SpringSystem springSystem = SpringSystem.create();

	// create spring
	final Spring spring = springSystem.createSpring();

	// add listeners along arc
	double arc = 2 * Math.PI / mCircles.length;

	for (int i = 0; i < mCircles.length; i++) {
		View view = mCircles[i];

		// map spring to a line segment from the center to the edge of the ring
		spring.addListener(new MapPerformer(view, View.TRANSLATION_X, 0, 1,
				0, (float) (RING_DIAMETER * Math.cos(i * arc))));

		spring.addListener(new MapPerformer(view, View.TRANSLATION_Y, 0, 1,
				0, (float) (RING_DIAMETER * Math.sin(i * arc))));

		spring.setEndValue(CLOSED);
	}

	mRootView.setOnTouchListener(new ToggleImitator(spring, CLOSED, OPEN));

	return mRootView;
}
 
Example 7
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 8
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 9
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);
        }
    }
}