| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
package android.transition; |
| 17 |
|
| 18 |
import android.graphics.Rect; |
| 19 |
import android.view.View; |
| 20 |
import android.view.ViewGroup; |
| 21 |
|
| 22 |
|
| 23 |
|
| 24 |
|
| 25 |
|
| 26 |
|
| 27 |
|
| 28 |
|
| 29 |
|
| 30 |
|
| 31 |
|
| 32 |
public class CircularPropagation extends VisibilityPropagation { |
| 33 |
private static final String TAG = "CircularPropagation"; |
| 34 |
|
| 35 |
private float mPropagationSpeed = 3.0f; |
| 36 |
|
| 37 |
|
| 38 |
|
| 39 |
|
| 40 |
|
| 41 |
|
| 42 |
|
| 43 |
|
| 44 |
|
| 45 |
|
| 46 |
|
| 47 |
|
| 48 |
|
| 49 |
public void setPropagationSpeed(float propagationSpeed) { |
| 50 |
if (propagationSpeed == 0) { |
| 51 |
throw new IllegalArgumentException("propagationSpeed may not be 0"); |
| 52 |
} |
| 53 |
mPropagationSpeed = propagationSpeed; |
| 54 |
} |
| 55 |
|
| 56 |
@Override |
| 57 |
public long getStartDelay(ViewGroup sceneRoot, Transition transition, |
| 58 |
TransitionValues startValues, TransitionValues endValues) { |
| 59 |
if (startValues == null && endValues == null) { |
| 60 |
return 0; |
| 61 |
} |
| 62 |
int directionMultiplier = 1; |
| 63 |
TransitionValues positionValues; |
| 64 |
if (endValues == null || getViewVisibility(startValues) == View.VISIBLE) { |
| 65 |
positionValues = startValues; |
| 66 |
directionMultiplier = -1; |
| 67 |
} else { |
| 68 |
positionValues = endValues; |
| 69 |
} |
| 70 |
|
| 71 |
int viewCenterX = getViewX(positionValues); |
| 72 |
int viewCenterY = getViewY(positionValues); |
| 73 |
|
| 74 |
Rect epicenter = transition.getEpicenter(); |
| 75 |
int epicenterX; |
| 76 |
int epicenterY; |
| 77 |
if (epicenter != null) { |
| 78 |
epicenterX = epicenter.centerX(); |
| 79 |
epicenterY = epicenter.centerY(); |
| 80 |
} else { |
| 81 |
int[] loc = new int[2]; |
| 82 |
sceneRoot.getLocationOnScreen(loc); |
| 83 |
epicenterX = Math.round(loc[0] + (sceneRoot.getWidth() / 2) |
| 84 |
+ sceneRoot.getTranslationX()); |
| 85 |
epicenterY = Math.round(loc[1] + (sceneRoot.getHeight() / 2) |
| 86 |
+ sceneRoot.getTranslationY()); |
| 87 |
} |
| 88 |
double distance = distance(viewCenterX, viewCenterY, epicenterX, epicenterY); |
| 89 |
double maxDistance = distance(0, 0, sceneRoot.getWidth(), sceneRoot.getHeight()); |
| 90 |
double distanceFraction = distance/maxDistance; |
| 91 |
|
| 92 |
long duration = transition.getDuration(); |
| 93 |
if (duration < 0) { |
| 94 |
duration = 300; |
| 95 |
} |
| 96 |
|
| 97 |
return Math.round(duration * directionMultiplier / mPropagationSpeed * distanceFraction); |
| 98 |
} |
| 99 |
|
| 100 |
private static double distance(float x1, float y1, float x2, float y2) { |
| 101 |
double x = x2 - x1; |
| 102 |
double y = y2 - y1; |
| 103 |
return Math.hypot(x, y); |
| 104 |
} |
| 105 |
} |