Skip to content

Instantly share code, notes, and snippets.

@jt-gilkeson
Last active December 5, 2015 11:42
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 jt-gilkeson/c0ca583df16958172fb0 to your computer and use it in GitHub Desktop.
Save jt-gilkeson/c0ca583df16958172fb0 to your computer and use it in GitHub Desktop.
Glide Crash
public class CrossFade
{
private int mDuration = 1500;
private ViewPropertyAnimator alphaAnim;
public CrossFade(int duration)
{
mDuration = duration;
}
public void crossFadeImage(final ImageView newView, final ImageView oldView, final Drawable newImage, boolean fadeIn)
{
if (alphaAnim != null)
{
// Cancel previous animation so there isn't a flicker when you start the new one
alphaAnim.cancel();
}
if (fadeIn)
{
setImage(newView, newImage, 0f);
// Cross fade image
alphaAnim = AnimationHelper.prepareFadeView(newView, 0f, 1f, 0, mDuration, new AnimatorListenerAdapter()
{
@Override
public void onAnimationEnd(Animator animator)
{
setImage(oldView, newImage);
newView.setVisibility(View.GONE);
}
});
alphaAnim.start();
}
else
{
setImage(oldView, newImage);
}
}
private void setImage(ImageView view, Drawable image)
{
setImage(view, image, 1f);
}
private void setImage(ImageView view, Drawable image, float alpha)
{
view.setAlpha(alpha);
view.setImageDrawable(image);
}
}
// Load all 3 images immediately after crossfade finishes, start new batch on 6 sec timer.
private class SplashboardPostcards
{
private CrossFadePostcard mMainPostcard;
private CrossFadePostcard mTopRightPostcard;
private CrossFadePostcard mBottomRightPostcard;
private int mDownloadSize = 0;
private int mSize = 0;
public SplashboardPostcards(View view)
{
mMainPostcard = new CrossFadePostcard((ImageView)view.findViewById(R.id.sb_postcard_main), (ImageView)view.findViewById(R.id.sb_postcard_main_old));
mTopRightPostcard = new CrossFadePostcard((ImageView)view.findViewById(R.id.sb_postcard_top_right), (ImageView)view.findViewById(R.id.sb_postcard_top_right_old));
mBottomRightPostcard = new CrossFadePostcard((ImageView)view.findViewById(R.id.sb_postcard_bottom_right), (ImageView)view.findViewById(R.id.sb_postcard_bottom_right_old));
}
private void updatePhotos(List<String> photos)
{
mSize = photos.size();
if (mDownloadSize == 0)
{
Activity activity = getActivity();
mDownloadSize = Math.max(ScreenSizeHelper.getScreenHeight(activity),
ScreenSizeHelper.getScreenWidth(activity)) / 2;
}
// Load 1, 2 or 3 images based on size
switch (mSize)
{
case 3:
loadImage(mBottomRightPostcard, photos.get(2));
//fallthrough
case 2:
loadImage(mTopRightPostcard, photos.get(1));
//fallthrough
case 1:
loadImage(mMainPostcard, photos.get(0));
}
}
private void loadImage(final CrossFadePostcard postcard, String photo)
{
Glide.with(SplashboardFragment.this)
.load(photo)
.override(mDownloadSize, mDownloadSize)
.centerCrop()
.into(new GlideDrawableImageViewTarget(postcard.mNewImageView)
{
@Override
public void onResourceReady(GlideDrawable drawable, GlideAnimation anim)
{
crossFadeWhenReady(postcard, drawable);
}
});
}
// This method synchronizes the cross fade for all images
private void crossFadeWhenReady(CrossFadePostcard postcard, Drawable drawable)
{
postcard.mDrawable = drawable;
boolean performedCrossFade = false;
if (mSize >= 3 && mMainPostcard.isReady() && mTopRightPostcard.isReady() && mBottomRightPostcard.isReady())
{
mMainPostcard.performCrossFade();
mTopRightPostcard.performCrossFade();
mBottomRightPostcard.performCrossFade();
performedCrossFade = true;
}
else if (mSize == 2 && mMainPostcard.isReady() && mTopRightPostcard.isReady())
{
mMainPostcard.performCrossFade();
mTopRightPostcard.performCrossFade();
performedCrossFade = true;
}
else if (mSize == 1 && mMainPostcard.isReady())
{
mMainPostcard.performCrossFade();
performedCrossFade = true;
}
// Only queue up next image load, after previous one has finished (prevent race condition)
if (performedCrossFade)
{
mHandler.postDelayed(nextPostcardUpdate, FEED_DELAY);
}
}
private class CrossFadePostcard
{
private ImageView mNewImageView;
private ImageView mOldImageView;
private CrossFade mCrossFade = new CrossFade(800);
private Drawable mDrawable = null;
CrossFadePostcard(ImageView newImageView, ImageView oldImageView)
{
mNewImageView = newImageView;
mOldImageView = oldImageView;
}
boolean isReady()
{
return mDrawable != null;
}
void performCrossFade()
{
mCrossFade.crossFadeImage(mNewImageView, mOldImageView, mDrawable, true);
mDrawable = null;
}
}
}
private Runnable nextPostcardUpdate = new Runnable()
{
@Override
public void run()
{
if (isAdded())
{
mPostcards.updatePhotos(mRandomImages.getNextImages());
}
else
{
mHandler.postDelayed(nextPostcardUpdate, FEED_DELAY);
}
}
};
// return 3 random images, different than the currently displayed ones
public List<String> getNextImages() { ... }
Fragment's onResume()
mHandler.removeCallbacks(nextPostcardUpdate);
mHandler.post(nextPostcardUpdate);
Fragment's onPause()
if (mHandler != null)
{
mHandler.removeCallbacks(nextPostcardUpdate);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment