Outline.java revision 31ba192dd201df2cad96a8c503f730130ab0d80f
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 (mRect == null) mRect = new Rect(); 159 mRect.set(left, top, right, bottom); 160 mRadius = radius; 161 mPath = null; 162 mIsFilled = true; 163 } 164 165 /** 166 * Convenience for {@link #setRoundRect(int, int, int, int, float)} 167 */ 168 public void setRoundRect(@NonNull Rect rect, float radius) { 169 setRoundRect(rect.left, rect.top, rect.right, rect.bottom, radius); 170 } 171 172 /** 173 * Sets the outline to the oval defined by input rect. 174 */ 175 public void setOval(int left, int top, int right, int bottom) { 176 if ((bottom - top) == (right - left)) { 177 // represent circle as round rect, for efficiency, and to enable clipping 178 setRoundRect(left, top, right, bottom, (bottom - top) / 2.0f); 179 return; 180 } 181 if (mPath == null) mPath = new Path(); 182 mPath.reset(); 183 mPath.addOval(left, top, right, bottom, Path.Direction.CW); 184 mRect = null; 185 mIsFilled = true; 186 } 187 188 /** 189 * Convenience for {@link #setOval(int, int, int, int)} 190 */ 191 public void setOval(@NonNull Rect rect) { 192 setOval(rect.left, rect.top, rect.right, rect.bottom); 193 } 194 195 /** 196 * Sets the Constructs an Outline from a {@link android.graphics.Path#isConvex() convex path}. 197 */ 198 public void setConvexPath(@NonNull Path convexPath) { 199 if (!convexPath.isConvex()) { 200 throw new IllegalArgumentException("path must be convex"); 201 } 202 if (mPath == null) mPath = new Path(); 203 204 mPath.set(convexPath); 205 mRect = null; 206 mRadius = -1.0f; 207 mIsFilled = true; 208 } 209} 210