/** * Sets a {@link Bitmap} to an {@link ImageView} using a fade-in animation. If there is a {@link * Drawable} already set on the ImageView then use that as the image to fade from. Otherwise fade * in from a transparent Drawable. */ @TargetApi(Build.VERSION_CODES.HONEYCOMB_MR1) private static void setImageBitmap( final ImageView imageView, final Bitmap bitmap, Resources resources, boolean fadeIn) { // If we're fading in and on HC MR1+ if (fadeIn && Utils.hasHoneycombMR1()) { // Use ViewPropertyAnimator to run a simple fade in + fade out animation to update the // ImageView imageView .animate() .scaleY(0.95f) .scaleX(0.95f) .alpha(0f) .setDuration(imageView.getDrawable() == null ? 0 : HALF_FADE_IN_TIME) .setListener( new AnimatorListenerAdapter() { @Override public void onAnimationEnd(Animator animation) { imageView.setImageBitmap(bitmap); imageView .animate() .alpha(1f) .scaleY(1f) .scaleX(1f) .setDuration(HALF_FADE_IN_TIME) .setListener(null); } }); } else if (fadeIn) { // Otherwise use a TransitionDrawable to fade in Drawable initialDrawable; if (imageView.getDrawable() != null) { initialDrawable = imageView.getDrawable(); } else { initialDrawable = transparentDrawable; } BitmapDrawable bitmapDrawable = new BitmapDrawable(resources, bitmap); // Use TransitionDrawable to fade in final TransitionDrawable td = new TransitionDrawable(new Drawable[] {initialDrawable, bitmapDrawable}); imageView.setImageDrawable(td); td.startTransition(Utils.ANIMATION_FADE_IN_TIME); } else { // No fade in, just set bitmap directly imageView.setImageBitmap(bitmap); } }