@TargetApi(Build.VERSION_CODES.O)
    @Nullable
    public static Bitmap getBitmap(Context context, Drawable drawable) {
        if (drawable instanceof BitmapDrawable) {
            return ((BitmapDrawable) drawable).getBitmap();
        } else if (drawable instanceof VectorDrawableCompat) {
            return getBitmap((VectorDrawableCompat) drawable);
        } else if (drawable instanceof VectorDrawable) {
            if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
                return getBitmap((VectorDrawable) drawable);
            }
            return null;
        } else if (drawable instanceof AdaptiveIconDrawable) {
            AdaptiveIconDrawable ad = (AdaptiveIconDrawable) drawable;
            return getBitmap(ad);

        } else {
            XLog.w("getBitmap Got drawable type:" + drawable);
            // https://cs.android.com/android/platform/superproject/+/master:packages/apps/Dialer/java/com/android/dialer/util/DrawableConverter.java?q=drawableToBitmap
            Bitmap bitmap;
            if (drawable.getIntrinsicWidth() <= 0 || drawable.getIntrinsicHeight() <= 0) {
                // Needed for drawables that are just a colour.
                bitmap = Bitmap.createBitmap(1, 1, Bitmap.Config.ARGB_8888);
            } else {
                bitmap = Bitmap.createBitmap(
                        drawable.getIntrinsicWidth(),
                        drawable.getIntrinsicHeight(),
                        Bitmap.Config.ARGB_8888);
            }

            XLog.i("created bitmap with width: %d, height: %d", bitmap.getWidth(), bitmap.getHeight());
            Canvas canvas = new Canvas(bitmap);
            drawable.setBounds(0, 0, canvas.getWidth(), canvas.getHeight());
            drawable.draw(canvas);
            return bitmap;
        }
    }
Created by guohao4 on 2017/10/24.
Email: Tornaco@163.com
android/modules/module_common/src/main/java/github/tornaco/android/thanos/util/BitmapUtil.java