177fafa55c81879c5715b74d212eed76d971db184Anthony Chen/*
277fafa55c81879c5715b74d212eed76d971db184Anthony Chen * Copyright (c) 2016, The Android Open Source Project
377fafa55c81879c5715b74d212eed76d971db184Anthony Chen *
477fafa55c81879c5715b74d212eed76d971db184Anthony Chen * Licensed under the Apache License, Version 2.0 (the "License");
577fafa55c81879c5715b74d212eed76d971db184Anthony Chen * you may not use this file except in compliance with the License.
677fafa55c81879c5715b74d212eed76d971db184Anthony Chen * You may obtain a copy of the License at
777fafa55c81879c5715b74d212eed76d971db184Anthony Chen *
877fafa55c81879c5715b74d212eed76d971db184Anthony Chen *     http://www.apache.org/licenses/LICENSE-2.0
977fafa55c81879c5715b74d212eed76d971db184Anthony Chen *
1077fafa55c81879c5715b74d212eed76d971db184Anthony Chen * Unless required by applicable law or agreed to in writing, software
1177fafa55c81879c5715b74d212eed76d971db184Anthony Chen * distributed under the License is distributed on an "AS IS" BASIS,
1277fafa55c81879c5715b74d212eed76d971db184Anthony Chen * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
1377fafa55c81879c5715b74d212eed76d971db184Anthony Chen * See the License for the specific language governing permissions and
1477fafa55c81879c5715b74d212eed76d971db184Anthony Chen * limitations under the License.
1577fafa55c81879c5715b74d212eed76d971db184Anthony Chen */
1677fafa55c81879c5715b74d212eed76d971db184Anthony Chenpackage com.android.car.stream;
1777fafa55c81879c5715b74d212eed76d971db184Anthony Chen
1877fafa55c81879c5715b74d212eed76d971db184Anthony Chenimport android.content.res.Resources;
1977fafa55c81879c5715b74d212eed76d971db184Anthony Chenimport android.graphics.Bitmap;
2077fafa55c81879c5715b74d212eed76d971db184Anthony Chenimport android.graphics.Canvas;
2177fafa55c81879c5715b74d212eed76d971db184Anthony Chenimport android.graphics.drawable.VectorDrawable;
2277fafa55c81879c5715b74d212eed76d971db184Anthony Chenimport android.support.annotation.Nullable;
2377fafa55c81879c5715b74d212eed76d971db184Anthony Chen
2477fafa55c81879c5715b74d212eed76d971db184Anthony Chen/**
2577fafa55c81879c5715b74d212eed76d971db184Anthony Chen * Utility class for manipulating Bitmaps.
2677fafa55c81879c5715b74d212eed76d971db184Anthony Chen */
2777fafa55c81879c5715b74d212eed76d971db184Anthony Chenpublic class BitmapUtils {
2877fafa55c81879c5715b74d212eed76d971db184Anthony Chen    private BitmapUtils() {}
2977fafa55c81879c5715b74d212eed76d971db184Anthony Chen
3077fafa55c81879c5715b74d212eed76d971db184Anthony Chen    /**
3177fafa55c81879c5715b74d212eed76d971db184Anthony Chen     * Returns a {@link Bitmap} from a {@link VectorDrawable}.
3277fafa55c81879c5715b74d212eed76d971db184Anthony Chen     * {@link android.graphics.BitmapFactory#decodeResource(Resources, int)} cannot be used to
3377fafa55c81879c5715b74d212eed76d971db184Anthony Chen     * retrieve a bitmap from a VectorDrawable, so this method works around that.
3477fafa55c81879c5715b74d212eed76d971db184Anthony Chen     */
3577fafa55c81879c5715b74d212eed76d971db184Anthony Chen    @Nullable
3677fafa55c81879c5715b74d212eed76d971db184Anthony Chen    public static Bitmap getBitmap(VectorDrawable vectorDrawable) {
3777fafa55c81879c5715b74d212eed76d971db184Anthony Chen        if (vectorDrawable == null) {
3877fafa55c81879c5715b74d212eed76d971db184Anthony Chen            return null;
3977fafa55c81879c5715b74d212eed76d971db184Anthony Chen        }
4077fafa55c81879c5715b74d212eed76d971db184Anthony Chen
4177fafa55c81879c5715b74d212eed76d971db184Anthony Chen        Bitmap bitmap = Bitmap.createBitmap(vectorDrawable.getIntrinsicWidth(),
4277fafa55c81879c5715b74d212eed76d971db184Anthony Chen                vectorDrawable.getIntrinsicHeight(), Bitmap.Config.ARGB_8888);
4377fafa55c81879c5715b74d212eed76d971db184Anthony Chen        Canvas canvas = new Canvas(bitmap);
4477fafa55c81879c5715b74d212eed76d971db184Anthony Chen        vectorDrawable.setBounds(0, 0, canvas.getWidth(), canvas.getHeight());
4577fafa55c81879c5715b74d212eed76d971db184Anthony Chen        vectorDrawable.draw(canvas);
4677fafa55c81879c5715b74d212eed76d971db184Anthony Chen        return bitmap;
4777fafa55c81879c5715b74d212eed76d971db184Anthony Chen    }
4877fafa55c81879c5715b74d212eed76d971db184Anthony Chen}
49