Outline.java revision 0645128b80621edee70f8cab4afb208fe0c26bec
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
17package android.graphics;
18
19import android.annotation.NonNull;
20import android.graphics.drawable.Drawable;
21import android.view.View;
22
23/**
24 * Defines a simple shape, used for bounding graphical regions.
25 * <p>
26 * Can be used with a View, or computed by a Drawable, to drive the shape of shadows cast by a
27 * View, or to clip the contents of the View.
28 *
29 * @see View#setOutline(Outline)
30 * @see Drawable#getOutline(Outline)
31 */
32public final class Outline {
33    /** @hide */
34    public Path mPath;
35
36    /** @hide */
37    public Rect mRect;
38    /** @hide */
39    public float mRadius;
40
41    /** @hide */
42    public boolean mIsFilled;
43
44    /**
45     * Constructs an empty Outline. Call one of the setter methods to make
46     * the outline valid for use with a View.
47     */
48    public Outline() {}
49
50    /**
51     * Constructs an Outline with a copy of the data in src.
52     */
53    public Outline(@NonNull Outline src) {
54        set(src);
55    }
56
57    /**
58     * Sets the outline to be empty.
59     *
60     * @see #isEmpty()
61     */
62    public void setEmpty() {
63        mPath = null;
64        mRect = null;
65        mRadius = 0;
66        mIsFilled = true;
67    }
68
69    /**
70     * Returns whether the Outline is empty.
71     * <p>
72     * Outlines are empty when constructed, or if {@link #setEmpty()} is called,
73     * until a setter method is called
74     *
75     * @see #setEmpty()
76     */
77    public boolean isEmpty() {
78        return mRect == null && mPath == null;
79    }
80
81
82    /**
83     * Returns whether the outline can be used to clip a View.
84     *
85     * Currently, only outlines that can be represented as a rectangle, circle, or round rect
86     * support clipping.
87     *
88     * @see {@link View#setClipToOutline(boolean)}
89     */
90    public boolean canClip() {
91        return !isEmpty() && mRect != null;
92    }
93
94    /**
95     * Sets whether the outline represents a fully opaque area.
96     *
97     * A filled outline is assumed, by the drawing system, to fully cover content beneath it,
98     * meaning content beneath may be optimized away.
99     *
100     * @hide
101     */
102    public void setFilled(boolean isFilled) {
103        mIsFilled = isFilled;
104    }
105
106    /**
107     * Returns whether the outline represents a fully opaque area.
108     *
109     * @hide
110     */
111    public boolean isFilled() {
112        return !isEmpty() && mIsFilled;
113    }
114
115    /**
116     * Replace the contents of this Outline with the contents of src.
117     *
118     * @param src Source outline to copy from.
119     */
120    public void set(@NonNull Outline src) {
121        if (src.mPath != null) {
122            if (mPath == null) {
123                mPath = new Path();
124            }
125            mPath.set(src.mPath);
126            mRect = null;
127        }
128        if (src.mRect != null) {
129            if (mRect == null) {
130                mRect = new Rect();
131            }
132            mRect.set(src.mRect);
133        }
134        mRadius = src.mRadius;
135        mIsFilled = src.mIsFilled;
136    }
137
138    /**
139     * Sets the Outline to the rounded rect defined by the input rect, and corner radius.
140     */
141    public void setRect(int left, int top, int right, int bottom) {
142        setRoundRect(left, top, right, bottom, 0.0f);
143    }
144
145    /**
146     * Convenience for {@link #setRect(int, int, int, int)}
147     */
148    public void setRect(@NonNull Rect rect) {
149        setRect(rect.left, rect.top, rect.right, rect.bottom);
150    }
151
152    /**
153     * Sets the Outline to the rounded rect defined by the input rect, and corner radius.
154     *
155     * Passing a zero radius is equivalent to calling {@link #setRect(int, int, int, int)}
156     */
157    public void setRoundRect(int left, int top, int right, int bottom, float radius) {
158        if (left >= right || top >= bottom) {
159            setEmpty();
160            return;
161        }
162
163        if (mRect == null) mRect = new Rect();
164        mRect.set(left, top, right, bottom);
165        mRadius = radius;
166        mPath = null;
167        mIsFilled = true;
168    }
169
170    /**
171     * Convenience for {@link #setRoundRect(int, int, int, int, float)}
172     */
173    public void setRoundRect(@NonNull Rect rect, float radius) {
174        setRoundRect(rect.left, rect.top, rect.right, rect.bottom, radius);
175    }
176
177    /**
178     * Sets the outline to the oval defined by input rect.
179     */
180    public void setOval(int left, int top, int right, int bottom) {
181        if (left >= right || top >= bottom) {
182            setEmpty();
183            return;
184        }
185
186        if ((bottom - top) == (right - left)) {
187            // represent circle as round rect, for efficiency, and to enable clipping
188            setRoundRect(left, top, right, bottom, (bottom - top) / 2.0f);
189            return;
190        }
191
192        if (mPath == null) mPath = new Path();
193        mPath.reset();
194        mPath.addOval(left, top, right, bottom, Path.Direction.CW);
195        mRect = null;
196        mIsFilled = true;
197    }
198
199    /**
200     * Convenience for {@link #setOval(int, int, int, int)}
201     */
202    public void setOval(@NonNull Rect rect) {
203        setOval(rect.left, rect.top, rect.right, rect.bottom);
204    }
205
206    /**
207     * Sets the Constructs an Outline from a {@link android.graphics.Path#isConvex() convex path}.
208     */
209    public void setConvexPath(@NonNull Path convexPath) {
210        if (convexPath.isEmpty()) {
211            setEmpty();
212            return;
213        }
214
215        if (!convexPath.isConvex()) {
216            throw new IllegalArgumentException("path must be convex");
217        }
218        if (mPath == null) mPath = new Path();
219
220        mPath.set(convexPath);
221        mRect = null;
222        mRadius = -1.0f;
223        mIsFilled = true;
224    }
225}
226