Starts cross-fade animation using TransitionDrawable. - Android Animation

Android examples for Animation:Fade Animation

Description

Starts cross-fade animation using TransitionDrawable.

Demo Code

/*/*w w  w  . j  a va  2 s. co m*/
 * Copyright (C) 2012 The Android Open Source Project
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
import android.animation.Animator;
import android.animation.AnimatorListenerAdapter;
import android.animation.ObjectAnimator;
import android.graphics.Canvas;
import android.graphics.drawable.BitmapDrawable;
import android.graphics.drawable.Drawable;
import android.graphics.drawable.LayerDrawable;
import android.util.Log;
import android.view.View;
import android.view.ViewPropertyAnimator;
import android.widget.ImageView;

public class Main{
    private static final String LOG_TAG = AnimationUtils.class
            .getSimpleName();
    /**
     * Turn on when you're interested in fading animation. Intentionally untied from other debug
     * settings.
     */
    private static final boolean FADE_DBG = false;
    /**
     * Duration for animations in msec, which can be used with
     * {@link ViewPropertyAnimator#setDuration(long)} for example.
     */
    public static final int ANIMATION_DURATION = 250;
    /**
     * Starts cross-fade animation using TransitionDrawable. Nothing will happen if "from" and "to"
     * are the same.
     */
    public static void startCrossFade(final ImageView imageView,
            final Drawable from, final Drawable to) {
        // We skip the cross-fade when those two Drawables are equal, or they are BitmapDrawables
        // pointing to the same Bitmap.
        final boolean areSameImage = from.equals(to)
                || ((from instanceof BitmapDrawable)
                        && (to instanceof BitmapDrawable) && ((BitmapDrawable) from)
                        .getBitmap().equals(
                                ((BitmapDrawable) to).getBitmap()));
        if (!areSameImage) {
            if (FADE_DBG) {
                log("Start cross-fade animation for " + imageView + "("
                        + Integer.toHexString(from.hashCode()) + " -> "
                        + Integer.toHexString(to.hashCode()) + ")");
            }

            CrossFadeDrawable crossFadeDrawable = newCrossFadeDrawable(
                    from, to);
            ObjectAnimator animator = crossFadeDrawable.getAnimator();
            imageView.setImageDrawable(crossFadeDrawable);
            animator.setDuration(ANIMATION_DURATION);
            animator.addListener(new AnimatorListenerAdapter() {
                @Override
                public void onAnimationStart(Animator animation) {
                    if (FADE_DBG) {
                        log("cross-fade animation start ("
                                + Integer.toHexString(from.hashCode())
                                + " -> "
                                + Integer.toHexString(to.hashCode()) + ")");
                    }
                }

                @Override
                public void onAnimationEnd(Animator animation) {
                    if (FADE_DBG) {
                        log("cross-fade animation ended ("
                                + Integer.toHexString(from.hashCode())
                                + " -> "
                                + Integer.toHexString(to.hashCode()) + ")");
                    }
                    animation.removeAllListeners();
                    // Workaround for issue 6300562; this will force the drawable to the
                    // resultant one regardless of animation glitch.
                    imageView.setImageDrawable(to);
                }
            });
            animator.start();

            /* We could use TransitionDrawable here, but it may cause some weird animation in
             * some corner cases. See issue 6300562
             * TODO: decide which to be used in the long run. TransitionDrawable is old but system
             * one. Ours uses new animation framework and thus have callback (great for testing),
             * while no framework support for the exact class.

            Drawable[] layers = new Drawable[2];
            layers[0] = from;
            layers[1] = to;
            TransitionDrawable transitionDrawable = new TransitionDrawable(layers);
            imageView.setImageDrawable(transitionDrawable);
            transitionDrawable.startTransition(ANIMATION_DURATION); */
            imageView.setTag(to);
        } else {
            if (FADE_DBG) {
                log("*Not* start cross-fade. " + imageView);
            }
        }
    }
    private static void log(String msg) {
        Log.d(LOG_TAG, msg);
    }
    private static CrossFadeDrawable newCrossFadeDrawable(Drawable first,
            Drawable second) {
        Drawable[] layers = new Drawable[2];
        layers[0] = first;
        layers[1] = second;
        return new CrossFadeDrawable(layers);
    }
}

Related Tutorials