Skip to content

Instantly share code, notes, and snippets.

@hsiaoer
Created June 11, 2019 16:08
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save hsiaoer/b362d77b9d9c5f9da7beb5997edac566 to your computer and use it in GitHub Desktop.
Save hsiaoer/b362d77b9d9c5f9da7beb5997edac566 to your computer and use it in GitHub Desktop.
Animation for the Background Sky
// Start the animation
mImageView.post(new Runnable() {
@Override
public void run() {
mScaleFactor = (float) mImageView.getHeight() / (float) mImageView.getDrawable().getIntrinsicHeight();
mMatrix.postScale(mScaleFactor, mScaleFactor);
mImageView.setImageMatrix(mMatrix);
animate();
}
});
// ...
// Create a display rect and matrix for the animation
// Movement should be from right to left.
private void animate() {
int width = mImageView.getDrawable().getIntrinsicWidth();
int height = mImageView.getDrawable().getIntrinsicHeight();
mDisplayRect.set(0, 0, width, height);
mMatrix.mapRect(mDisplayRect);
animate(mDisplayRect.left, mDisplayRect.left - (mDisplayRect.right - mImageView.getWidth()));
}
// Use a ValueAnimator to update the view displayed.
private void animate(float from, float to) {
mCurrentAnimator = ValueAnimator.ofFloat(from, to);
mCurrentAnimator.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() {
@Override
public void onAnimationUpdate(ValueAnimator animation) {
float value = (Float) animation.getAnimatedValue();
mMatrix.reset();
mMatrix.postScale(mScaleFactor, mScaleFactor);
mMatrix.postTranslate(value, 0);
mImageView.setImageMatrix(mMatrix);
}
});
// Animation takes place over 5s
mCurrentAnimator.setDuration(5000L);
mCurrentAnimator.addListener(new AnimatorListenerAdapter() {
@Override
public void onAnimationEnd(Animator animation) {
closeButton.setVisibility(View.VISIBLE);
}
});
mCurrentAnimator.start();
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment