animate View Bitmap - Android User Interface

Android examples for User Interface:View Animation

Description

animate View Bitmap

Demo Code


//package com.java2s;

import android.animation.ObjectAnimator;

import android.graphics.Bitmap;
import android.graphics.drawable.BitmapDrawable;

import android.graphics.drawable.Drawable;
import android.graphics.drawable.TransitionDrawable;
import android.support.annotation.NonNull;
import android.view.View;
import android.widget.ImageView;

public class Main {
    private static final int ANIM_DORITION = 400;

    public static void animateViewBitmap(@NonNull final ImageView root,
            Bitmap bitmap) {/*from www . j a v a 2  s  .c om*/
        if (bitmap == null && root.getDrawable() != null) {
            ObjectAnimator.ofFloat(root, View.ALPHA, 1f, 0f)
                    .setDuration(ANIM_DORITION).start();
            return;
        }

        Drawable[] layers = new Drawable[2];
        layers[0] = root.getDrawable();
        layers[1] = new BitmapDrawable(root.getResources(), bitmap);
        if (layers[0] == null) {//if null then alpha
            root.setImageDrawable(layers[1]);
            ObjectAnimator.ofFloat(root, View.ALPHA, 0.0f, 1.0f)
                    .setDuration(ANIM_DORITION).start();
            return;
        }
        TransitionDrawable transitionDrawable = new TransitionDrawable(
                layers);
        root.setImageDrawable(transitionDrawable);
        transitionDrawable.startTransition(ANIM_DORITION);
    }
}

Related Tutorials