package com.github.takahirom.materialelement.view; 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.graphics.drawable.VectorDrawableCompat; import android.support.v4.content.ContextCompat; /** * From https://github.com/konifar/droidkaigi2016/blob/e5d52ed29619fa7cc53b5eeaf59ef36b1ca12635/app/src/main/java/io/github/droidkaigi/confsched/util/ResourceUtil.java */ public class ResourceUtil { @TargetApi(Build.VERSION_CODES.LOLLIPOP) private static Bitmap getBitmap(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; } private static Bitmap getBitmap(VectorDrawableCompat 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; } public static Bitmap getBitmap(Context context, @DrawableRes int drawableResId) { Drawable drawable = VectorDrawableCompat.create(context.getResources(), drawableResId, context.getTheme()); if (drawable instanceof BitmapDrawable) { return ((BitmapDrawable) drawable).getBitmap(); } else if (drawable instanceof VectorDrawableCompat) { return getBitmap((VectorDrawableCompat) drawable); } else if (drawable instanceof VectorDrawable) { return getBitmap((VectorDrawable) drawable); } else { throw new IllegalArgumentException("Unsupported drawable type"); } } }