Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
private class SpringFingerFollow implements OnTouchListener {
private final Point mScreenSize;
private Spring mSpringX;
private Spring mSpringY;
private SpringConfig mSpringConfig = new SpringConfig(30, 8);
private VelocityTracker mVelocityTracker;
private SpringFingerFollow() {
SpringSystem springSystem = SpringSystem.create();
mSpringX = springSystem.createSpring();
mSpringX.addListener(new SimpleSpringListener() {
@Override
public void onSpringUpdate(Spring spring) {
int value = (int) spring.getCurrentValue();
Log.d("ACHTUNG springX", "cur value: " + value);
windowManager.updateViewLayout(FlyingBall.this, params);
params.x = value;
invalidate();
}
});
mSpringY = springSystem.createSpring();
mSpringY.addListener(new SimpleSpringListener() {
@Override
public void onSpringUpdate(Spring spring) {
int value = (int) spring.getCurrentValue();
Log.d("ACHTUNG springY", "cur value: " + value);
windowManager.updateViewLayout(FlyingBall.this, params);
params.y = value;
invalidate();
}
});
mSpringX.setSpringConfig(mSpringConfig);
mSpringY.setSpringConfig(mSpringConfig);
Display display = windowManager.getDefaultDisplay();
mScreenSize = new Point();
display.getSize(mScreenSize);
}
@Override
public boolean onTouch(View v, MotionEvent event) {
Log.d("ACHTUNG touch", "x:" + event.getRawX() + ", y:" + event.getRawY() + "(" + event.getAction() + ")");
switch (event.getAction()) {
case MotionEvent.ACTION_DOWN:
mVelocityTracker = VelocityTracker.obtain();
mVelocityTracker.addMovement(event);
break;
case MotionEvent.ACTION_MOVE:
mSpringX.setEndValue(event.getRawX());
mSpringY.setEndValue(event.getRawY());
mVelocityTracker.addMovement(event);
break;
case MotionEvent.ACTION_UP:
mVelocityTracker.addMovement(event);
double velocityX = mSpringX.getVelocity();
double velocityY = mSpringY.getVelocity();
Point destinationPoint = computeDestinationPoint(((int)velocityX), ((int)velocityY));
// mVelocityTracker.computeCurrentVelocity(1000);
// int xVelocity = (int) mVelocityTracker.getXVelocity();
// int yVelocity = (int) mVelocityTracker.getYVelocity();
// Toast.makeText(getContext(), "Vx=" +xVelocity + ", Vy=" + yVelocity, Toast.LENGTH_SHORT).show();
// Point destinationPoint = computeDestinationPoint(30, 15);
// Point destinationPoint = computeDestinationPoint(30, 15);
mSpringX.setEndValue(destinationPoint.x);
mSpringY.setEndValue(destinationPoint.y);
break;
}
return false;
}
private Point computeDestinationPoint(int velocityX, int velocityY) {
double timeX = getTimeOfArrivalOnAxis(params.x, velocityX, true);
double timeY = getTimeOfArrivalOnAxis(params.y, velocityY, false);
double time = timeX < timeY ? timeX : timeY;
int distanceX = (int) (time * velocityX);
int distanceY = (int) (time * velocityY);
return new Point(params.x + distanceX, params.y + distanceY);
}
private double getTimeOfArrivalOnAxis(int currentPositionOnAxis, int velocity, boolean isAxisX) {
if (velocity == 0.0) {
return Double.MAX_VALUE;
}
int lastPosition;
if (velocity > 0) {
lastPosition = isAxisX ? mScreenSize.x : mScreenSize.y;
} else {
lastPosition = 0;
}
int distance = lastPosition - currentPositionOnAxis;
return ((double)distance) / velocity;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.