Skip to content

Instantly share code, notes, and snippets.

@davinctor
Created August 25, 2016 11:54
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save davinctor/352b113a91c594f48bec5ea29e49331b to your computer and use it in GitHub Desktop.
Save davinctor/352b113a91c594f48bec5ea29e49331b to your computer and use it in GitHub Desktop.
Drawable util methods
package com.cleveroad.slidingtutorial.sample.util;
import android.annotation.TargetApi;
import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.Canvas;
import android.graphics.drawable.BitmapDrawable;
import android.graphics.drawable.Drawable;
import android.graphics.drawable.VectorDrawable;
import android.os.Build;
import android.support.annotation.DrawableRes;
import android.support.annotation.NonNull;
import android.support.v4.content.ContextCompat;
@SuppressWarnings("unused")
public class DrawableUtils {
@TargetApi(Build.VERSION_CODES.LOLLIPOP)
private static Bitmap getVectorDrawableBitmap(@NonNull VectorDrawable vectorDrawable) {
Bitmap bitmap = Bitmap.createBitmap(vectorDrawable.getIntrinsicWidth(),
vectorDrawable.getIntrinsicHeight(), Bitmap.Config.ARGB_8888);
Canvas canvas = new Canvas(bitmap);
vectorDrawable.setBounds(0, 0, canvas.getWidth(), canvas.getHeight());
vectorDrawable.draw(canvas);
return bitmap;
}
/**
* Get bitmap from drawable
* @param drawable drawable
* @return {@link Bitmap} from drawable
*/
public static Bitmap getBitmapByDrawable(@NonNull Drawable drawable) {
if (drawable instanceof BitmapDrawable) {
return ((BitmapDrawable) drawable).getBitmap();
} else if (drawable instanceof VectorDrawable) {
return getVectorDrawableBitmap((VectorDrawable) drawable);
} else {
throw new IllegalArgumentException("Unsupported drawable type");
}
}
/**
* Get bitmap from drawable
* @param context {@link Context} instance
* @param drawableRes drawable resource id
* @return {@link Bitmap} from drawable
*/
public static Bitmap getBitmapByDrawable(@NonNull Context context, @DrawableRes int drawableRes) {
Drawable drawable = ContextCompat.getDrawable(context, drawableRes);
if (drawable instanceof BitmapDrawable) {
return ((BitmapDrawable) drawable).getBitmap();
} else if (drawable instanceof VectorDrawable) {
return getVectorDrawableBitmap((VectorDrawable) drawable);
} else {
throw new IllegalArgumentException("Unsupported drawable type");
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment