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 com.android.launcher3;
18 
19 import android.graphics.Canvas;
20 import android.graphics.ColorFilter;
21 import android.graphics.Rect;
22 import android.graphics.drawable.Drawable;
23 
24 public class BorderCropDrawable extends Drawable {
25 
26     private final Drawable mChild;
27     private final Rect mBoundsShift;
28     private final Rect mPadding;
29 
BorderCropDrawable(Drawable child, boolean cropLeft, boolean cropTop, boolean cropRight, boolean cropBottom)30     BorderCropDrawable(Drawable child, boolean cropLeft,
31             boolean cropTop, boolean cropRight, boolean cropBottom) {
32         mChild = child;
33 
34         mBoundsShift = new Rect();
35         mPadding = new Rect();
36         mChild.getPadding(mPadding);
37 
38         if (cropLeft) {
39             mBoundsShift.left = -mPadding.left;
40             mPadding.left = 0;
41         }
42         if (cropTop) {
43             mBoundsShift.top = -mPadding.top;
44             mPadding.top = 0;
45         }
46         if (cropRight) {
47             mBoundsShift.right = mPadding.right;
48             mPadding.right = 0;
49         }
50         if (cropBottom) {
51             mBoundsShift.bottom = mPadding.bottom;
52             mPadding.bottom = 0;
53         }
54     }
55 
56     @Override
onBoundsChange(Rect bounds)57     protected void onBoundsChange(Rect bounds) {
58         mChild.setBounds(
59                 bounds.left + mBoundsShift.left,
60                 bounds.top + mBoundsShift.top,
61                 bounds.right + mBoundsShift.right,
62                 bounds.bottom + mBoundsShift.bottom);
63     }
64 
65     @Override
getPadding(Rect padding)66     public boolean getPadding(Rect padding) {
67         padding.set(mPadding);
68         return (padding.left | padding.top | padding.right | padding.bottom) != 0;
69     }
70 
71     @Override
draw(Canvas canvas)72     public void draw(Canvas canvas) {
73         mChild.draw(canvas);
74     }
75 
76     @Override
getOpacity()77     public int getOpacity() {
78         return mChild.getOpacity();
79     }
80 
81     @Override
setAlpha(int alpha)82     public void setAlpha(int alpha) {
83         mChild.setAlpha(alpha);
84     }
85 
86     @Override
setColorFilter(ColorFilter cf)87     public void setColorFilter(ColorFilter cf) {
88         mChild.setColorFilter(cf);
89     }
90 }
91