| 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.TimeInterpolator; |
| 20 |
import android.content.Context; |
| 21 |
import android.content.res.TypedArray; |
| 22 |
import android.util.AndroidRuntimeException; |
| 23 |
import android.util.AttributeSet; |
| 24 |
import android.view.View; |
| 25 |
import android.view.ViewGroup; |
| 26 |
|
| 27 |
import com.android.internal.R; |
| 28 |
|
| 29 |
import java.util.ArrayList; |
| 30 |
|
| 31 |
|
| 32 |
|
| 33 |
|
| 34 |
|
| 35 |
|
| 36 |
|
| 37 |
|
| 38 |
|
| 39 |
|
| 40 |
|
| 41 |
|
| 42 |
|
| 43 |
|
| 44 |
|
| 45 |
|
| 46 |
|
| 47 |
|
| 48 |
|
| 49 |
|
| 50 |
|
| 51 |
|
| 52 |
|
| 53 |
|
| 54 |
|
| 55 |
public class TransitionSet extends Transition { |
| 56 |
|
| 57 |
|
| 58 |
|
| 59 |
private static final int FLAG_CHANGE_INTERPOLATOR = 0x01; |
| 60 |
|
| 61 |
|
| 62 |
|
| 63 |
private static final int FLAG_CHANGE_PROPAGATION = 0x02; |
| 64 |
|
| 65 |
|
| 66 |
|
| 67 |
private static final int FLAG_CHANGE_PATH_MOTION = 0x04; |
| 68 |
|
| 69 |
|
| 70 |
|
| 71 |
static final int FLAG_CHANGE_EPICENTER = 0x08; |
| 72 |
|
| 73 |
ArrayList<Transition> mTransitions = new ArrayList<Transition>(); |
| 74 |
private boolean mPlayTogether = true; |
| 75 |
int mCurrentListeners; |
| 76 |
boolean mStarted = false; |
| 77 |
|
| 78 |
|
| 79 |
private int mChangeFlags = 0; |
| 80 |
|
| 81 |
|
| 82 |
|
| 83 |
|
| 84 |
|
| 85 |
public static final int ORDERING_TOGETHER = 0; |
| 86 |
|
| 87 |
|
| 88 |
|
| 89 |
|
| 90 |
|
| 91 |
|
| 92 |
|
| 93 |
public static final int ORDERING_SEQUENTIAL = 1; |
| 94 |
|
| 95 |
|
| 96 |
|
| 97 |
|
| 98 |
|
| 99 |
|
| 100 |
public TransitionSet() { |
| 101 |
} |
| 102 |
|
| 103 |
public TransitionSet(Context context, AttributeSet attrs) { |
| 104 |
super(context, attrs); |
| 105 |
TypedArray a = context.obtainStyledAttributes(attrs, R.styleable.TransitionSet); |
| 106 |
int ordering = a.getInt(R.styleable.TransitionSet_transitionOrdering, |
| 107 |
TransitionSet.ORDERING_TOGETHER); |
| 108 |
setOrdering(ordering); |
| 109 |
a.recycle(); |
| 110 |
} |
| 111 |
|
| 112 |
|
| 113 |
|
| 114 |
|
| 115 |
|
| 116 |
|
| 117 |
|
| 118 |
|
| 119 |
|
| 120 |
public TransitionSet setOrdering(int ordering) { |
| 121 |
switch (ordering) { |
| 122 |
case ORDERING_SEQUENTIAL: |
| 123 |
mPlayTogether = false; |
| 124 |
break; |
| 125 |
case ORDERING_TOGETHER: |
| 126 |
mPlayTogether = true; |
| 127 |
break; |
| 128 |
default: |
| 129 |
throw new AndroidRuntimeException("Invalid parameter for TransitionSet " + |
| 130 |
"ordering: " + ordering); |
| 131 |
} |
| 132 |
return this; |
| 133 |
} |
| 134 |
|
| 135 |
|
| 136 |
|
| 137 |
|
| 138 |
|
| 139 |
|
| 140 |
|
| 141 |
|
| 142 |
|
| 143 |
|
| 144 |
public int getOrdering() { |
| 145 |
return mPlayTogether ? ORDERING_TOGETHER : ORDERING_SEQUENTIAL; |
| 146 |
} |
| 147 |
|
| 148 |
|
| 149 |
|
| 150 |
|
| 151 |
|
| 152 |
|
| 153 |
|
| 154 |
|
| 155 |
|
| 156 |
|
| 157 |
|
| 158 |
|
| 159 |
|
| 160 |
|
| 161 |
|
| 162 |
|
| 163 |
|
| 164 |
public TransitionSet addTransition(Transition transition) { |
| 165 |
if (transition != null) { |
| 166 |
addTransitionInternal(transition); |
| 167 |
if (mDuration >= 0) { |
| 168 |
transition.setDuration(mDuration); |
| 169 |
} |
| 170 |
if ((mChangeFlags & FLAG_CHANGE_INTERPOLATOR) != 0) { |
| 171 |
transition.setInterpolator(getInterpolator()); |
| 172 |
} |
| 173 |
if ((mChangeFlags & FLAG_CHANGE_PROPAGATION) != 0) { |
| 174 |
transition.setPropagation(getPropagation()); |
| 175 |
} |
| 176 |
if ((mChangeFlags & FLAG_CHANGE_PATH_MOTION) != 0) { |
| 177 |
transition.setPathMotion(getPathMotion()); |
| 178 |
} |
| 179 |
if ((mChangeFlags & FLAG_CHANGE_EPICENTER) != 0) { |
| 180 |
transition.setEpicenterCallback(getEpicenterCallback()); |
| 181 |
} |
| 182 |
} |
| 183 |
return this; |
| 184 |
} |
| 185 |
|
| 186 |
private void addTransitionInternal(Transition transition) { |
| 187 |
mTransitions.add(transition); |
| 188 |
transition.mParent = this; |
| 189 |
} |
| 190 |
|
| 191 |
|
| 192 |
|
| 193 |
|
| 194 |
|
| 195 |
|
| 196 |
|
| 197 |
|
| 198 |
public int getTransitionCount() { |
| 199 |
return mTransitions.size(); |
| 200 |
} |
| 201 |
|
| 202 |
|
| 203 |
|
| 204 |
|
| 205 |
|
| 206 |
|
| 207 |
|
| 208 |
|
| 209 |
public Transition getTransitionAt(int index) { |
| 210 |
if (index < 0 || index >= mTransitions.size()) { |
| 211 |
return null; |
| 212 |
} |
| 213 |
return mTransitions.get(index); |
| 214 |
} |
| 215 |
|
| 216 |
|
| 217 |
|
| 218 |
|
| 219 |
|
| 220 |
|
| 221 |
|
| 222 |
|
| 223 |
@Override |
| 224 |
public TransitionSet setDuration(long duration) { |
| 225 |
super.setDuration(duration); |
| 226 |
if (mDuration >= 0 && mTransitions != null) { |
| 227 |
int numTransitions = mTransitions.size(); |
| 228 |
for (int i = 0; i < numTransitions; ++i) { |
| 229 |
mTransitions.get(i).setDuration(duration); |
| 230 |
} |
| 231 |
} |
| 232 |
return this; |
| 233 |
} |
| 234 |
|
| 235 |
@Override |
| 236 |
public TransitionSet setStartDelay(long startDelay) { |
| 237 |
return (TransitionSet) super.setStartDelay(startDelay); |
| 238 |
} |
| 239 |
|
| 240 |
@Override |
| 241 |
public TransitionSet setInterpolator(TimeInterpolator interpolator) { |
| 242 |
mChangeFlags |= FLAG_CHANGE_INTERPOLATOR; |
| 243 |
if (mTransitions != null) { |
| 244 |
int numTransitions = mTransitions.size(); |
| 245 |
for (int i = 0; i < numTransitions; ++i) { |
| 246 |
mTransitions.get(i).setInterpolator(interpolator); |
| 247 |
} |
| 248 |
} |
| 249 |
return (TransitionSet) super.setInterpolator(interpolator); |
| 250 |
} |
| 251 |
|
| 252 |
@Override |
| 253 |
public TransitionSet addTarget(View target) { |
| 254 |
for (int i = 0; i < mTransitions.size(); i++) { |
| 255 |
mTransitions.get(i).addTarget(target); |
| 256 |
} |
| 257 |
return (TransitionSet) super.addTarget(target); |
| 258 |
} |
| 259 |
|
| 260 |
@Override |
| 261 |
public TransitionSet addTarget(int targetId) { |
| 262 |
for (int i = 0; i < mTransitions.size(); i++) { |
| 263 |
mTransitions.get(i).addTarget(targetId); |
| 264 |
} |
| 265 |
return (TransitionSet) super.addTarget(targetId); |
| 266 |
} |
| 267 |
|
| 268 |
@Override |
| 269 |
public TransitionSet addTarget(String targetName) { |
| 270 |
for (int i = 0; i < mTransitions.size(); i++) { |
| 271 |
mTransitions.get(i).addTarget(targetName); |
| 272 |
} |
| 273 |
return (TransitionSet) super.addTarget(targetName); |
| 274 |
} |
| 275 |
|
| 276 |
@Override |
| 277 |
public TransitionSet addTarget(Class targetType) { |
| 278 |
for (int i = 0; i < mTransitions.size(); i++) { |
| 279 |
mTransitions.get(i).addTarget(targetType); |
| 280 |
} |
| 281 |
return (TransitionSet) super.addTarget(targetType); |
| 282 |
} |
| 283 |
|
| 284 |
@Override |
| 285 |
public TransitionSet addListener(TransitionListener listener) { |
| 286 |
return (TransitionSet) super.addListener(listener); |
| 287 |
} |
| 288 |
|
| 289 |
@Override |
| 290 |
public TransitionSet removeTarget(int targetId) { |
| 291 |
for (int i = 0; i < mTransitions.size(); i++) { |
| 292 |
mTransitions.get(i).removeTarget(targetId); |
| 293 |
} |
| 294 |
return (TransitionSet) super.removeTarget(targetId); |
| 295 |
} |
| 296 |
|
| 297 |
@Override |
| 298 |
public TransitionSet removeTarget(View target) { |
| 299 |
for (int i = 0; i < mTransitions.size(); i++) { |
| 300 |
mTransitions.get(i).removeTarget(target); |
| 301 |
} |
| 302 |
return (TransitionSet) super.removeTarget(target); |
| 303 |
} |
| 304 |
|
| 305 |
@Override |
| 306 |
public TransitionSet removeTarget(Class target) { |
| 307 |
for (int i = 0; i < mTransitions.size(); i++) { |
| 308 |
mTransitions.get(i).removeTarget(target); |
| 309 |
} |
| 310 |
return (TransitionSet) super.removeTarget(target); |
| 311 |
} |
| 312 |
|
| 313 |
@Override |
| 314 |
public TransitionSet removeTarget(String target) { |
| 315 |
for (int i = 0; i < mTransitions.size(); i++) { |
| 316 |
mTransitions.get(i).removeTarget(target); |
| 317 |
} |
| 318 |
return (TransitionSet) super.removeTarget(target); |
| 319 |
} |
| 320 |
|
| 321 |
@Override |
| 322 |
public Transition excludeTarget(View target, boolean exclude) { |
| 323 |
for (int i = 0; i < mTransitions.size(); i++) { |
| 324 |
mTransitions.get(i).excludeTarget(target, exclude); |
| 325 |
} |
| 326 |
return super.excludeTarget(target, exclude); |
| 327 |
} |
| 328 |
|
| 329 |
@Override |
| 330 |
public Transition excludeTarget(String targetName, boolean exclude) { |
| 331 |
for (int i = 0; i < mTransitions.size(); i++) { |
| 332 |
mTransitions.get(i).excludeTarget(targetName, exclude); |
| 333 |
} |
| 334 |
return super.excludeTarget(targetName, exclude); |
| 335 |
} |
| 336 |
|
| 337 |
@Override |
| 338 |
public Transition excludeTarget(int targetId, boolean exclude) { |
| 339 |
for (int i = 0; i < mTransitions.size(); i++) { |
| 340 |
mTransitions.get(i).excludeTarget(targetId, exclude); |
| 341 |
} |
| 342 |
return super.excludeTarget(targetId, exclude); |
| 343 |
} |
| 344 |
|
| 345 |
@Override |
| 346 |
public Transition excludeTarget(Class type, boolean exclude) { |
| 347 |
for (int i = 0; i < mTransitions.size(); i++) { |
| 348 |
mTransitions.get(i).excludeTarget(type, exclude); |
| 349 |
} |
| 350 |
return super.excludeTarget(type, exclude); |
| 351 |
} |
| 352 |
|
| 353 |
@Override |
| 354 |
public TransitionSet removeListener(TransitionListener listener) { |
| 355 |
return (TransitionSet) super.removeListener(listener); |
| 356 |
} |
| 357 |
|
| 358 |
@Override |
| 359 |
public void setPathMotion(PathMotion pathMotion) { |
| 360 |
super.setPathMotion(pathMotion); |
| 361 |
mChangeFlags |= FLAG_CHANGE_PATH_MOTION; |
| 362 |
if (mTransitions != null) { |
| 363 |
for (int i = 0; i < mTransitions.size(); i++) { |
| 364 |
mTransitions.get(i).setPathMotion(pathMotion); |
| 365 |
} |
| 366 |
} |
| 367 |
} |
| 368 |
|
| 369 |
|
| 370 |
|
| 371 |
|
| 372 |
|
| 373 |
|
| 374 |
|
| 375 |
public TransitionSet removeTransition(Transition transition) { |
| 376 |
mTransitions.remove(transition); |
| 377 |
transition.mParent = null; |
| 378 |
return this; |
| 379 |
} |
| 380 |
|
| 381 |
|
| 382 |
|
| 383 |
|
| 384 |
|
| 385 |
|
| 386 |
private void setupStartEndListeners() { |
| 387 |
TransitionSetListener listener = new TransitionSetListener(this); |
| 388 |
for (Transition childTransition : mTransitions) { |
| 389 |
childTransition.addListener(listener); |
| 390 |
} |
| 391 |
mCurrentListeners = mTransitions.size(); |
| 392 |
} |
| 393 |
|
| 394 |
|
| 395 |
|
| 396 |
|
| 397 |
|
| 398 |
static class TransitionSetListener extends TransitionListenerAdapter { |
| 399 |
TransitionSet mTransitionSet; |
| 400 |
TransitionSetListener(TransitionSet transitionSet) { |
| 401 |
mTransitionSet = transitionSet; |
| 402 |
} |
| 403 |
@Override |
| 404 |
public void onTransitionStart(Transition transition) { |
| 405 |
if (!mTransitionSet.mStarted) { |
| 406 |
mTransitionSet.start(); |
| 407 |
mTransitionSet.mStarted = true; |
| 408 |
} |
| 409 |
} |
| 410 |
|
| 411 |
@Override |
| 412 |
public void onTransitionEnd(Transition transition) { |
| 413 |
--mTransitionSet.mCurrentListeners; |
| 414 |
if (mTransitionSet.mCurrentListeners == 0) { |
| 415 |
|
| 416 |
mTransitionSet.mStarted = false; |
| 417 |
mTransitionSet.end(); |
| 418 |
} |
| 419 |
transition.removeListener(this); |
| 420 |
} |
| 421 |
} |
| 422 |
|
| 423 |
|
| 424 |
|
| 425 |
|
| 426 |
@Override |
| 427 |
protected void createAnimators(ViewGroup sceneRoot, TransitionValuesMaps startValues, |
| 428 |
TransitionValuesMaps endValues, ArrayList<TransitionValues> startValuesList, |
| 429 |
ArrayList<TransitionValues> endValuesList) { |
| 430 |
long startDelay = getStartDelay(); |
| 431 |
int numTransitions = mTransitions.size(); |
| 432 |
for (int i = 0; i < numTransitions; i++) { |
| 433 |
Transition childTransition = mTransitions.get(i); |
| 434 |
|
| 435 |
|
| 436 |
if (startDelay > 0 && (mPlayTogether || i == 0)) { |
| 437 |
long childStartDelay = childTransition.getStartDelay(); |
| 438 |
if (childStartDelay > 0) { |
| 439 |
childTransition.setStartDelay(startDelay + childStartDelay); |
| 440 |
} else { |
| 441 |
childTransition.setStartDelay(startDelay); |
| 442 |
} |
| 443 |
} |
| 444 |
childTransition.createAnimators(sceneRoot, startValues, endValues, startValuesList, |
| 445 |
endValuesList); |
| 446 |
} |
| 447 |
} |
| 448 |
|
| 449 |
|
| 450 |
|
| 451 |
|
| 452 |
@Override |
| 453 |
protected void runAnimators() { |
| 454 |
if (mTransitions.isEmpty()) { |
| 455 |
start(); |
| 456 |
end(); |
| 457 |
return; |
| 458 |
} |
| 459 |
setupStartEndListeners(); |
| 460 |
int numTransitions = mTransitions.size(); |
| 461 |
if (!mPlayTogether) { |
| 462 |
|
| 463 |
|
| 464 |
for (int i = 1; i < numTransitions; ++i) { |
| 465 |
Transition previousTransition = mTransitions.get(i - 1); |
| 466 |
final Transition nextTransition = mTransitions.get(i); |
| 467 |
previousTransition.addListener(new TransitionListenerAdapter() { |
| 468 |
@Override |
| 469 |
public void onTransitionEnd(Transition transition) { |
| 470 |
nextTransition.runAnimators(); |
| 471 |
transition.removeListener(this); |
| 472 |
} |
| 473 |
}); |
| 474 |
} |
| 475 |
Transition firstTransition = mTransitions.get(0); |
| 476 |
if (firstTransition != null) { |
| 477 |
firstTransition.runAnimators(); |
| 478 |
} |
| 479 |
} else { |
| 480 |
for (int i = 0; i < numTransitions; ++i) { |
| 481 |
mTransitions.get(i).runAnimators(); |
| 482 |
} |
| 483 |
} |
| 484 |
} |
| 485 |
|
| 486 |
@Override |
| 487 |
public void captureStartValues(TransitionValues transitionValues) { |
| 488 |
if (isValidTarget(transitionValues.view)) { |
| 489 |
for (Transition childTransition : mTransitions) { |
| 490 |
if (childTransition.isValidTarget(transitionValues.view)) { |
| 491 |
childTransition.captureStartValues(transitionValues); |
| 492 |
transitionValues.targetedTransitions.add(childTransition); |
| 493 |
} |
| 494 |
} |
| 495 |
} |
| 496 |
} |
| 497 |
|
| 498 |
@Override |
| 499 |
public void captureEndValues(TransitionValues transitionValues) { |
| 500 |
if (isValidTarget(transitionValues.view)) { |
| 501 |
for (Transition childTransition : mTransitions) { |
| 502 |
if (childTransition.isValidTarget(transitionValues.view)) { |
| 503 |
childTransition.captureEndValues(transitionValues); |
| 504 |
transitionValues.targetedTransitions.add(childTransition); |
| 505 |
} |
| 506 |
} |
| 507 |
} |
| 508 |
} |
| 509 |
|
| 510 |
@Override |
| 511 |
void capturePropagationValues(TransitionValues transitionValues) { |
| 512 |
super.capturePropagationValues(transitionValues); |
| 513 |
int numTransitions = mTransitions.size(); |
| 514 |
for (int i = 0; i < numTransitions; ++i) { |
| 515 |
mTransitions.get(i).capturePropagationValues(transitionValues); |
| 516 |
} |
| 517 |
} |
| 518 |
|
| 519 |
|
| 520 |
@Override |
| 521 |
public void pause(View sceneRoot) { |
| 522 |
super.pause(sceneRoot); |
| 523 |
int numTransitions = mTransitions.size(); |
| 524 |
for (int i = 0; i < numTransitions; ++i) { |
| 525 |
mTransitions.get(i).pause(sceneRoot); |
| 526 |
} |
| 527 |
} |
| 528 |
|
| 529 |
|
| 530 |
@Override |
| 531 |
public void resume(View sceneRoot) { |
| 532 |
super.resume(sceneRoot); |
| 533 |
int numTransitions = mTransitions.size(); |
| 534 |
for (int i = 0; i < numTransitions; ++i) { |
| 535 |
mTransitions.get(i).resume(sceneRoot); |
| 536 |
} |
| 537 |
} |
| 538 |
|
| 539 |
|
| 540 |
@Override |
| 541 |
protected void cancel() { |
| 542 |
super.cancel(); |
| 543 |
int numTransitions = mTransitions.size(); |
| 544 |
for (int i = 0; i < numTransitions; ++i) { |
| 545 |
mTransitions.get(i).cancel(); |
| 546 |
} |
| 547 |
} |
| 548 |
|
| 549 |
|
| 550 |
@Override |
| 551 |
void forceToEnd(ViewGroup sceneRoot) { |
| 552 |
super.forceToEnd(sceneRoot); |
| 553 |
int numTransitions = mTransitions.size(); |
| 554 |
for (int i = 0; i < numTransitions; ++i) { |
| 555 |
mTransitions.get(i).forceToEnd(sceneRoot); |
| 556 |
} |
| 557 |
} |
| 558 |
|
| 559 |
@Override |
| 560 |
TransitionSet setSceneRoot(ViewGroup sceneRoot) { |
| 561 |
super.setSceneRoot(sceneRoot); |
| 562 |
int numTransitions = mTransitions.size(); |
| 563 |
for (int i = 0; i < numTransitions; ++i) { |
| 564 |
mTransitions.get(i).setSceneRoot(sceneRoot); |
| 565 |
} |
| 566 |
return (TransitionSet) this; |
| 567 |
} |
| 568 |
|
| 569 |
@Override |
| 570 |
void setCanRemoveViews(boolean canRemoveViews) { |
| 571 |
super.setCanRemoveViews(canRemoveViews); |
| 572 |
int numTransitions = mTransitions.size(); |
| 573 |
for (int i = 0; i < numTransitions; ++i) { |
| 574 |
mTransitions.get(i).setCanRemoveViews(canRemoveViews); |
| 575 |
} |
| 576 |
} |
| 577 |
|
| 578 |
@Override |
| 579 |
public void setPropagation(TransitionPropagation propagation) { |
| 580 |
super.setPropagation(propagation); |
| 581 |
mChangeFlags |= FLAG_CHANGE_PROPAGATION; |
| 582 |
int numTransitions = mTransitions.size(); |
| 583 |
for (int i = 0; i < numTransitions; ++i) { |
| 584 |
mTransitions.get(i).setPropagation(propagation); |
| 585 |
} |
| 586 |
} |
| 587 |
|
| 588 |
@Override |
| 589 |
public void setEpicenterCallback(EpicenterCallback epicenterCallback) { |
| 590 |
super.setEpicenterCallback(epicenterCallback); |
| 591 |
mChangeFlags |= FLAG_CHANGE_EPICENTER; |
| 592 |
int numTransitions = mTransitions.size(); |
| 593 |
for (int i = 0; i < numTransitions; ++i) { |
| 594 |
mTransitions.get(i).setEpicenterCallback(epicenterCallback); |
| 595 |
} |
| 596 |
} |
| 597 |
|
| 598 |
@Override |
| 599 |
String toString(String indent) { |
| 600 |
String result = super.toString(indent); |
| 601 |
for (int i = 0; i < mTransitions.size(); ++i) { |
| 602 |
result += "\n" + mTransitions.get(i).toString(indent + " "); |
| 603 |
} |
| 604 |
return result; |
| 605 |
} |
| 606 |
|
| 607 |
@Override |
| 608 |
public TransitionSet clone() { |
| 609 |
TransitionSet clone = (TransitionSet) super.clone(); |
| 610 |
clone.mTransitions = new ArrayList<Transition>(); |
| 611 |
int numTransitions = mTransitions.size(); |
| 612 |
for (int i = 0; i < numTransitions; ++i) { |
| 613 |
clone.addTransitionInternal((Transition) mTransitions.get(i).clone()); |
| 614 |
} |
| 615 |
return clone; |
| 616 |
} |
| 617 |
} |