use of com.facebook.rebound.Spring in project Backboard by tumblr.
the class AppearFragment method onCreateView.
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
mRootView = inflater.inflate(R.layout.fragment_appear, container, false);
// grab the circles
mCircles = new View[6];
mCircles[0] = mRootView.findViewById(R.id.circle0);
mCircles[1] = mRootView.findViewById(R.id.circle1);
mCircles[2] = mRootView.findViewById(R.id.circle2);
mCircles[3] = mRootView.findViewById(R.id.circle3);
mCircles[4] = mRootView.findViewById(R.id.circle4);
mCircles[5] = mRootView.findViewById(R.id.circle5);
final SpringSystem springSystem = SpringSystem.create();
final Spring[] springs = new Spring[6];
final Actor[] actors = new Actor[6];
// the selected view should move to heaven and the unselected ones should go to hell
View.OnClickListener select = new View.OnClickListener() {
@Override
public void onClick(View v) {
Spring spring = (Spring) v.getTag();
// get root location so we can compensate for it
int[] rootLocation = new int[2];
mRootView.getLocationInWindow(rootLocation);
int[] location = new int[2];
for (int i = 0; i < mCircles.length; i++) {
actors[i].setTouchEnabled(false);
for (Actor.Motion motion : actors[i].getMotions()) {
for (EventImitator imitator : motion.getImitators()) {
if (imitator instanceof MotionImitator) {
final MotionImitator motionImitator = (MotionImitator) imitator;
if (motionImitator.getProperty() == MotionProperty.Y) {
// TODO: disable the y-motion because it is about to be animated
// imitator.getSpring().deregister();
} else {
imitator.release(null);
}
}
}
}
mCircles[i].getLocationInWindow(location);
if (springs[i] == spring) {
// goes to the top
springs[i].setEndValue(-location[1] + rootLocation[1] - v.getMeasuredHeight());
} else {
// go back to the bottom
springs[i].setEndValue(mRootView.getMeasuredHeight() - location[1] + rootLocation[1] + 2 * Math.random() * mCircles[i].getMeasuredHeight());
}
}
}
};
// attach listeners
for (int i = 0; i < mCircles.length; i++) {
springs[i] = springSystem.createSpring();
springs[i].addListener(new Performer(mCircles[i], View.TRANSLATION_Y));
mCircles[i].setTag(springs[i]);
mCircles[i].setOnClickListener(select);
actors[i] = new Actor.Builder(springSystem, mCircles[i]).addTranslateMotion(MotionProperty.X).addTranslateMotion(MotionProperty.Y).build();
}
mRootView.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
// grab location of root view so we can compensate for it
int[] rootLocation = new int[2];
v.getLocationInWindow(rootLocation);
int[] location = new int[2];
for (int i = 0; i < mCircles.length; i++) {
if (springs[i].getEndValue() == 0) {
// hide
mCircles[i].getLocationInWindow(location);
// if the end values are different, they will move at different speeds
springs[i].setEndValue(mRootView.getMeasuredHeight() - location[1] + rootLocation[1] + 2 * Math.random() * mCircles[i].getMeasuredHeight());
} else {
actors[i].setTouchEnabled(true);
for (Actor.Motion motion : actors[i].getMotions()) {
for (EventImitator imitator : motion.getImitators()) {
if (imitator instanceof MotionImitator) {
final MotionImitator motionImitator = (MotionImitator) imitator;
imitator.getSpring().setCurrentValue(0);
// TODO: re-enable the y motion.
// if (imitator.getProperty() == MotionProperty.Y &&
// !imitator.getSpring().isRegistered()) {
// imitator.getSpring().register();
// }
}
}
}
// appear
springs[i].setEndValue(0);
}
}
}
});
return mRootView;
}
use of com.facebook.rebound.Spring in project Backboard by tumblr.
the class BloomFragment method onCreateView.
@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;
}
use of com.facebook.rebound.Spring in project Backboard by tumblr.
the class FlowerFragment method onCreateView.
@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;
}
use of com.facebook.rebound.Spring in project Backboard by tumblr.
the class FollowFragment method onCreateView.
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
mRootView = (ViewGroup) inflater.inflate(R.layout.fragment_follow, container, false);
mCircle = mRootView.findViewById(R.id.circle);
FrameLayout.LayoutParams leaderParams = (FrameLayout.LayoutParams) mCircle.getLayoutParams();
mFollowers = new View[4];
float diameter = TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, DIAMETER, getResources().getDisplayMetrics());
TypedArray circles = getResources().obtainTypedArray(R.array.circles);
// create the circle views
int colorIndex = 1;
for (int i = 0; i < mFollowers.length; i++) {
mFollowers[i] = new View(getActivity());
FrameLayout.LayoutParams params = new FrameLayout.LayoutParams((int) diameter, (int) diameter);
params.gravity = leaderParams.gravity;
mFollowers[i].setLayoutParams(params);
mFollowers[i].setBackgroundDrawable(getResources().getDrawable(circles.getResourceId(colorIndex, -1)));
colorIndex++;
if (colorIndex >= circles.length()) {
colorIndex = 0;
}
mRootView.addView(mFollowers[i]);
}
circles.recycle();
/* Animation code */
final SpringSystem springSystem = SpringSystem.create();
// create the springs that control movement
final Spring springX = springSystem.createSpring();
final Spring springY = springSystem.createSpring();
// bind circle movement to events
new Actor.Builder(springSystem, mCircle).addMotion(springX, MotionProperty.X).addMotion(springY, MotionProperty.Y).build();
// add springs to connect between the views
final Spring[] followsX = new Spring[mFollowers.length];
final Spring[] followsY = new Spring[mFollowers.length];
for (int i = 0; i < mFollowers.length; i++) {
// create spring to bind views
followsX[i] = springSystem.createSpring();
followsY[i] = springSystem.createSpring();
followsX[i].addListener(new Performer(mFollowers[i], View.TRANSLATION_X));
followsY[i].addListener(new Performer(mFollowers[i], View.TRANSLATION_Y));
// imitates another character
final SpringImitator followX = new SpringImitator(followsX[i]);
final SpringImitator followY = new SpringImitator(followsY[i]);
// imitate the previous character
if (i == 0) {
springX.addListener(followX);
springY.addListener(followY);
} else {
followsX[i - 1].addListener(followX);
followsY[i - 1].addListener(followY);
}
}
return mRootView;
}
use of com.facebook.rebound.Spring in project Backboard by tumblr.
the class ZoomFragment method onCreateView.
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
final View rootView = inflater.inflate(R.layout.fragment_scale, container, false);
final View rect = rootView.findViewById(R.id.rect);
final SpringSystem springSystem = SpringSystem.create();
final Spring spring = springSystem.createSpring();
spring.addListener(new Performer(rect, View.SCALE_X));
spring.addListener(new Performer(rect, View.SCALE_Y));
spring.setCurrentValue(1.0f, true);
final ScaleGestureDetector scaleGestureDetector = new ScaleGestureDetector(getActivity(), new ScaleGestureDetector.SimpleOnScaleGestureListener() {
@Override
public boolean onScale(ScaleGestureDetector detector) {
spring.setCurrentValue(spring.getCurrentValue() * detector.getScaleFactor(), true);
return true;
}
@Override
public void onScaleEnd(ScaleGestureDetector detector) {
spring.setEndValue(1.0f);
}
});
rootView.setOnTouchListener(new View.OnTouchListener() {
@Override
@SuppressLint("ClickableViewAccessibility")
public boolean onTouch(View v, MotionEvent event) {
return scaleGestureDetector.onTouchEvent(event);
}
});
return rootView;
}
Aggregations