Chris Craik | 9a347f1 | 2014-06-27 17:23:47 -0700 | [diff] [blame] | 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, |
Chris Craik | 77b5cad | 2014-07-30 18:23:07 -0700 | [diff] [blame] | 28 | * or generates a 0 alpha, rectangular Outline the size of the View if a background |
| 29 | * isn't present. |
Chris Craik | 9a347f1 | 2014-06-27 17:23:47 -0700 | [diff] [blame] | 30 | * |
| 31 | * @see Drawable#getOutline(Outline) |
| 32 | */ |
| 33 | public static final ViewOutlineProvider BACKGROUND = new ViewOutlineProvider() { |
| 34 | @Override |
Chris Craik | 31ba192 | 2014-07-18 13:48:09 -0700 | [diff] [blame] | 35 | public void getOutline(View view, Outline outline) { |
Chris Craik | 9a347f1 | 2014-06-27 17:23:47 -0700 | [diff] [blame] | 36 | Drawable background = view.getBackground(); |
Chris Craik | 31ba192 | 2014-07-18 13:48:09 -0700 | [diff] [blame] | 37 | if (background != null) { |
| 38 | background.getOutline(outline); |
Chris Craik | 77b5cad | 2014-07-30 18:23:07 -0700 | [diff] [blame] | 39 | } else { |
Chris Craik | 77b5cad | 2014-07-30 18:23:07 -0700 | [diff] [blame] | 40 | outline.setRect(0, 0, view.getWidth(), view.getHeight()); |
| 41 | outline.setAlpha(0.0f); |
Chris Craik | 9a347f1 | 2014-06-27 17:23:47 -0700 | [diff] [blame] | 42 | } |
Chris Craik | 9a347f1 | 2014-06-27 17:23:47 -0700 | [diff] [blame] | 43 | } |
| 44 | }; |
| 45 | |
| 46 | /** |
Chris Craik | f56885d | 2014-08-08 10:46:03 -0700 | [diff] [blame] | 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 | /** |
Chris Craik | 9a347f1 | 2014-06-27 17:23:47 -0700 | [diff] [blame] | 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 | * |
Chris Craik | 6efd175 | 2014-08-04 13:42:18 -0700 | [diff] [blame] | 82 | * The input outline is empty and has an alpha of <code>1.0f</code>. |
| 83 | * |
Chris Craik | 9a347f1 | 2014-06-27 17:23:47 -0700 | [diff] [blame] | 84 | * @param view The view building the outline. |
| 85 | * @param outline The empty outline to be populated. |
Chris Craik | 9a347f1 | 2014-06-27 17:23:47 -0700 | [diff] [blame] | 86 | */ |
Chris Craik | 31ba192 | 2014-07-18 13:48:09 -0700 | [diff] [blame] | 87 | public abstract void getOutline(View view, Outline outline); |
Chris Craik | 9a347f1 | 2014-06-27 17:23:47 -0700 | [diff] [blame] | 88 | } |