Animates the changing of a ImageView (or its subclasses like ImageButton)'s photo. - Android User Interface

Android examples for User Interface:ImageView

Description

Animates the changing of a ImageView (or its subclasses like ImageButton)'s photo.

Demo Code


import android.graphics.Color;
import android.graphics.drawable.ColorDrawable;
import android.graphics.drawable.Drawable;
import android.graphics.drawable.TransitionDrawable;
import android.view.animation.DecelerateInterpolator;
import android.view.animation.Interpolator;
import android.widget.ImageView;

public class Main{
    public static final int QUICK_ANIM_DURATION = 200;

    public static void animateTransitionOfImage(ImageView imageView,
            Drawable newDrawable, boolean crossFadeEnabled,
            int animDuration, final ImageView.ScaleType imageViewScaleType) {

        // TransitionDrawable doesn't accept null Drawables
        if (imageView.getDrawable() == null)
            imageView/*w w w  .  j a  v a  2  s .c o  m*/
                    .setImageDrawable(new ColorDrawable(Color.TRANSPARENT));
        if (newDrawable == null)
            newDrawable = new ColorDrawable(Color.TRANSPARENT);

        TransitionDrawable transitionDrawable = new TransitionDrawable(
                new Drawable[] { imageView.getDrawable(), newDrawable });

        imageView.setImageDrawable(transitionDrawable);
        imageView.setScaleType(imageViewScaleType);
        transitionDrawable.setCrossFadeEnabled(crossFadeEnabled);
        transitionDrawable.startTransition(animDuration);
    }

    public static long animateTransitionOfImage(ImageView imageView,
            Drawable newDrawable) {
        animateTransitionOfImage(imageView, newDrawable, true,
                AnimationUtils.QUICK_ANIM_DURATION,
                ImageView.ScaleType.CENTER_CROP);

        return AnimationUtils.QUICK_ANIM_DURATION;
    }
}

Related Tutorials