use of com.tumblr.backboard.performer.Performer 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.tumblr.backboard.performer.Performer 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.tumblr.backboard.performer.Performer 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;
}
use of com.tumblr.backboard.performer.Performer in project Backboard by tumblr.
the class ExplosionFragment method createCircle.
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);
}
use of com.tumblr.backboard.performer.Performer in project Backboard by tumblr.
the class ScaleFragment 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));
rootView.setOnTouchListener(new View.OnTouchListener() {
@Override
@SuppressLint("ClickableViewAccessibility")
public boolean onTouch(View v, MotionEvent event) {
switch(event.getAction()) {
case MotionEvent.ACTION_DOWN:
spring.setVelocity(0);
case MotionEvent.ACTION_MOVE:
// can't use Imitation here because there is no nice mapping from
// an event property to a Spring
float scaleX, scaleY;
float delta = event.getX() - (rect.getX() + rect.getMeasuredWidth() / 2);
scaleX = Math.abs(delta) / (rect.getMeasuredWidth() / 2);
delta = event.getY() - (rect.getY() + rect.getMeasuredHeight() / 2);
scaleY = Math.abs(delta) / (rect.getMeasuredHeight() / 2);
float scale = Math.max(scaleX, scaleY);
spring.setEndValue(scale);
break;
case MotionEvent.ACTION_UP:
spring.setEndValue(1f);
break;
}
return true;
}
});
return rootView;
}
Aggregations