if (controller == null) {
throw new NullPointerException();
}
if (controller.hasOffsetAnimator()) {
Animator animator = controller.getOffsetAnimator();
animator.cancel();
controller.setOffsetAnimator(null);
}
Rectangle bounds = controller.getItemBounds();
bounds.x += controller.getOffset();
final int from = bounds.x;
TimingTarget tt = new TimingTargetAdapter() {
@Override
public void timingEvent(Animator source, double fraction) {
int distance = (int )Math.round(((double )to - (double )from) * fraction);
Rectangle bounds = controller.getItemBounds();
int oldOffset = controller.getOffset();
int newOffset = from + distance - bounds.x;
controller.setOffset(newOffset);
if (!hasDraggedItem()) {
Point mouseLocation = getDisplay().getCursorLocation();
mouseLocation = toControl(mouseLocation);
Rectangle testBounds = controller.getItemBounds();
testBounds.x += controller.getOffset();
if (testBounds.contains(mouseLocation)) {
processFocus(controller);
} else {
processFocus(null);
}
}
redraw(bounds.x + oldOffset, bounds.y, bounds.width, bounds.height, true);
redraw(bounds.x + newOffset, bounds.y, bounds.width, bounds.height, true);
}
@Override
public void end(Animator source) {
Rectangle oldBounds = controller.getItemBounds();
oldBounds.x += controller.getOffset();
Rectangle newBounds = controller.getItemBounds();
int newOffset = to - newBounds.x;
controller.setOffset(newOffset);
newBounds.x += newOffset;
redraw(oldBounds.x, oldBounds.y, oldBounds.width, oldBounds.height, true);
redraw(newBounds.x, newBounds.y, newBounds.width, newBounds.height, true);
}
};
Interpolator interpol = new AccelerationInterpolator(0, 0.9);
Animator animator = new Animator.Builder().addTarget(tt).setDuration(160, TimeUnit.MILLISECONDS).setInterpolator(interpol).build();
controller.setOffsetAnimator(animator);
animator.start();
}