blob: a1a02f67ff5bc297296b5a78e2880d61d9e93b4a [file] [log] [blame]
Chris Craik9a347f12014-06-27 17:23:47 -07001/*
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
17package android.view;
18
19import android.graphics.Outline;
20import android.graphics.drawable.Drawable;
21
22/**
23 * Interface by which a View builds its {@link Outline}, used for shadow casting and clipping.
24 */
25public abstract class ViewOutlineProvider {
26 /**
27 * Default outline provider for Views, which queries the Outline from the View's background,
Chris Craik77b5cad2014-07-30 18:23:07 -070028 * or generates a 0 alpha, rectangular Outline the size of the View if a background
29 * isn't present.
Chris Craik9a347f12014-06-27 17:23:47 -070030 *
31 * @see Drawable#getOutline(Outline)
32 */
33 public static final ViewOutlineProvider BACKGROUND = new ViewOutlineProvider() {
34 @Override
Chris Craik31ba1922014-07-18 13:48:09 -070035 public void getOutline(View view, Outline outline) {
Chris Craik9a347f12014-06-27 17:23:47 -070036 Drawable background = view.getBackground();
Chris Craik31ba1922014-07-18 13:48:09 -070037 if (background != null) {
38 background.getOutline(outline);
Chris Craik77b5cad2014-07-30 18:23:07 -070039 } else {
Chris Craik77b5cad2014-07-30 18:23:07 -070040 outline.setRect(0, 0, view.getWidth(), view.getHeight());
41 outline.setAlpha(0.0f);
Chris Craik9a347f12014-06-27 17:23:47 -070042 }
Chris Craik9a347f12014-06-27 17:23:47 -070043 }
44 };
45
46 /**
Chris Craikf56885d2014-08-08 10:46:03 -070047 * 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 Craik9a347f12014-06-27 17:23:47 -070076 * 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 Craik6efd1752014-08-04 13:42:18 -070082 * The input outline is empty and has an alpha of <code>1.0f</code>.
83 *
Chris Craik9a347f12014-06-27 17:23:47 -070084 * @param view The view building the outline.
85 * @param outline The empty outline to be populated.
Chris Craik9a347f12014-06-27 17:23:47 -070086 */
Chris Craik31ba1922014-07-18 13:48:09 -070087 public abstract void getOutline(View view, Outline outline);
Chris Craik9a347f12014-06-27 17:23:47 -070088}