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