• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 android.view;
18 
19 import com.android.resources.Density;
20 import com.android.tools.layoutlib.annotations.LayoutlibDelegate;
21 
22 import android.graphics.Bitmap;
23 import android.graphics.Bitmap_Delegate;
24 import android.graphics.Canvas;
25 import android.graphics.Outline;
26 import android.graphics.Path_Delegate;
27 import android.graphics.Rect;
28 import android.graphics.Region.Op;
29 import android.view.animation.Transformation;
30 
31 import java.awt.Graphics2D;
32 import java.awt.image.BufferedImage;
33 
34 /**
35  * Delegate used to provide new implementation of a select few methods of {@link ViewGroup}
36  * <p/>
37  * Through the layoutlib_create tool, the original  methods of ViewGroup have been replaced by calls
38  * to methods of the same name in this delegate class.
39  */
40 public class ViewGroup_Delegate {
41 
42     /**
43      * Overrides the original drawChild call in ViewGroup to draw the shadow.
44      */
45     @LayoutlibDelegate
drawChild(ViewGroup thisVG, Canvas canvas, View child, long drawingTime)46     /*package*/ static boolean drawChild(ViewGroup thisVG, Canvas canvas, View child,
47             long drawingTime) {
48         if (child.getZ() > thisVG.getZ()) {
49             // The background's bounds are set lazily. Make sure they are set correctly so that
50             // the outline obtained is correct.
51             child.setBackgroundBounds();
52             ViewOutlineProvider outlineProvider = child.getOutlineProvider();
53             if (outlineProvider != null) {
54                 Outline outline = child.mAttachInfo.mTmpOutline;
55                 outlineProvider.getOutline(child, outline);
56                 if (outline.mPath != null || (outline.mRect != null && !outline.mRect.isEmpty())) {
57                     int restoreTo = transformCanvas(thisVG, canvas, child);
58                     drawShadow(thisVG, canvas, child, outline);
59                     canvas.restoreToCount(restoreTo);
60                 }
61             }
62         }
63         return thisVG.drawChild_Original(canvas, child, drawingTime);
64     }
65 
drawShadow(ViewGroup parent, Canvas canvas, View child, Outline outline)66     private static void drawShadow(ViewGroup parent, Canvas canvas, View child,
67             Outline outline) {
68         float elevation = getElevation(child, parent);
69         if(outline.mMode == Outline.MODE_ROUND_RECT && outline.mRect != null) {
70             RectShadowPainter.paintShadow(outline, elevation, canvas);
71             return;
72         }
73         BufferedImage shadow = null;
74         if (outline.mPath != null) {
75             shadow = getPathShadow(outline, canvas, elevation);
76         }
77         if (shadow == null) {
78             return;
79         }
80         Bitmap bitmap = Bitmap_Delegate.createBitmap(shadow, false,
81                 Density.getEnum(canvas.getDensity()));
82         Rect clipBounds = canvas.getClipBounds();
83         Rect newBounds = new Rect(clipBounds);
84         newBounds.inset((int)-elevation, (int)-elevation);
85         canvas.clipRect(newBounds, Op.REPLACE);
86         canvas.drawBitmap(bitmap, 0, 0, null);
87         canvas.clipRect(clipBounds, Op.REPLACE);
88     }
89 
getElevation(View child, ViewGroup parent)90     private static float getElevation(View child, ViewGroup parent) {
91         return child.getZ() - parent.getZ();
92     }
93 
getPathShadow(Outline outline, Canvas canvas, float elevation)94     private static BufferedImage getPathShadow(Outline outline, Canvas canvas, float elevation) {
95         Rect clipBounds = canvas.getClipBounds();
96         if (clipBounds.isEmpty()) {
97           return null;
98         }
99         BufferedImage image = new BufferedImage(clipBounds.width(), clipBounds.height(),
100                 BufferedImage.TYPE_INT_ARGB);
101         Graphics2D graphics = image.createGraphics();
102         graphics.draw(Path_Delegate.getDelegate(outline.mPath.mNativePath).getJavaShape());
103         graphics.dispose();
104         return ShadowPainter.createDropShadow(image, (int) elevation);
105     }
106 
107     // Copied from android.view.View#draw(Canvas, ViewGroup, long) and removed code paths
108     // which were never taken. Ideally, we should hook up the shadow code in the same method so
109     // that we don't have to transform the canvas twice.
transformCanvas(ViewGroup thisVG, Canvas canvas, View child)110     private static int transformCanvas(ViewGroup thisVG, Canvas canvas, View child) {
111         final int restoreTo = canvas.save();
112         final boolean childHasIdentityMatrix = child.hasIdentityMatrix();
113         int flags = thisVG.mGroupFlags;
114         Transformation transformToApply = null;
115         boolean concatMatrix = false;
116         if ((flags & ViewGroup.FLAG_SUPPORT_STATIC_TRANSFORMATIONS) != 0) {
117             final Transformation t = thisVG.getChildTransformation();
118             final boolean hasTransform = thisVG.getChildStaticTransformation(child, t);
119             if (hasTransform) {
120                 final int transformType = t.getTransformationType();
121                 transformToApply = transformType != Transformation.TYPE_IDENTITY ? t : null;
122                 concatMatrix = (transformType & Transformation.TYPE_MATRIX) != 0;
123             }
124         }
125         concatMatrix |= childHasIdentityMatrix;
126 
127         child.computeScroll();
128         int sx = child.mScrollX;
129         int sy = child.mScrollY;
130 
131         canvas.translate(child.mLeft - sx, child.mTop - sy);
132         float alpha = child.getAlpha() * child.getTransitionAlpha();
133 
134         if (transformToApply != null || alpha < 1 || !childHasIdentityMatrix) {
135             if (transformToApply != null || !childHasIdentityMatrix) {
136                 int transX = -sx;
137                 int transY = -sy;
138 
139                 if (transformToApply != null) {
140                     if (concatMatrix) {
141                         // Undo the scroll translation, apply the transformation matrix,
142                         // then redo the scroll translate to get the correct result.
143                         canvas.translate(-transX, -transY);
144                         canvas.concat(transformToApply.getMatrix());
145                         canvas.translate(transX, transY);
146                     }
147                     if (!childHasIdentityMatrix) {
148                         canvas.translate(-transX, -transY);
149                         canvas.concat(child.getMatrix());
150                         canvas.translate(transX, transY);
151                     }
152                 }
153 
154             }
155         }
156         return restoreTo;
157     }
158 }
159