• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2015 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 package com.android.messaging.ui;
17 
18 import android.graphics.drawable.Drawable;
19 import android.graphics.drawable.InsetDrawable;
20 
21 /**
22  * A "placeholder" drawable that has the same sizing properties as the real UI element it
23  * replaces.
24  *
25  * This is an InsetDrawable that takes a placeholder drawable (an animation list, or simply
26  * a color drawable) and place it in the center of the inset drawable that's sized to the
27  * requested source width and height of the image that it replaces. Unlike the base
28  * InsetDrawable, this implementation returns the true width and height of the real image
29  * that it's placeholding, instead of the intrinsic size of the contained drawable, so that
30  * when used in an ImageView, it may be positioned/scaled/cropped the same way the real
31  * image is.
32  */
33 public class PlaceholderInsetDrawable extends InsetDrawable {
34     // The dimensions of the real image that this drawable is replacing.
35     private final int mSourceWidth;
36     private final int mSourceHeight;
37 
38     /**
39      * Given a source drawable, wraps it around in this placeholder drawable by placing the
40      * drawable at the center of the container if possible (or fill the container if the
41      * drawable doesn't have intrinsic size such as color drawable).
42      */
fromDrawable(final Drawable drawable, final int sourceWidth, final int sourceHeight)43     public static PlaceholderInsetDrawable fromDrawable(final Drawable drawable,
44             final int sourceWidth, final int sourceHeight) {
45         final int drawableWidth = drawable.getIntrinsicWidth();
46         final int drawableHeight = drawable.getIntrinsicHeight();
47         final int insetHorizontal = drawableWidth < 0 || drawableWidth > sourceWidth ?
48                 0 : (sourceWidth - drawableWidth) / 2;
49         final int insetVertical = drawableHeight < 0 || drawableHeight > sourceHeight ?
50                 0 : (sourceHeight - drawableHeight) / 2;
51         return new PlaceholderInsetDrawable(drawable, insetHorizontal, insetVertical,
52                 insetHorizontal, insetVertical, sourceWidth, sourceHeight);
53     }
54 
PlaceholderInsetDrawable(final Drawable drawable, final int insetLeft, final int insetTop, final int insetRight, final int insetBottom, final int sourceWidth, final int sourceHeight)55     private PlaceholderInsetDrawable(final Drawable drawable, final int insetLeft,
56             final int insetTop, final int insetRight, final int insetBottom,
57             final int sourceWidth, final int sourceHeight) {
58         super(drawable, insetLeft, insetTop, insetRight, insetBottom);
59         mSourceWidth = sourceWidth;
60         mSourceHeight = sourceHeight;
61     }
62 
63     @Override
getIntrinsicWidth()64     public int getIntrinsicWidth() {
65         return mSourceWidth;
66     }
67 
68     @Override
getIntrinsicHeight()69     public int getIntrinsicHeight() {
70         return mSourceHeight;
71     }
72 }