| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
package android.view; |
| 17 |
|
| 18 |
import android.animation.LayoutTransition; |
| 19 |
import android.annotation.NonNull; |
| 20 |
import android.compat.annotation.UnsupportedAppUsage; |
| 21 |
import android.content.Context; |
| 22 |
import android.graphics.Canvas; |
| 23 |
import android.graphics.Rect; |
| 24 |
import android.graphics.drawable.Drawable; |
| 25 |
import android.os.Build; |
| 26 |
|
| 27 |
import java.util.ArrayList; |
| 28 |
|
| 29 |
|
| 30 |
|
| 31 |
|
| 32 |
|
| 33 |
|
| 34 |
|
| 35 |
|
| 36 |
|
| 37 |
|
| 38 |
|
| 39 |
|
| 40 |
|
| 41 |
|
| 42 |
public class ViewOverlay { |
| 43 |
|
| 44 |
|
| 45 |
|
| 46 |
|
| 47 |
|
| 48 |
|
| 49 |
OverlayViewGroup mOverlayViewGroup; |
| 50 |
|
| 51 |
ViewOverlay(Context context, View hostView) { |
| 52 |
mOverlayViewGroup = new OverlayViewGroup(context, hostView); |
| 53 |
} |
| 54 |
|
| 55 |
|
| 56 |
|
| 57 |
|
| 58 |
|
| 59 |
|
| 60 |
@UnsupportedAppUsage(maxTargetSdk = Build.VERSION_CODES.R, trackingBug = 170729553) |
| 61 |
ViewGroup getOverlayView() { |
| 62 |
return mOverlayViewGroup; |
| 63 |
} |
| 64 |
|
| 65 |
|
| 66 |
|
| 67 |
|
| 68 |
|
| 69 |
|
| 70 |
|
| 71 |
|
| 72 |
|
| 73 |
|
| 74 |
|
| 75 |
|
| 76 |
|
| 77 |
public void add(@NonNull Drawable drawable) { |
| 78 |
mOverlayViewGroup.add(drawable); |
| 79 |
} |
| 80 |
|
| 81 |
|
| 82 |
|
| 83 |
|
| 84 |
|
| 85 |
|
| 86 |
|
| 87 |
|
| 88 |
|
| 89 |
public void remove(@NonNull Drawable drawable) { |
| 90 |
mOverlayViewGroup.remove(drawable); |
| 91 |
} |
| 92 |
|
| 93 |
|
| 94 |
|
| 95 |
|
| 96 |
public void clear() { |
| 97 |
mOverlayViewGroup.clear(); |
| 98 |
} |
| 99 |
|
| 100 |
@UnsupportedAppUsage(maxTargetSdk = Build.VERSION_CODES.R, trackingBug = 170729553) |
| 101 |
boolean isEmpty() { |
| 102 |
return mOverlayViewGroup.isEmpty(); |
| 103 |
} |
| 104 |
|
| 105 |
|
| 106 |
|
| 107 |
|
| 108 |
|
| 109 |
|
| 110 |
|
| 111 |
|
| 112 |
|
| 113 |
|
| 114 |
|
| 115 |
|
| 116 |
|
| 117 |
|
| 118 |
|
| 119 |
|
| 120 |
|
| 121 |
|
| 122 |
|
| 123 |
|
| 124 |
|
| 125 |
|
| 126 |
static class OverlayViewGroup extends ViewGroup { |
| 127 |
|
| 128 |
|
| 129 |
|
| 130 |
|
| 131 |
|
| 132 |
final View mHostView; |
| 133 |
|
| 134 |
|
| 135 |
|
| 136 |
|
| 137 |
ArrayList<Drawable> mDrawables = null; |
| 138 |
|
| 139 |
OverlayViewGroup(Context context, View hostView) { |
| 140 |
super(context); |
| 141 |
mHostView = hostView; |
| 142 |
mAttachInfo = mHostView.mAttachInfo; |
| 143 |
|
| 144 |
mRight = hostView.getWidth(); |
| 145 |
mBottom = hostView.getHeight(); |
| 146 |
|
| 147 |
mRenderNode.setLeftTopRightBottom(0, 0, mRight, mBottom); |
| 148 |
} |
| 149 |
|
| 150 |
public void add(@NonNull Drawable drawable) { |
| 151 |
if (drawable == null) { |
| 152 |
throw new IllegalArgumentException("drawable must be non-null"); |
| 153 |
} |
| 154 |
if (mDrawables == null) { |
| 155 |
mDrawables = new ArrayList<>(); |
| 156 |
} |
| 157 |
if (!mDrawables.contains(drawable)) { |
| 158 |
|
| 159 |
mDrawables.add(drawable); |
| 160 |
invalidate(drawable.getBounds()); |
| 161 |
drawable.setCallback(this); |
| 162 |
} |
| 163 |
} |
| 164 |
|
| 165 |
public void remove(@NonNull Drawable drawable) { |
| 166 |
if (drawable == null) { |
| 167 |
throw new IllegalArgumentException("drawable must be non-null"); |
| 168 |
} |
| 169 |
if (mDrawables != null) { |
| 170 |
mDrawables.remove(drawable); |
| 171 |
invalidate(drawable.getBounds()); |
| 172 |
drawable.setCallback(null); |
| 173 |
} |
| 174 |
} |
| 175 |
|
| 176 |
@Override |
| 177 |
protected boolean verifyDrawable(@NonNull Drawable who) { |
| 178 |
return super.verifyDrawable(who) || (mDrawables != null && mDrawables.contains(who)); |
| 179 |
} |
| 180 |
|
| 181 |
public void add(@NonNull View child) { |
| 182 |
if (child == null) { |
| 183 |
throw new IllegalArgumentException("view must be non-null"); |
| 184 |
} |
| 185 |
|
| 186 |
if (child.getParent() instanceof ViewGroup) { |
| 187 |
ViewGroup parent = (ViewGroup) child.getParent(); |
| 188 |
if (parent != mHostView && parent.getParent() != null && |
| 189 |
parent.mAttachInfo != null) { |
| 190 |
|
| 191 |
|
| 192 |
int[] parentLocation = new int[2]; |
| 193 |
int[] hostViewLocation = new int[2]; |
| 194 |
parent.getLocationOnScreen(parentLocation); |
| 195 |
mHostView.getLocationOnScreen(hostViewLocation); |
| 196 |
child.offsetLeftAndRight(parentLocation[0] - hostViewLocation[0]); |
| 197 |
child.offsetTopAndBottom(parentLocation[1] - hostViewLocation[1]); |
| 198 |
} |
| 199 |
parent.removeView(child); |
| 200 |
if (parent.getLayoutTransition() != null) { |
| 201 |
|
| 202 |
parent.getLayoutTransition().cancel(LayoutTransition.DISAPPEARING); |
| 203 |
} |
| 204 |
|
| 205 |
if (child.getParent() != null) { |
| 206 |
child.mParent = null; |
| 207 |
} |
| 208 |
} |
| 209 |
super.addView(child); |
| 210 |
} |
| 211 |
|
| 212 |
public void remove(@NonNull View view) { |
| 213 |
if (view == null) { |
| 214 |
throw new IllegalArgumentException("view must be non-null"); |
| 215 |
} |
| 216 |
|
| 217 |
super.removeView(view); |
| 218 |
} |
| 219 |
|
| 220 |
public void clear() { |
| 221 |
removeAllViews(); |
| 222 |
if (mDrawables != null) { |
| 223 |
for (Drawable drawable : mDrawables) { |
| 224 |
drawable.setCallback(null); |
| 225 |
} |
| 226 |
mDrawables.clear(); |
| 227 |
} |
| 228 |
} |
| 229 |
|
| 230 |
boolean isEmpty() { |
| 231 |
if (getChildCount() == 0 && |
| 232 |
(mDrawables == null || mDrawables.size() == 0)) { |
| 233 |
return true; |
| 234 |
} |
| 235 |
return false; |
| 236 |
} |
| 237 |
|
| 238 |
@Override |
| 239 |
public void invalidateDrawable(@NonNull Drawable drawable) { |
| 240 |
invalidate(drawable.getBounds()); |
| 241 |
} |
| 242 |
|
| 243 |
@Override |
| 244 |
protected void dispatchDraw(Canvas canvas) { |
| 245 |
|
| 246 |
|
| 247 |
|
| 248 |
|
| 249 |
|
| 250 |
|
| 251 |
|
| 252 |
|
| 253 |
canvas.enableZ(); |
| 254 |
|
| 255 |
super.dispatchDraw(canvas); |
| 256 |
|
| 257 |
canvas.disableZ(); |
| 258 |
final int numDrawables = (mDrawables == null) ? 0 : mDrawables.size(); |
| 259 |
for (int i = 0; i < numDrawables; ++i) { |
| 260 |
mDrawables.get(i).draw(canvas); |
| 261 |
} |
| 262 |
} |
| 263 |
|
| 264 |
@Override |
| 265 |
protected void onLayout(boolean changed, int l, int t, int r, int b) { |
| 266 |
|
| 267 |
} |
| 268 |
|
| 269 |
|
| 270 |
|
| 271 |
|
| 272 |
|
| 273 |
|
| 274 |
|
| 275 |
|
| 276 |
|
| 277 |
|
| 278 |
@Override |
| 279 |
public void invalidate(Rect dirty) { |
| 280 |
super.invalidate(dirty); |
| 281 |
if (mHostView != null) { |
| 282 |
mHostView.invalidate(dirty); |
| 283 |
} |
| 284 |
} |
| 285 |
|
| 286 |
@Override |
| 287 |
public void invalidate(int l, int t, int r, int b) { |
| 288 |
super.invalidate(l, t, r, b); |
| 289 |
if (mHostView != null) { |
| 290 |
mHostView.invalidate(l, t, r, b); |
| 291 |
} |
| 292 |
} |
| 293 |
|
| 294 |
@Override |
| 295 |
public void invalidate() { |
| 296 |
super.invalidate(); |
| 297 |
if (mHostView != null) { |
| 298 |
mHostView.invalidate(); |
| 299 |
} |
| 300 |
} |
| 301 |
|
| 302 |
|
| 303 |
@Override |
| 304 |
public void invalidate(boolean invalidateCache) { |
| 305 |
super.invalidate(invalidateCache); |
| 306 |
if (mHostView != null) { |
| 307 |
mHostView.invalidate(invalidateCache); |
| 308 |
} |
| 309 |
} |
| 310 |
|
| 311 |
@Override |
| 312 |
void invalidateViewProperty(boolean invalidateParent, boolean forceRedraw) { |
| 313 |
super.invalidateViewProperty(invalidateParent, forceRedraw); |
| 314 |
if (mHostView != null) { |
| 315 |
mHostView.invalidateViewProperty(invalidateParent, forceRedraw); |
| 316 |
} |
| 317 |
} |
| 318 |
|
| 319 |
@Override |
| 320 |
protected void invalidateParentCaches() { |
| 321 |
super.invalidateParentCaches(); |
| 322 |
if (mHostView != null) { |
| 323 |
mHostView.invalidateParentCaches(); |
| 324 |
} |
| 325 |
} |
| 326 |
|
| 327 |
@Override |
| 328 |
protected void invalidateParentIfNeeded() { |
| 329 |
super.invalidateParentIfNeeded(); |
| 330 |
if (mHostView != null) { |
| 331 |
mHostView.invalidateParentIfNeeded(); |
| 332 |
} |
| 333 |
} |
| 334 |
|
| 335 |
@Override |
| 336 |
public void onDescendantInvalidated(@NonNull View child, @NonNull View target) { |
| 337 |
if (mHostView != null) { |
| 338 |
if (mHostView instanceof ViewGroup) { |
| 339 |
|
| 340 |
((ViewGroup) mHostView).onDescendantInvalidated(mHostView, target); |
| 341 |
|
| 342 |
|
| 343 |
|
| 344 |
super.onDescendantInvalidated(child, target); |
| 345 |
} else { |
| 346 |
|
| 347 |
|
| 348 |
invalidate(); |
| 349 |
} |
| 350 |
} |
| 351 |
} |
| 352 |
|
| 353 |
@Override |
| 354 |
public ViewParent invalidateChildInParent(int[] location, Rect dirty) { |
| 355 |
if (mHostView != null) { |
| 356 |
dirty.offset(location[0], location[1]); |
| 357 |
if (mHostView instanceof ViewGroup) { |
| 358 |
location[0] = 0; |
| 359 |
location[1] = 0; |
| 360 |
super.invalidateChildInParent(location, dirty); |
| 361 |
return ((ViewGroup) mHostView).invalidateChildInParent(location, dirty); |
| 362 |
} else { |
| 363 |
invalidate(dirty); |
| 364 |
} |
| 365 |
} |
| 366 |
return null; |
| 367 |
} |
| 368 |
} |
| 369 |
|
| 370 |
} |