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 android.graphics.Outline;
20 import android.graphics.drawable.Drawable;
21 
22 /**
23  * Interface by which a View builds its {@link Outline}, used for shadow casting and clipping.
24  */
25 public abstract class ViewOutlineProvider {
26     /**
27      * Default outline provider for Views, which queries the Outline from the View's background,
28      * or generates a 0 alpha, rectangular Outline the size of the View if a background
29      * isn't present.
30      *
31      * @see Drawable#getOutline(Outline)
32      */
33     public static final ViewOutlineProvider BACKGROUND = new ViewOutlineProvider() {
34         @Override
35         public void getOutline(View view, Outline outline) {
36             Drawable background = view.getBackground();
37             if (background != null) {
38                 background.getOutline(outline);
39             } else {
40                 outline.setRect(0, 0, view.getWidth(), view.getHeight());
41                 outline.setAlpha(0.0f);
42             }
43         }
44     };
45 
46     /**
47      * Maintains the outline of the View to match its rectangular bounds,
48      * at <code>1.0f</code> alpha.
49      *
50      * This can be used to enable Views that are opaque but lacking a background cast a shadow.
51      */
52     public static final ViewOutlineProvider BOUNDS = new ViewOutlineProvider() {
53         @Override
54         public void getOutline(View view, Outline outline) {
55             outline.setRect(0, 0, view.getWidth(), view.getHeight());
56         }
57     };
58 
59     /**
60      * Maintains the outline of the View to match its rectangular padded bounds,
61      * at <code>1.0f</code> alpha.
62      *
63      * This can be used to enable Views that are opaque but lacking a background cast a shadow.
64      */
65     public static final ViewOutlineProvider PADDED_BOUNDS = new ViewOutlineProvider() {
66         @Override
67         public void getOutline(View view, Outline outline) {
68             outline.setRect(view.getPaddingLeft(),
69                     view.getPaddingTop(),
70                     view.getWidth() - view.getPaddingRight(),
71                     view.getHeight() - view.getPaddingBottom());
72         }
73     };
74 
75     /**
76      * Called to get the provider to populate the Outline.
77      *
78      * This method will be called by a View when its owned Drawables are invalidated, when the
79      * View's size changes, or if {@link View#invalidateOutline()} is called
80      * explicitly.
81      *
82      * The input outline is empty and has an alpha of <code>1.0f</code>.
83      *
84      * @param view The view building the outline.
85      * @param outline The empty outline to be populated.
86      */
getOutline(View view, Outline outline)87     public abstract void getOutline(View view, Outline outline);
88 }
89