Last active
August 29, 2015 14:06
-
-
Save subinkrishna/85c44ba3598887c0ab5d to your computer and use it in GitHub Desktop.
Set a bitmap as background to a View
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 ViewUtil { | |
/** | |
* 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)); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment