| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
package android.transition; |
| 18 |
|
| 19 |
import android.animation.Animator; |
| 20 |
import android.animation.Animator.AnimatorListener; |
| 21 |
import android.animation.Animator.AnimatorPauseListener; |
| 22 |
import android.annotation.IntDef; |
| 23 |
import android.content.Context; |
| 24 |
import android.content.res.TypedArray; |
| 25 |
import android.util.AttributeSet; |
| 26 |
import android.view.View; |
| 27 |
import android.view.ViewGroup; |
| 28 |
import android.view.ViewGroupOverlay; |
| 29 |
|
| 30 |
import com.android.internal.R; |
| 31 |
|
| 32 |
import java.lang.annotation.Retention; |
| 33 |
import java.lang.annotation.RetentionPolicy; |
| 34 |
|
| 35 |
|
| 36 |
|
| 37 |
|
| 38 |
|
| 39 |
|
| 40 |
|
| 41 |
|
| 42 |
|
| 43 |
|
| 44 |
|
| 45 |
|
| 46 |
|
| 47 |
|
| 48 |
public abstract class Visibility extends Transition { |
| 49 |
|
| 50 |
static final String PROPNAME_VISIBILITY = "android:visibility:visibility"; |
| 51 |
private static final String PROPNAME_PARENT = "android:visibility:parent"; |
| 52 |
private static final String PROPNAME_SCREEN_LOCATION = "android:visibility:screenLocation"; |
| 53 |
|
| 54 |
|
| 55 |
@Retention(RetentionPolicy.SOURCE) |
| 56 |
@IntDef(flag = true, value = { |
| 57 |
MODE_IN, |
| 58 |
MODE_OUT, |
| 59 |
Fade.IN, |
| 60 |
Fade.OUT |
| 61 |
}) |
| 62 |
@interface VisibilityMode {} |
| 63 |
|
| 64 |
|
| 65 |
|
| 66 |
|
| 67 |
|
| 68 |
|
| 69 |
public static final int MODE_IN = 0x1; |
| 70 |
|
| 71 |
|
| 72 |
|
| 73 |
|
| 74 |
|
| 75 |
|
| 76 |
public static final int MODE_OUT = 0x2; |
| 77 |
|
| 78 |
private static final String[] sTransitionProperties = { |
| 79 |
PROPNAME_VISIBILITY, |
| 80 |
PROPNAME_PARENT, |
| 81 |
}; |
| 82 |
|
| 83 |
private static class VisibilityInfo { |
| 84 |
boolean visibilityChange; |
| 85 |
boolean fadeIn; |
| 86 |
int startVisibility; |
| 87 |
int endVisibility; |
| 88 |
ViewGroup startParent; |
| 89 |
ViewGroup endParent; |
| 90 |
} |
| 91 |
|
| 92 |
private int mMode = MODE_IN | MODE_OUT; |
| 93 |
private boolean mSuppressLayout = true; |
| 94 |
|
| 95 |
public Visibility() {} |
| 96 |
|
| 97 |
public Visibility(Context context, AttributeSet attrs) { |
| 98 |
super(context, attrs); |
| 99 |
TypedArray a = context.obtainStyledAttributes(attrs, R.styleable.VisibilityTransition); |
| 100 |
int mode = a.getInt(R.styleable.VisibilityTransition_transitionVisibilityMode, 0); |
| 101 |
a.recycle(); |
| 102 |
if (mode != 0) { |
| 103 |
setMode(mode); |
| 104 |
} |
| 105 |
} |
| 106 |
|
| 107 |
|
| 108 |
|
| 109 |
|
| 110 |
|
| 111 |
|
| 112 |
public void setSuppressLayout(boolean suppress) { |
| 113 |
this.mSuppressLayout = suppress; |
| 114 |
} |
| 115 |
|
| 116 |
|
| 117 |
|
| 118 |
|
| 119 |
|
| 120 |
|
| 121 |
|
| 122 |
|
| 123 |
|
| 124 |
public void setMode(@VisibilityMode int mode) { |
| 125 |
if ((mode & ~(MODE_IN | MODE_OUT)) != 0) { |
| 126 |
throw new IllegalArgumentException("Only MODE_IN and MODE_OUT flags are allowed"); |
| 127 |
} |
| 128 |
mMode = mode; |
| 129 |
} |
| 130 |
|
| 131 |
|
| 132 |
|
| 133 |
|
| 134 |
|
| 135 |
|
| 136 |
|
| 137 |
|
| 138 |
@VisibilityMode |
| 139 |
public int getMode() { |
| 140 |
return mMode; |
| 141 |
} |
| 142 |
|
| 143 |
@Override |
| 144 |
public String[] getTransitionProperties() { |
| 145 |
return sTransitionProperties; |
| 146 |
} |
| 147 |
|
| 148 |
private void captureValues(TransitionValues transitionValues) { |
| 149 |
int visibility = transitionValues.view.getVisibility(); |
| 150 |
transitionValues.values.put(PROPNAME_VISIBILITY, visibility); |
| 151 |
transitionValues.values.put(PROPNAME_PARENT, transitionValues.view.getParent()); |
| 152 |
int[] loc = new int[2]; |
| 153 |
transitionValues.view.getLocationOnScreen(loc); |
| 154 |
transitionValues.values.put(PROPNAME_SCREEN_LOCATION, loc); |
| 155 |
} |
| 156 |
|
| 157 |
@Override |
| 158 |
public void captureStartValues(TransitionValues transitionValues) { |
| 159 |
captureValues(transitionValues); |
| 160 |
} |
| 161 |
|
| 162 |
@Override |
| 163 |
public void captureEndValues(TransitionValues transitionValues) { |
| 164 |
captureValues(transitionValues); |
| 165 |
} |
| 166 |
|
| 167 |
|
| 168 |
|
| 169 |
|
| 170 |
|
| 171 |
|
| 172 |
|
| 173 |
|
| 174 |
|
| 175 |
|
| 176 |
|
| 177 |
|
| 178 |
|
| 179 |
|
| 180 |
|
| 181 |
|
| 182 |
public boolean isVisible(TransitionValues values) { |
| 183 |
if (values == null) { |
| 184 |
return false; |
| 185 |
} |
| 186 |
int visibility = (Integer) values.values.get(PROPNAME_VISIBILITY); |
| 187 |
View parent = (View) values.values.get(PROPNAME_PARENT); |
| 188 |
|
| 189 |
return visibility == View.VISIBLE && parent != null; |
| 190 |
} |
| 191 |
|
| 192 |
private static VisibilityInfo getVisibilityChangeInfo(TransitionValues startValues, |
| 193 |
TransitionValues endValues) { |
| 194 |
final VisibilityInfo visInfo = new VisibilityInfo(); |
| 195 |
visInfo.visibilityChange = false; |
| 196 |
visInfo.fadeIn = false; |
| 197 |
if (startValues != null && startValues.values.containsKey(PROPNAME_VISIBILITY)) { |
| 198 |
visInfo.startVisibility = (Integer) startValues.values.get(PROPNAME_VISIBILITY); |
| 199 |
visInfo.startParent = (ViewGroup) startValues.values.get(PROPNAME_PARENT); |
| 200 |
} else { |
| 201 |
visInfo.startVisibility = -1; |
| 202 |
visInfo.startParent = null; |
| 203 |
} |
| 204 |
if (endValues != null && endValues.values.containsKey(PROPNAME_VISIBILITY)) { |
| 205 |
visInfo.endVisibility = (Integer) endValues.values.get(PROPNAME_VISIBILITY); |
| 206 |
visInfo.endParent = (ViewGroup) endValues.values.get(PROPNAME_PARENT); |
| 207 |
} else { |
| 208 |
visInfo.endVisibility = -1; |
| 209 |
visInfo.endParent = null; |
| 210 |
} |
| 211 |
if (startValues != null && endValues != null) { |
| 212 |
if (visInfo.startVisibility == visInfo.endVisibility && |
| 213 |
visInfo.startParent == visInfo.endParent) { |
| 214 |
return visInfo; |
| 215 |
} else { |
| 216 |
if (visInfo.startVisibility != visInfo.endVisibility) { |
| 217 |
if (visInfo.startVisibility == View.VISIBLE) { |
| 218 |
visInfo.fadeIn = false; |
| 219 |
visInfo.visibilityChange = true; |
| 220 |
} else if (visInfo.endVisibility == View.VISIBLE) { |
| 221 |
visInfo.fadeIn = true; |
| 222 |
visInfo.visibilityChange = true; |
| 223 |
} |
| 224 |
|
| 225 |
} else if (visInfo.startParent != visInfo.endParent) { |
| 226 |
if (visInfo.endParent == null) { |
| 227 |
visInfo.fadeIn = false; |
| 228 |
visInfo.visibilityChange = true; |
| 229 |
} else if (visInfo.startParent == null) { |
| 230 |
visInfo.fadeIn = true; |
| 231 |
visInfo.visibilityChange = true; |
| 232 |
} |
| 233 |
} |
| 234 |
} |
| 235 |
} else if (startValues == null && visInfo.endVisibility == View.VISIBLE) { |
| 236 |
visInfo.fadeIn = true; |
| 237 |
visInfo.visibilityChange = true; |
| 238 |
} else if (endValues == null && visInfo.startVisibility == View.VISIBLE) { |
| 239 |
visInfo.fadeIn = false; |
| 240 |
visInfo.visibilityChange = true; |
| 241 |
} |
| 242 |
return visInfo; |
| 243 |
} |
| 244 |
|
| 245 |
@Override |
| 246 |
public Animator createAnimator(ViewGroup sceneRoot, TransitionValues startValues, |
| 247 |
TransitionValues endValues) { |
| 248 |
VisibilityInfo visInfo = getVisibilityChangeInfo(startValues, endValues); |
| 249 |
if (visInfo.visibilityChange |
| 250 |
&& (visInfo.startParent != null || visInfo.endParent != null)) { |
| 251 |
if (visInfo.fadeIn) { |
| 252 |
return onAppear(sceneRoot, startValues, visInfo.startVisibility, |
| 253 |
endValues, visInfo.endVisibility); |
| 254 |
} else { |
| 255 |
return onDisappear(sceneRoot, startValues, visInfo.startVisibility, |
| 256 |
endValues, visInfo.endVisibility |
| 257 |
); |
| 258 |
} |
| 259 |
} |
| 260 |
return null; |
| 261 |
} |
| 262 |
|
| 263 |
|
| 264 |
|
| 265 |
|
| 266 |
|
| 267 |
|
| 268 |
|
| 269 |
|
| 270 |
|
| 271 |
|
| 272 |
|
| 273 |
|
| 274 |
|
| 275 |
|
| 276 |
|
| 277 |
|
| 278 |
|
| 279 |
|
| 280 |
|
| 281 |
public Animator onAppear(ViewGroup sceneRoot, |
| 282 |
TransitionValues startValues, int startVisibility, |
| 283 |
TransitionValues endValues, int endVisibility) { |
| 284 |
if ((mMode & MODE_IN) != MODE_IN || endValues == null) { |
| 285 |
return null; |
| 286 |
} |
| 287 |
if (startValues == null) { |
| 288 |
VisibilityInfo parentVisibilityInfo = null; |
| 289 |
View endParent = (View) endValues.view.getParent(); |
| 290 |
TransitionValues startParentValues = getMatchedTransitionValues(endParent, |
| 291 |
false); |
| 292 |
TransitionValues endParentValues = getTransitionValues(endParent, false); |
| 293 |
parentVisibilityInfo = |
| 294 |
getVisibilityChangeInfo(startParentValues, endParentValues); |
| 295 |
if (parentVisibilityInfo.visibilityChange) { |
| 296 |
return null; |
| 297 |
} |
| 298 |
} |
| 299 |
return onAppear(sceneRoot, endValues.view, startValues, endValues); |
| 300 |
} |
| 301 |
|
| 302 |
|
| 303 |
|
| 304 |
|
| 305 |
|
| 306 |
|
| 307 |
|
| 308 |
|
| 309 |
|
| 310 |
|
| 311 |
|
| 312 |
|
| 313 |
|
| 314 |
|
| 315 |
|
| 316 |
|
| 317 |
public Animator onAppear(ViewGroup sceneRoot, View view, TransitionValues startValues, |
| 318 |
TransitionValues endValues) { |
| 319 |
return null; |
| 320 |
} |
| 321 |
|
| 322 |
|
| 323 |
|
| 324 |
|
| 325 |
|
| 326 |
|
| 327 |
|
| 328 |
|
| 329 |
|
| 330 |
|
| 331 |
|
| 332 |
|
| 333 |
|
| 334 |
|
| 335 |
|
| 336 |
|
| 337 |
|
| 338 |
|
| 339 |
|
| 340 |
|
| 341 |
|
| 342 |
|
| 343 |
|
| 344 |
|
| 345 |
|
| 346 |
|
| 347 |
|
| 348 |
|
| 349 |
|
| 350 |
|
| 351 |
|
| 352 |
|
| 353 |
|
| 354 |
|
| 355 |
|
| 356 |
public Animator onDisappear(ViewGroup sceneRoot, |
| 357 |
TransitionValues startValues, int startVisibility, |
| 358 |
TransitionValues endValues, int endVisibility) { |
| 359 |
if ((mMode & MODE_OUT) != MODE_OUT) { |
| 360 |
return null; |
| 361 |
} |
| 362 |
|
| 363 |
if (startValues == null) { |
| 364 |
|
| 365 |
return null; |
| 366 |
} |
| 367 |
|
| 368 |
final View startView = startValues.view; |
| 369 |
final View endView = (endValues != null) ? endValues.view : null; |
| 370 |
View overlayView = null; |
| 371 |
View viewToKeep = null; |
| 372 |
boolean reusingOverlayView = false; |
| 373 |
|
| 374 |
View savedOverlayView = (View) startView.getTag(R.id.transition_overlay_view_tag); |
| 375 |
if (savedOverlayView != null) { |
| 376 |
|
| 377 |
|
| 378 |
|
| 379 |
overlayView = savedOverlayView; |
| 380 |
reusingOverlayView = true; |
| 381 |
} else { |
| 382 |
boolean needOverlayForStartView = false; |
| 383 |
|
| 384 |
if (endView == null || endView.getParent() == null) { |
| 385 |
if (endView != null) { |
| 386 |
|
| 387 |
overlayView = endView; |
| 388 |
} else { |
| 389 |
needOverlayForStartView = true; |
| 390 |
} |
| 391 |
} else { |
| 392 |
|
| 393 |
if (endVisibility == View.INVISIBLE) { |
| 394 |
viewToKeep = endView; |
| 395 |
} else { |
| 396 |
|
| 397 |
if (startView == endView) { |
| 398 |
viewToKeep = endView; |
| 399 |
} else { |
| 400 |
needOverlayForStartView = true; |
| 401 |
} |
| 402 |
} |
| 403 |
} |
| 404 |
|
| 405 |
if (needOverlayForStartView) { |
| 406 |
|
| 407 |
|
| 408 |
|
| 409 |
if (startView.getParent() == null) { |
| 410 |
|
| 411 |
overlayView = startView; |
| 412 |
} else if (startView.getParent() instanceof View) { |
| 413 |
View startParent = (View) startView.getParent(); |
| 414 |
TransitionValues startParentValues = getTransitionValues(startParent, true); |
| 415 |
TransitionValues endParentValues = getMatchedTransitionValues(startParent, |
| 416 |
true); |
| 417 |
VisibilityInfo parentVisibilityInfo = |
| 418 |
getVisibilityChangeInfo(startParentValues, endParentValues); |
| 419 |
if (!parentVisibilityInfo.visibilityChange) { |
| 420 |
overlayView = TransitionUtils.copyViewImage(sceneRoot, startView, |
| 421 |
startParent); |
| 422 |
} else { |
| 423 |
int id = startParent.getId(); |
| 424 |
if (startParent.getParent() == null && id != View.NO_ID |
| 425 |
&& sceneRoot.findViewById(id) != null && mCanRemoveViews) { |
| 426 |
|
| 427 |
|
| 428 |
|
| 429 |
overlayView = startView; |
| 430 |
} else { |
| 431 |
|
| 432 |
} |
| 433 |
} |
| 434 |
} |
| 435 |
} |
| 436 |
} |
| 437 |
|
| 438 |
if (overlayView != null) { |
| 439 |
|
| 440 |
final ViewGroupOverlay overlay; |
| 441 |
if (!reusingOverlayView) { |
| 442 |
overlay = sceneRoot.getOverlay(); |
| 443 |
int[] screenLoc = (int[]) startValues.values.get(PROPNAME_SCREEN_LOCATION); |
| 444 |
int screenX = screenLoc[0]; |
| 445 |
int screenY = screenLoc[1]; |
| 446 |
int[] loc = new int[2]; |
| 447 |
sceneRoot.getLocationOnScreen(loc); |
| 448 |
overlayView.offsetLeftAndRight((screenX - loc[0]) - overlayView.getLeft()); |
| 449 |
overlayView.offsetTopAndBottom((screenY - loc[1]) - overlayView.getTop()); |
| 450 |
overlay.add(overlayView); |
| 451 |
} else { |
| 452 |
overlay = null; |
| 453 |
} |
| 454 |
Animator animator = onDisappear(sceneRoot, overlayView, startValues, endValues); |
| 455 |
if (!reusingOverlayView) { |
| 456 |
if (animator == null) { |
| 457 |
overlay.remove(overlayView); |
| 458 |
} else { |
| 459 |
startView.setTagInternal(R.id.transition_overlay_view_tag, overlayView); |
| 460 |
final View finalOverlayView = overlayView; |
| 461 |
addListener(new TransitionListenerAdapter() { |
| 462 |
|
| 463 |
@Override |
| 464 |
public void onTransitionPause(Transition transition) { |
| 465 |
overlay.remove(finalOverlayView); |
| 466 |
} |
| 467 |
|
| 468 |
@Override |
| 469 |
public void onTransitionResume(Transition transition) { |
| 470 |
if (finalOverlayView.getParent() == null) { |
| 471 |
overlay.add(finalOverlayView); |
| 472 |
} else { |
| 473 |
cancel(); |
| 474 |
} |
| 475 |
} |
| 476 |
|
| 477 |
@Override |
| 478 |
public void onTransitionEnd(Transition transition) { |
| 479 |
startView.setTagInternal(R.id.transition_overlay_view_tag, null); |
| 480 |
overlay.remove(finalOverlayView); |
| 481 |
transition.removeListener(this); |
| 482 |
} |
| 483 |
}); |
| 484 |
} |
| 485 |
} |
| 486 |
return animator; |
| 487 |
} |
| 488 |
|
| 489 |
if (viewToKeep != null) { |
| 490 |
int originalVisibility = viewToKeep.getVisibility(); |
| 491 |
viewToKeep.setTransitionVisibility(View.VISIBLE); |
| 492 |
Animator animator = onDisappear(sceneRoot, viewToKeep, startValues, endValues); |
| 493 |
if (animator != null) { |
| 494 |
DisappearListener disappearListener = new DisappearListener(viewToKeep, |
| 495 |
endVisibility, mSuppressLayout); |
| 496 |
animator.addListener(disappearListener); |
| 497 |
animator.addPauseListener(disappearListener); |
| 498 |
addListener(disappearListener); |
| 499 |
} else { |
| 500 |
viewToKeep.setTransitionVisibility(originalVisibility); |
| 501 |
} |
| 502 |
return animator; |
| 503 |
} |
| 504 |
return null; |
| 505 |
} |
| 506 |
|
| 507 |
@Override |
| 508 |
public boolean isTransitionRequired(TransitionValues startValues, TransitionValues newValues) { |
| 509 |
if (startValues == null && newValues == null) { |
| 510 |
return false; |
| 511 |
} |
| 512 |
if (startValues != null && newValues != null && |
| 513 |
newValues.values.containsKey(PROPNAME_VISIBILITY) != |
| 514 |
startValues.values.containsKey(PROPNAME_VISIBILITY)) { |
| 515 |
|
| 516 |
|
| 517 |
return false; |
| 518 |
} |
| 519 |
VisibilityInfo changeInfo = getVisibilityChangeInfo(startValues, newValues); |
| 520 |
return changeInfo.visibilityChange && (changeInfo.startVisibility == View.VISIBLE || |
| 521 |
changeInfo.endVisibility == View.VISIBLE); |
| 522 |
} |
| 523 |
|
| 524 |
|
| 525 |
|
| 526 |
|
| 527 |
|
| 528 |
|
| 529 |
|
| 530 |
|
| 531 |
|
| 532 |
|
| 533 |
|
| 534 |
|
| 535 |
|
| 536 |
|
| 537 |
|
| 538 |
|
| 539 |
|
| 540 |
public Animator onDisappear(ViewGroup sceneRoot, View view, TransitionValues startValues, |
| 541 |
TransitionValues endValues) { |
| 542 |
return null; |
| 543 |
} |
| 544 |
|
| 545 |
private static class DisappearListener |
| 546 |
extends TransitionListenerAdapter implements AnimatorListener, AnimatorPauseListener { |
| 547 |
private final View mView; |
| 548 |
private final int mFinalVisibility; |
| 549 |
private final ViewGroup mParent; |
| 550 |
private final boolean mSuppressLayout; |
| 551 |
|
| 552 |
private boolean mLayoutSuppressed; |
| 553 |
boolean mCanceled = false; |
| 554 |
|
| 555 |
public DisappearListener(View view, int finalVisibility, boolean suppressLayout) { |
| 556 |
this.mView = view; |
| 557 |
this.mFinalVisibility = finalVisibility; |
| 558 |
this.mParent = (ViewGroup) view.getParent(); |
| 559 |
this.mSuppressLayout = suppressLayout; |
| 560 |
|
| 561 |
suppressLayout(true); |
| 562 |
} |
| 563 |
|
| 564 |
@Override |
| 565 |
public void onAnimationPause(Animator animation) { |
| 566 |
if (!mCanceled) { |
| 567 |
mView.setTransitionVisibility(mFinalVisibility); |
| 568 |
} |
| 569 |
} |
| 570 |
|
| 571 |
@Override |
| 572 |
public void onAnimationResume(Animator animation) { |
| 573 |
if (!mCanceled) { |
| 574 |
mView.setTransitionVisibility(View.VISIBLE); |
| 575 |
} |
| 576 |
} |
| 577 |
|
| 578 |
@Override |
| 579 |
public void onAnimationCancel(Animator animation) { |
| 580 |
mCanceled = true; |
| 581 |
} |
| 582 |
|
| 583 |
@Override |
| 584 |
public void onAnimationRepeat(Animator animation) { |
| 585 |
} |
| 586 |
|
| 587 |
@Override |
| 588 |
public void onAnimationStart(Animator animation) { |
| 589 |
} |
| 590 |
|
| 591 |
@Override |
| 592 |
public void onAnimationEnd(Animator animation) { |
| 593 |
hideViewWhenNotCanceled(); |
| 594 |
} |
| 595 |
|
| 596 |
@Override |
| 597 |
public void onTransitionEnd(Transition transition) { |
| 598 |
hideViewWhenNotCanceled(); |
| 599 |
transition.removeListener(this); |
| 600 |
} |
| 601 |
|
| 602 |
@Override |
| 603 |
public void onTransitionPause(Transition transition) { |
| 604 |
suppressLayout(false); |
| 605 |
} |
| 606 |
|
| 607 |
@Override |
| 608 |
public void onTransitionResume(Transition transition) { |
| 609 |
suppressLayout(true); |
| 610 |
} |
| 611 |
|
| 612 |
private void hideViewWhenNotCanceled() { |
| 613 |
if (!mCanceled) { |
| 614 |
|
| 615 |
mView.setTransitionVisibility(mFinalVisibility); |
| 616 |
if (mParent != null) { |
| 617 |
mParent.invalidate(); |
| 618 |
} |
| 619 |
} |
| 620 |
|
| 621 |
suppressLayout(false); |
| 622 |
} |
| 623 |
|
| 624 |
private void suppressLayout(boolean suppress) { |
| 625 |
if (mSuppressLayout && mLayoutSuppressed != suppress && mParent != null) { |
| 626 |
mLayoutSuppressed = suppress; |
| 627 |
mParent.suppressLayout(suppress); |
| 628 |
} |
| 629 |
} |
| 630 |
} |
| 631 |
} |