Sets a Bitmap as background to a view. - Android User Interface

Android examples for User Interface:View Bitmap

Description

Sets a Bitmap as background to a view.

Demo Code


import android.annotation.TargetApi;
import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.drawable.BitmapDrawable;
import android.os.Build;
import android.view.View;

public class Main {
  /** Log tag *///from   ww  w  .j a  v a2s  . c o m
  public static final String TAG = "";

  /**
   * Sets a Bitmap as background to a view.
   *
   * @param context
   * @param view
   * @param bitmap
   */
  public static void setBitmapBackground(final Context context,
      final View view, final Bitmap bitmap) {
    if ((null != context) && (null != view) && (null != bitmap)) {
      if (Build.VERSION.SDK_INT >= 16) {
        setBitmapBackgroundV16Plus(context, view, bitmap);
      } else {
        setBitmapBackgroundV16Minus(context, view, bitmap);
      }
    }
  }

  @TargetApi(Build.VERSION_CODES.JELLY_BEAN)
  private static void setBitmapBackgroundV16Plus(final Context context,
      final View view, final Bitmap bitmap) {
    view.setBackground(new BitmapDrawable(context.getResources(), bitmap));
  }

  @SuppressWarnings("deprecation")
  private static void setBitmapBackgroundV16Minus(final Context context,
      final View view, final Bitmap bitmap) {
    view.setBackgroundDrawable(new BitmapDrawable(context.getResources(),
        bitmap));
  }
}

Related Tutorials