RoundedBitmapDrawableFactory.java revision b31c3281d870e9abb673db239234d580dcc4feff
1/*
2 * Copyright (C) 2014 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 */
16
17package androidx.core.graphics.drawable;
18
19import android.content.res.Resources;
20import android.graphics.Bitmap;
21import android.graphics.BitmapFactory;
22import android.graphics.Rect;
23import android.os.Build;
24import androidx.annotation.NonNull;
25import androidx.annotation.Nullable;
26import androidx.core.graphics.BitmapCompat;
27import androidx.core.view.GravityCompat;
28import androidx.core.view.ViewCompat;
29import android.util.Log;
30
31import java.io.InputStream;
32
33/**
34 * Constructs {@link RoundedBitmapDrawable RoundedBitmapDrawable} objects,
35 * either from Bitmaps directly, or from streams and files.
36 */
37public final class RoundedBitmapDrawableFactory {
38    private static final String TAG = "RoundedBitmapDrawableFa";
39
40    private static class DefaultRoundedBitmapDrawable extends RoundedBitmapDrawable {
41        DefaultRoundedBitmapDrawable(Resources res, Bitmap bitmap) {
42            super(res, bitmap);
43        }
44
45        @Override
46        public void setMipMap(boolean mipMap) {
47            if (mBitmap != null) {
48                BitmapCompat.setHasMipMap(mBitmap, mipMap);
49                invalidateSelf();
50            }
51        }
52
53        @Override
54        public boolean hasMipMap() {
55            return mBitmap != null && BitmapCompat.hasMipMap(mBitmap);
56        }
57
58        @Override
59        void gravityCompatApply(int gravity, int bitmapWidth, int bitmapHeight,
60                Rect bounds, Rect outRect) {
61            GravityCompat.apply(gravity, bitmapWidth, bitmapHeight,
62                    bounds, outRect, ViewCompat.LAYOUT_DIRECTION_LTR);
63        }
64    }
65
66    /**
67     * Returns a new drawable by creating it from a bitmap, setting initial target density based on
68     * the display metrics of the resources.
69     */
70    @NonNull
71    public static RoundedBitmapDrawable create(@NonNull Resources res, @Nullable Bitmap bitmap) {
72        if (Build.VERSION.SDK_INT >= 21) {
73            return new RoundedBitmapDrawable21(res, bitmap);
74        }
75        return new DefaultRoundedBitmapDrawable(res, bitmap);
76    }
77
78    /**
79     * Returns a new drawable, creating it by opening a given file path and decoding the bitmap.
80     */
81    @NonNull
82    public static RoundedBitmapDrawable create(@NonNull Resources res, @NonNull String filepath) {
83        final RoundedBitmapDrawable drawable = create(res, BitmapFactory.decodeFile(filepath));
84        if (drawable.getBitmap() == null) {
85            Log.w(TAG, "RoundedBitmapDrawable cannot decode " + filepath);
86        }
87        return drawable;
88    }
89
90
91    /**
92     * Returns a new drawable, creating it by decoding a bitmap from the given input stream.
93     */
94    @NonNull
95    public static RoundedBitmapDrawable create(@NonNull Resources res, @NonNull InputStream is) {
96        final RoundedBitmapDrawable drawable = create(res, BitmapFactory.decodeStream(is));
97        if (drawable.getBitmap() == null) {
98            Log.w(TAG, "RoundedBitmapDrawable cannot decode " + is);
99        }
100        return drawable;
101    }
102
103    private RoundedBitmapDrawableFactory() {}
104
105}
106