| 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.transition.Slide.GravityFlag; |
| 20 |
import android.view.Gravity; |
| 21 |
import android.view.View; |
| 22 |
import android.view.ViewGroup; |
| 23 |
|
| 24 |
|
| 25 |
|
| 26 |
|
| 27 |
|
| 28 |
|
| 29 |
|
| 30 |
|
| 31 |
|
| 32 |
public class SidePropagation extends VisibilityPropagation { |
| 33 |
private static final String TAG = "SlidePropagation"; |
| 34 |
|
| 35 |
private float mPropagationSpeed = 3.0f; |
| 36 |
private int mSide = Gravity.BOTTOM; |
| 37 |
|
| 38 |
|
| 39 |
|
| 40 |
|
| 41 |
|
| 42 |
|
| 43 |
|
| 44 |
|
| 45 |
|
| 46 |
|
| 47 |
|
| 48 |
|
| 49 |
public void setSide(@GravityFlag int side) { |
| 50 |
mSide = side; |
| 51 |
} |
| 52 |
|
| 53 |
|
| 54 |
|
| 55 |
|
| 56 |
|
| 57 |
|
| 58 |
|
| 59 |
|
| 60 |
|
| 61 |
|
| 62 |
|
| 63 |
|
| 64 |
|
| 65 |
public void setPropagationSpeed(float propagationSpeed) { |
| 66 |
if (propagationSpeed == 0) { |
| 67 |
throw new IllegalArgumentException("propagationSpeed may not be 0"); |
| 68 |
} |
| 69 |
mPropagationSpeed = propagationSpeed; |
| 70 |
} |
| 71 |
|
| 72 |
@Override |
| 73 |
public long getStartDelay(ViewGroup sceneRoot, Transition transition, |
| 74 |
TransitionValues startValues, TransitionValues endValues) { |
| 75 |
if (startValues == null && endValues == null) { |
| 76 |
return 0; |
| 77 |
} |
| 78 |
int directionMultiplier = 1; |
| 79 |
Rect epicenter = transition.getEpicenter(); |
| 80 |
TransitionValues positionValues; |
| 81 |
if (endValues == null || getViewVisibility(startValues) == View.VISIBLE) { |
| 82 |
positionValues = startValues; |
| 83 |
directionMultiplier = -1; |
| 84 |
} else { |
| 85 |
positionValues = endValues; |
| 86 |
} |
| 87 |
|
| 88 |
int viewCenterX = getViewX(positionValues); |
| 89 |
int viewCenterY = getViewY(positionValues); |
| 90 |
|
| 91 |
int[] loc = new int[2]; |
| 92 |
sceneRoot.getLocationOnScreen(loc); |
| 93 |
int left = loc[0] + Math.round(sceneRoot.getTranslationX()); |
| 94 |
int top = loc[1] + Math.round(sceneRoot.getTranslationY()); |
| 95 |
int right = left + sceneRoot.getWidth(); |
| 96 |
int bottom = top + sceneRoot.getHeight(); |
| 97 |
|
| 98 |
int epicenterX; |
| 99 |
int epicenterY; |
| 100 |
if (epicenter != null) { |
| 101 |
epicenterX = epicenter.centerX(); |
| 102 |
epicenterY = epicenter.centerY(); |
| 103 |
} else { |
| 104 |
epicenterX = (left + right) / 2; |
| 105 |
epicenterY = (top + bottom) / 2; |
| 106 |
} |
| 107 |
|
| 108 |
float distance = distance(sceneRoot, viewCenterX, viewCenterY, epicenterX, epicenterY, |
| 109 |
left, top, right, bottom); |
| 110 |
float maxDistance = getMaxDistance(sceneRoot); |
| 111 |
float distanceFraction = distance/maxDistance; |
| 112 |
|
| 113 |
long duration = transition.getDuration(); |
| 114 |
if (duration < 0) { |
| 115 |
duration = 300; |
| 116 |
} |
| 117 |
|
| 118 |
return Math.round(duration * directionMultiplier / mPropagationSpeed * distanceFraction); |
| 119 |
} |
| 120 |
|
| 121 |
private int distance(View sceneRoot, int viewX, int viewY, int epicenterX, int epicenterY, |
| 122 |
int left, int top, int right, int bottom) { |
| 123 |
final int side; |
| 124 |
if (mSide == Gravity.START) { |
| 125 |
final boolean isRtl = sceneRoot.getLayoutDirection() == View.LAYOUT_DIRECTION_RTL; |
| 126 |
side = isRtl ? Gravity.RIGHT : Gravity.LEFT; |
| 127 |
} else if (mSide == Gravity.END) { |
| 128 |
final boolean isRtl = sceneRoot.getLayoutDirection() == View.LAYOUT_DIRECTION_RTL; |
| 129 |
side = isRtl ? Gravity.LEFT : Gravity.RIGHT; |
| 130 |
} else { |
| 131 |
side = mSide; |
| 132 |
} |
| 133 |
int distance = 0; |
| 134 |
switch (side) { |
| 135 |
case Gravity.LEFT: |
| 136 |
distance = right - viewX + Math.abs(epicenterY - viewY); |
| 137 |
break; |
| 138 |
case Gravity.TOP: |
| 139 |
distance = bottom - viewY + Math.abs(epicenterX - viewX); |
| 140 |
break; |
| 141 |
case Gravity.RIGHT: |
| 142 |
distance = viewX - left + Math.abs(epicenterY - viewY); |
| 143 |
break; |
| 144 |
case Gravity.BOTTOM: |
| 145 |
distance = viewY - top + Math.abs(epicenterX - viewX); |
| 146 |
break; |
| 147 |
} |
| 148 |
return distance; |
| 149 |
} |
| 150 |
|
| 151 |
private int getMaxDistance(ViewGroup sceneRoot) { |
| 152 |
switch (mSide) { |
| 153 |
case Gravity.LEFT: |
| 154 |
case Gravity.RIGHT: |
| 155 |
case Gravity.START: |
| 156 |
case Gravity.END: |
| 157 |
return sceneRoot.getWidth(); |
| 158 |
default: |
| 159 |
return sceneRoot.getHeight(); |
| 160 |
} |
| 161 |
} |
| 162 |
} |