| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
package android.window; |
| 18 |
|
| 19 |
import android.util.FloatProperty; |
| 20 |
|
| 21 |
import com.android.internal.dynamicanimation.animation.SpringAnimation; |
| 22 |
import com.android.internal.dynamicanimation.animation.SpringForce; |
| 23 |
|
| 24 |
|
| 25 |
|
| 26 |
|
| 27 |
|
| 28 |
|
| 29 |
|
| 30 |
|
| 31 |
|
| 32 |
|
| 33 |
public class BackProgressAnimator { |
| 34 |
|
| 35 |
|
| 36 |
|
| 37 |
|
| 38 |
|
| 39 |
private static final float SCALE_FACTOR = 100f; |
| 40 |
private final SpringAnimation mSpring; |
| 41 |
private ProgressCallback mCallback; |
| 42 |
private float mProgress = 0; |
| 43 |
private BackEvent mLastBackEvent; |
| 44 |
private boolean mStarted = false; |
| 45 |
|
| 46 |
private void setProgress(float progress) { |
| 47 |
mProgress = progress; |
| 48 |
} |
| 49 |
|
| 50 |
private float getProgress() { |
| 51 |
return mProgress; |
| 52 |
} |
| 53 |
|
| 54 |
private static final FloatProperty<BackProgressAnimator> PROGRESS_PROP = |
| 55 |
new FloatProperty<BackProgressAnimator>("progress") { |
| 56 |
@Override |
| 57 |
public void setValue(BackProgressAnimator animator, float value) { |
| 58 |
animator.setProgress(value); |
| 59 |
animator.updateProgressValue(value); |
| 60 |
} |
| 61 |
|
| 62 |
@Override |
| 63 |
public Float get(BackProgressAnimator object) { |
| 64 |
return object.getProgress(); |
| 65 |
} |
| 66 |
}; |
| 67 |
|
| 68 |
|
| 69 |
|
| 70 |
public interface ProgressCallback { |
| 71 |
|
| 72 |
void onProgressUpdate(BackEvent event); |
| 73 |
} |
| 74 |
|
| 75 |
public BackProgressAnimator() { |
| 76 |
mSpring = new SpringAnimation(this, PROGRESS_PROP); |
| 77 |
mSpring.setSpring(new SpringForce() |
| 78 |
.setStiffness(SpringForce.STIFFNESS_MEDIUM) |
| 79 |
.setDampingRatio(SpringForce.DAMPING_RATIO_NO_BOUNCY)); |
| 80 |
} |
| 81 |
|
| 82 |
|
| 83 |
|
| 84 |
|
| 85 |
|
| 86 |
|
| 87 |
public void onBackProgressed(BackEvent event) { |
| 88 |
if (!mStarted) { |
| 89 |
return; |
| 90 |
} |
| 91 |
mLastBackEvent = event; |
| 92 |
mSpring.animateToFinalPosition(event.getProgress() * SCALE_FACTOR); |
| 93 |
} |
| 94 |
|
| 95 |
|
| 96 |
|
| 97 |
|
| 98 |
|
| 99 |
|
| 100 |
|
| 101 |
|
| 102 |
public void onBackStarted(BackEvent event, ProgressCallback callback) { |
| 103 |
reset(); |
| 104 |
mLastBackEvent = event; |
| 105 |
mCallback = callback; |
| 106 |
mStarted = true; |
| 107 |
} |
| 108 |
|
| 109 |
|
| 110 |
|
| 111 |
|
| 112 |
public void reset() { |
| 113 |
mSpring.animateToFinalPosition(0); |
| 114 |
if (mSpring.canSkipToEnd()) { |
| 115 |
mSpring.skipToEnd(); |
| 116 |
} else { |
| 117 |
|
| 118 |
mSpring.cancel(); |
| 119 |
} |
| 120 |
mStarted = false; |
| 121 |
mLastBackEvent = null; |
| 122 |
mCallback = null; |
| 123 |
mProgress = 0; |
| 124 |
} |
| 125 |
|
| 126 |
private void updateProgressValue(float progress) { |
| 127 |
if (mLastBackEvent == null || mCallback == null || !mStarted) { |
| 128 |
return; |
| 129 |
} |
| 130 |
mCallback.onProgressUpdate( |
| 131 |
new BackEvent(mLastBackEvent.getTouchX(), mLastBackEvent.getTouchY(), |
| 132 |
progress / SCALE_FACTOR, mLastBackEvent.getSwipeEdge(), |
| 133 |
mLastBackEvent.getDepartingAnimationTarget())); |
| 134 |
} |
| 135 |
|
| 136 |
} |