load Bitmap Into ImageView - Android android.widget

Android examples for android.widget:ImageView

Description

load Bitmap Into ImageView

Demo Code

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.drawable.BitmapDrawable;
import android.graphics.drawable.Drawable;
import android.graphics.drawable.TransitionDrawable;
import android.widget.ImageView;

public class Main {

  public static void loadBitmapIntoView(Context context, ImageView view, Bitmap bm, boolean fadeIn) {
    final Drawable previousDrawable = view.getDrawable();
    if (fadeIn && previousDrawable != null) {
      final Drawable[] layers = new Drawable[2];
      // Prevent cascade of TransitionDrawables.
      if (previousDrawable instanceof TransitionDrawable) {
        final TransitionDrawable previousTransitionDrawable = (TransitionDrawable) previousDrawable;
        layers[0] = previousTransitionDrawable.getDrawable(previousTransitionDrawable.getNumberOfLayers() - 1);
      } else {//from ww w .j av a2s .  c om
        layers[0] = previousDrawable;
      }
      layers[1] = new BitmapDrawable(context.getResources(), bm);
      TransitionDrawable drawable = new TransitionDrawable(layers);

      view.setImageDrawable(drawable);
      drawable.setCrossFadeEnabled(true);
      drawable.startTransition(200);
    } else {
      view.setImageBitmap(bm);
    }
  }

}

Related Tutorials