Skip to content

Instantly share code, notes, and snippets.

@yuriyskulskiy
Last active August 17, 2020 18:57
Show Gist options
  • Save yuriyskulskiy/44785315c1efb037a07d8a2a828b2a33 to your computer and use it in GitHub Desktop.
Save yuriyskulskiy/44785315c1efb037a07d8a2a828b2a33 to your computer and use it in GitHub Desktop.
AnimatedLayout part 4: update AnimatedLayout.java
...
private int mIdleState = SUMMER_STATE; // set initial state as SUMMER_STATE
...
public void animateBy(int dy) {
float newOffset;
if (dy > 0) {
newOffset = (mOffsetValue - dy) < 0 ? 0 : mOffsetValue - dy;
} else {
//scroll to the left
newOffset = (mOffsetValue - dy) > getWidth() ? getWidth() : mOffsetValue - dy;
}
if (mCurrentAnimation == IDLE_ANIMATION_STATE) {
//start from idle state
mCurrentAnimation = (dy < 0) ? FROM_LEFT_TO_RIGHT : FROM_RIGHT_TO_LEFT;
performScreenShot();
if (mIdleState == SUMMER_STATE) {
applyWinter();
} else {
applySummer();
}
}
if (newOffset == 0 || newOffset == getWidth()) {
mIdleState = newOffset == 0 ? WINTER_STATE : SUMMER_STATE;
if (mIdleState == SUMMER_STATE) {
applySummer();
} else {
applyWinter();
}
mCurrentAnimation = IDLE_ANIMATION_STATE;
}
updateOffset(newOffset);
invalidate();
}
public boolean isForwardAnimationPossible() {
return mOffsetValue > 0;
}
public boolean isReverseAnimationPossible() {
return mOffsetValue < getWidth();
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment