/* * Copyright (C) 2021 The Android Open Source Project * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ package com.android.car.settings.common; import android.graphics.Bitmap; import android.graphics.Canvas; import android.graphics.drawable.BitmapDrawable; import android.graphics.drawable.Drawable; import android.graphics.drawable.Icon; /** Contains utility functions to operate on Drawables. */ public class DrawableUtil { private DrawableUtil() { } /** * Create an {@link Icon} object from a {@link Drawable}. */ public static Icon createIconFromDrawable(Drawable drawable) { return Icon.createWithBitmap(createBitmapFromDrawable(drawable)); } /** * Create an {@link Bitmap} image from a {@link Drawable}. */ public static Bitmap createBitmapFromDrawable(Drawable drawable) { Bitmap bitmap; if (drawable instanceof BitmapDrawable) { bitmap = ((BitmapDrawable) drawable).getBitmap(); } else { int width = drawable.getIntrinsicWidth(); int height = drawable.getIntrinsicHeight(); bitmap = createBitmap(drawable, width > 0 ? width : 1, height > 0 ? height : 1); } return bitmap; } private static Bitmap createBitmap(Drawable drawable, int width, int height) { Bitmap bitmap = Bitmap.createBitmap(width, height, Bitmap.Config.ARGB_8888); Canvas canvas = new Canvas(bitmap); drawable.setBounds(0, 0, canvas.getWidth(), canvas.getHeight()); drawable.draw(canvas); return bitmap; } }