1/*
2 * Copyright (c) 2016, The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 *     http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16package com.android.car.stream;
17
18import android.content.res.Resources;
19import android.graphics.Bitmap;
20import android.graphics.Canvas;
21import android.graphics.drawable.VectorDrawable;
22import android.support.annotation.Nullable;
23
24/**
25 * Utility class for manipulating Bitmaps.
26 */
27public class BitmapUtils {
28    private BitmapUtils() {}
29
30    /**
31     * Returns a {@link Bitmap} from a {@link VectorDrawable}.
32     * {@link android.graphics.BitmapFactory#decodeResource(Resources, int)} cannot be used to
33     * retrieve a bitmap from a VectorDrawable, so this method works around that.
34     */
35    @Nullable
36    public static Bitmap getBitmap(VectorDrawable vectorDrawable) {
37        if (vectorDrawable == null) {
38            return null;
39        }
40
41        Bitmap bitmap = Bitmap.createBitmap(vectorDrawable.getIntrinsicWidth(),
42                vectorDrawable.getIntrinsicHeight(), Bitmap.Config.ARGB_8888);
43        Canvas canvas = new Canvas(bitmap);
44        vectorDrawable.setBounds(0, 0, canvas.getWidth(), canvas.getHeight());
45        vectorDrawable.draw(canvas);
46        return bitmap;
47    }
48}
49