diff options
Diffstat (limited to 'core/java/android/widget/EdgeEffect.java')
| -rw-r--r-- | core/java/android/widget/EdgeEffect.java | 501 |
1 files changed, 449 insertions, 52 deletions
diff --git a/core/java/android/widget/EdgeEffect.java b/core/java/android/widget/EdgeEffect.java index c10ffbee686a..c110ab956030 100644 --- a/core/java/android/widget/EdgeEffect.java +++ b/core/java/android/widget/EdgeEffect.java @@ -16,20 +16,33 @@ package android.widget; +import android.animation.ValueAnimator; import android.annotation.ColorInt; +import android.annotation.IntDef; +import android.annotation.NonNull; import android.annotation.Nullable; +import android.compat.Compatibility; +import android.compat.annotation.ChangeId; +import android.compat.annotation.EnabledSince; import android.compat.annotation.UnsupportedAppUsage; import android.content.Context; import android.content.res.TypedArray; import android.graphics.BlendMode; import android.graphics.Canvas; +import android.graphics.Matrix; import android.graphics.Paint; +import android.graphics.RecordingCanvas; import android.graphics.Rect; +import android.graphics.RenderNode; import android.os.Build; +import android.util.AttributeSet; import android.view.animation.AnimationUtils; import android.view.animation.DecelerateInterpolator; import android.view.animation.Interpolator; +import java.lang.annotation.Retention; +import java.lang.annotation.RetentionPolicy; + /** * This class performs the graphical effect used at the edges of scrollable widgets * when the user scrolls beyond the content bounds in 2D space. @@ -49,12 +62,91 @@ import android.view.animation.Interpolator; * {@link #draw(Canvas)} method.</p> */ public class EdgeEffect { + /** + * This sets the edge effect to use stretch instead of glow. + * + * @hide + */ + @ChangeId + @EnabledSince(targetSdkVersion = Build.VERSION_CODES.BASE) + public static final long USE_STRETCH_EDGE_EFFECT_BY_DEFAULT = 171228096L; /** * The default blend mode used by {@link EdgeEffect}. */ public static final BlendMode DEFAULT_BLEND_MODE = BlendMode.SRC_ATOP; + /** + * Completely disable edge effect + */ + private static final int TYPE_NONE = -1; + + /** + * Use a color edge glow for the edge effect. + */ + private static final int TYPE_GLOW = 0; + + /** + * Use a stretch for the edge effect. + */ + private static final int TYPE_STRETCH = 1; + + /** + * The velocity threshold before the spring animation is considered settled. + * The idea here is that velocity should be less than 0.1 pixel per second. + */ + private static final double VELOCITY_THRESHOLD = 0.01; + + /** + * The speed at which we should start linearly interpolating to the destination. + * When using a spring, as it gets closer to the destination, the speed drops off exponentially. + * Instead of landing very slowly, a better experience is achieved if the final + * destination is arrived at quicker. + */ + private static final float LINEAR_VELOCITY_TAKE_OVER = 200f; + + /** + * The value threshold before the spring animation is considered close enough to + * the destination to be settled. This should be around 0.01 pixel. + */ + private static final double VALUE_THRESHOLD = 0.001; + + /** + * The maximum distance at which we should start linearly interpolating to the destination. + * When using a spring, as it gets closer to the destination, the speed drops off exponentially. + * Instead of landing very slowly, a better experience is achieved if the final + * destination is arrived at quicker. + */ + private static final double LINEAR_DISTANCE_TAKE_OVER = 8.0; + + /** + * The natural frequency of the stretch spring. + */ + private static final double NATURAL_FREQUENCY = 24.657; + + /** + * The damping ratio of the stretch spring. + */ + private static final double DAMPING_RATIO = 0.98; + + /** + * The variation of the velocity for the stretch effect when it meets the bound. + * if value is > 1, it will accentuate the absorption of the movement. + */ + private static final float ON_ABSORB_VELOCITY_ADJUSTMENT = 13f; + + /** @hide */ + @IntDef({TYPE_NONE, TYPE_GLOW, TYPE_STRETCH}) + @Retention(RetentionPolicy.SOURCE) + public @interface EdgeEffectType { + } + + private static final float LINEAR_STRETCH_INTENSITY = 0.016f; + + private static final float EXP_STRETCH_INTENSITY = 0.016f; + + private static final float SCROLL_DIST_AFFECTED_BY_EXP_STRETCH = 0.33f; + @SuppressWarnings("UnusedDeclaration") private static final String TAG = "EdgeEffect"; @@ -89,6 +181,8 @@ public class EdgeEffect { private float mGlowAlpha; @UnsupportedAppUsage(maxTargetSdk = Build.VERSION_CODES.R, trackingBug = 170729553) private float mGlowScaleY; + private float mDistance; + private float mVelocity; // only for stretch animations private float mGlowAlphaStart; private float mGlowAlphaFinish; @@ -98,7 +192,7 @@ public class EdgeEffect { private long mStartTime; private float mDuration; - private final Interpolator mInterpolator; + private final Interpolator mInterpolator = new DecelerateInterpolator(); private static final int STATE_IDLE = 0; private static final int STATE_PULL = 1; @@ -115,6 +209,8 @@ public class EdgeEffect { private float mPullDistance; private final Rect mBounds = new Rect(); + private float mWidth; + private float mHeight; @UnsupportedAppUsage(maxTargetSdk = Build.VERSION_CODES.P, trackingBug = 123769450) private final Paint mPaint = new Paint(); private float mRadius; @@ -123,20 +219,49 @@ public class EdgeEffect { private float mTargetDisplacement = 0.5f; /** + * Current edge effect type, consumers should always query + * {@link #getCurrentEdgeEffectBehavior()} instead of this parameter + * directly in case animations have been disabled (ex. for accessibility reasons) + */ + private @EdgeEffectType int mEdgeEffectType = TYPE_GLOW; + private Matrix mTmpMatrix = null; + private float[] mTmpPoints = null; + + /** * Construct a new EdgeEffect with a theme appropriate for the provided context. * @param context Context used to provide theming and resource information for the EdgeEffect */ public EdgeEffect(Context context) { - mPaint.setAntiAlias(true); + this(context, null); + } + + /** + * Construct a new EdgeEffect with a theme appropriate for the provided context. + * @param context Context used to provide theming and resource information for the EdgeEffect + * @param attrs The attributes of the XML tag that is inflating the view + */ + public EdgeEffect(@NonNull Context context, @Nullable AttributeSet attrs) { final TypedArray a = context.obtainStyledAttributes( - com.android.internal.R.styleable.EdgeEffect); + attrs, com.android.internal.R.styleable.EdgeEffect); final int themeColor = a.getColor( com.android.internal.R.styleable.EdgeEffect_colorEdgeEffect, 0xff666666); + mEdgeEffectType = Compatibility.isChangeEnabled(USE_STRETCH_EDGE_EFFECT_BY_DEFAULT) + ? TYPE_STRETCH : TYPE_GLOW; a.recycle(); + + mPaint.setAntiAlias(true); mPaint.setColor((themeColor & 0xffffff) | 0x33000000); mPaint.setStyle(Paint.Style.FILL); mPaint.setBlendMode(DEFAULT_BLEND_MODE); - mInterpolator = new DecelerateInterpolator(); + } + + @EdgeEffectType + private int getCurrentEdgeEffectBehavior() { + if (!ValueAnimator.areAnimatorsEnabled()) { + return TYPE_NONE; + } else { + return mEdgeEffectType; + } } /** @@ -157,6 +282,9 @@ public class EdgeEffect { mBaseGlowScale = h > 0 ? Math.min(oh / h, 1.f) : 1.f; mBounds.set(mBounds.left, mBounds.top, width, (int) Math.min(height, h)); + + mWidth = width; + mHeight = height; } /** @@ -176,6 +304,8 @@ public class EdgeEffect { */ public void finish() { mState = STATE_IDLE; + mDistance = 0; + mVelocity = 0; } /** @@ -209,13 +339,25 @@ public class EdgeEffect { * Values may be from 0-1. */ public void onPull(float deltaDistance, float displacement) { + int edgeEffectBehavior = getCurrentEdgeEffectBehavior(); + if (edgeEffectBehavior == TYPE_NONE) { + finish(); + return; + } final long now = AnimationUtils.currentAnimationTimeMillis(); mTargetDisplacement = displacement; - if (mState == STATE_PULL_DECAY && now - mStartTime < mDuration) { + if (mState == STATE_PULL_DECAY && now - mStartTime < mDuration + && edgeEffectBehavior == TYPE_GLOW) { return; } if (mState != STATE_PULL) { - mGlowScaleY = Math.max(PULL_GLOW_BEGIN, mGlowScaleY); + if (edgeEffectBehavior == TYPE_STRETCH) { + // Restore the mPullDistance to the fraction it is currently showing -- we want + // to "catch" the current stretch value. + mPullDistance = mDistance; + } else { + mGlowScaleY = Math.max(PULL_GLOW_BEGIN, mGlowScaleY); + } } mState = STATE_PULL; @@ -223,14 +365,21 @@ public class EdgeEffect { mDuration = PULL_TIME; mPullDistance += deltaDistance; - - final float absdd = Math.abs(deltaDistance); - mGlowAlpha = mGlowAlphaStart = Math.min(MAX_ALPHA, - mGlowAlpha + (absdd * PULL_DISTANCE_ALPHA_GLOW_FACTOR)); + if (edgeEffectBehavior == TYPE_STRETCH) { + // Don't allow stretch beyond 1 + mPullDistance = Math.min(1f, mPullDistance); + } + mDistance = Math.max(0f, mPullDistance); + mVelocity = 0; if (mPullDistance == 0) { mGlowScaleY = mGlowScaleYStart = 0; + mGlowAlpha = mGlowAlphaStart = 0; } else { + final float absdd = Math.abs(deltaDistance); + mGlowAlpha = mGlowAlphaStart = Math.min(MAX_ALPHA, + mGlowAlpha + (absdd * PULL_DISTANCE_ALPHA_GLOW_FACTOR)); + final float scale = (float) (Math.max(0, 1 - 1 / Math.sqrt(Math.abs(mPullDistance) * mBounds.height()) - 0.3d) / 0.7d); @@ -239,6 +388,72 @@ public class EdgeEffect { mGlowAlphaFinish = mGlowAlpha; mGlowScaleYFinish = mGlowScaleY; + if (edgeEffectBehavior == TYPE_STRETCH && mDistance == 0) { + mState = STATE_IDLE; + } + } + + /** + * A view should call this when content is pulled away from an edge by the user. + * This will update the state of the current visual effect and its associated animation. + * The host view should always {@link android.view.View#invalidate()} after this + * and draw the results accordingly. This works similarly to {@link #onPull(float, float)}, + * but returns the amount of <code>deltaDistance</code> that has been consumed. If the + * {@link #getDistance()} is currently 0 and <code>deltaDistance</code> is negative, this + * function will return 0 and the drawn value will remain unchanged. + * + * This method can be used to reverse the effect from a pull or absorb and partially consume + * some of a motion: + * + * <pre class="prettyprint"> + * if (deltaY < 0) { + * float consumed = edgeEffect.onPullDistance(deltaY / getHeight(), x / getWidth()); + * deltaY -= consumed * getHeight(); + * if (edgeEffect.getDistance() == 0f) edgeEffect.onRelease(); + * } + * </pre> + * + * @param deltaDistance Change in distance since the last call. Values may be 0 (no change) to + * 1.f (full length of the view) or negative values to express change + * back toward the edge reached to initiate the effect. + * @param displacement The displacement from the starting side of the effect of the point + * initiating the pull. In the case of touch this is the finger position. + * Values may be from 0-1. + * @return The amount of <code>deltaDistance</code> that was consumed, a number between + * 0 and <code>deltaDistance</code>. + */ + public float onPullDistance(float deltaDistance, float displacement) { + int edgeEffectBehavior = getCurrentEdgeEffectBehavior(); + if (edgeEffectBehavior == TYPE_NONE) { + return 0f; + } + float finalDistance = Math.max(0f, deltaDistance + mDistance); + float delta = finalDistance - mDistance; + if (delta == 0f && mDistance == 0f) { + return 0f; // No pull, don't do anything. + } + + if (mState != STATE_PULL && mState != STATE_PULL_DECAY && edgeEffectBehavior == TYPE_GLOW) { + // Catch the edge glow in the middle of an animation. + mPullDistance = mDistance; + mState = STATE_PULL; + } + onPull(delta, displacement); + return delta; + } + + /** + * Returns the pull distance needed to be released to remove the showing effect. + * It is determined by the {@link #onPull(float, float)} <code>deltaDistance</code> and + * any animating values, including from {@link #onAbsorb(int)} and {@link #onRelease()}. + * + * This can be used in conjunction with {@link #onPullDistance(float, float)} to + * release the currently showing effect. + * + * @return The pull distance that must be released to remove the showing effect. + */ + public float getDistance() { + return mDistance; } /** @@ -260,6 +475,7 @@ public class EdgeEffect { mGlowAlphaFinish = 0.f; mGlowScaleYFinish = 0.f; + mVelocity = 0.f; mStartTime = AnimationUtils.currentAnimationTimeMillis(); mDuration = RECEDE_TIME; @@ -276,27 +492,38 @@ public class EdgeEffect { * @param velocity Velocity at impact in pixels per second. */ public void onAbsorb(int velocity) { - mState = STATE_ABSORB; - velocity = Math.min(Math.max(MIN_VELOCITY, Math.abs(velocity)), MAX_VELOCITY); - - mStartTime = AnimationUtils.currentAnimationTimeMillis(); - mDuration = 0.15f + (velocity * 0.02f); - - // The glow depends more on the velocity, and therefore starts out - // nearly invisible. - mGlowAlphaStart = GLOW_ALPHA_START; - mGlowScaleYStart = Math.max(mGlowScaleY, 0.f); - - - // Growth for the size of the glow should be quadratic to properly - // respond - // to a user's scrolling speed. The faster the scrolling speed, the more - // intense the effect should be for both the size and the saturation. - mGlowScaleYFinish = Math.min(0.025f + (velocity * (velocity / 100) * 0.00015f) / 2, 1.f); - // Alpha should change for the glow as well as size. - mGlowAlphaFinish = Math.max( - mGlowAlphaStart, Math.min(velocity * VELOCITY_GLOW_FACTOR * .00001f, MAX_ALPHA)); - mTargetDisplacement = 0.5f; + int edgeEffectBehavior = getCurrentEdgeEffectBehavior(); + if (edgeEffectBehavior == TYPE_STRETCH) { + mState = STATE_RECEDE; + mVelocity = velocity * ON_ABSORB_VELOCITY_ADJUSTMENT; + mStartTime = AnimationUtils.currentAnimationTimeMillis(); + } else if (edgeEffectBehavior == TYPE_GLOW) { + mState = STATE_ABSORB; + mVelocity = 0; + velocity = Math.min(Math.max(MIN_VELOCITY, Math.abs(velocity)), MAX_VELOCITY); + + mStartTime = AnimationUtils.currentAnimationTimeMillis(); + mDuration = 0.15f + (velocity * 0.02f); + + // The glow depends more on the velocity, and therefore starts out + // nearly invisible. + mGlowAlphaStart = GLOW_ALPHA_START; + mGlowScaleYStart = Math.max(mGlowScaleY, 0.f); + + // Growth for the size of the glow should be quadratic to properly + // respond + // to a user's scrolling speed. The faster the scrolling speed, the more + // intense the effect should be for both the size and the saturation. + mGlowScaleYFinish = Math.min(0.025f + (velocity * (velocity / 100) * 0.00015f) / 2, + 1.f); + // Alpha should change for the glow as well as size. + mGlowAlphaFinish = Math.max( + mGlowAlphaStart, + Math.min(velocity * VELOCITY_GLOW_FACTOR * .00001f, MAX_ALPHA)); + mTargetDisplacement = 0.5f; + } else { + finish(); + } } /** @@ -333,7 +560,6 @@ public class EdgeEffect { return mPaint.getColor(); } - /** * Returns the blend mode. A blend mode defines how source pixels * (generated by a drawing command) are composited with the destination pixels @@ -351,33 +577,98 @@ public class EdgeEffect { * Draw into the provided canvas. Assumes that the canvas has been rotated * accordingly and the size has been set. The effect will be drawn the full * width of X=0 to X=width, beginning from Y=0 and extending to some factor < - * 1.f of height. + * 1.f of height. The effect will only be visible on a + * hardware canvas, e.g. {@link RenderNode#beginRecording()}. * * @param canvas Canvas to draw into * @return true if drawing should continue beyond this frame to continue the * animation */ public boolean draw(Canvas canvas) { - update(); - - final int count = canvas.save(); - - final float centerX = mBounds.centerX(); - final float centerY = mBounds.height() - mRadius; - - canvas.scale(1.f, Math.min(mGlowScaleY, 1.f) * mBaseGlowScale, centerX, 0); - - final float displacement = Math.max(0, Math.min(mDisplacement, 1.f)) - 0.5f; - float translateX = mBounds.width() * displacement / 2; - - canvas.clipRect(mBounds); - canvas.translate(translateX, 0); - mPaint.setAlpha((int) (0xff * mGlowAlpha)); - canvas.drawCircle(centerX, centerY, mRadius, mPaint); - canvas.restoreToCount(count); + int edgeEffectBehavior = getCurrentEdgeEffectBehavior(); + if (edgeEffectBehavior == TYPE_GLOW) { + update(); + final int count = canvas.save(); + + final float centerX = mBounds.centerX(); + final float centerY = mBounds.height() - mRadius; + + canvas.scale(1.f, Math.min(mGlowScaleY, 1.f) * mBaseGlowScale, centerX, 0); + + final float displacement = Math.max(0, Math.min(mDisplacement, 1.f)) - 0.5f; + float translateX = mBounds.width() * displacement / 2; + + canvas.clipRect(mBounds); + canvas.translate(translateX, 0); + mPaint.setAlpha((int) (0xff * mGlowAlpha)); + canvas.drawCircle(centerX, centerY, mRadius, mPaint); + canvas.restoreToCount(count); + } else if (edgeEffectBehavior == TYPE_STRETCH && canvas instanceof RecordingCanvas) { + if (mState == STATE_RECEDE) { + updateSpring(); + } + if (mDistance != 0f) { + RecordingCanvas recordingCanvas = (RecordingCanvas) canvas; + if (mTmpMatrix == null) { + mTmpMatrix = new Matrix(); + mTmpPoints = new float[12]; + } + //noinspection deprecation + recordingCanvas.getMatrix(mTmpMatrix); + + mTmpPoints[0] = 0; + mTmpPoints[1] = 0; // top-left + mTmpPoints[2] = mWidth; + mTmpPoints[3] = 0; // top-right + mTmpPoints[4] = mWidth; + mTmpPoints[5] = mHeight; // bottom-right + mTmpPoints[6] = 0; + mTmpPoints[7] = mHeight; // bottom-left + mTmpPoints[8] = mWidth * mDisplacement; + mTmpPoints[9] = 0; // drag start point + mTmpPoints[10] = mWidth * mDisplacement; + mTmpPoints[11] = mHeight * mDistance; // drag point + mTmpMatrix.mapPoints(mTmpPoints); + + RenderNode renderNode = recordingCanvas.mNode; + + float left = renderNode.getLeft() + + min(mTmpPoints[0], mTmpPoints[2], mTmpPoints[4], mTmpPoints[6]); + float top = renderNode.getTop() + + min(mTmpPoints[1], mTmpPoints[3], mTmpPoints[5], mTmpPoints[7]); + float right = renderNode.getLeft() + + max(mTmpPoints[0], mTmpPoints[2], mTmpPoints[4], mTmpPoints[6]); + float bottom = renderNode.getTop() + + max(mTmpPoints[1], mTmpPoints[3], mTmpPoints[5], mTmpPoints[7]); + // assume rotations of increments of 90 degrees + float x = mTmpPoints[10] - mTmpPoints[8]; + float width = right - left; + float vecX = dampStretchVector(Math.max(-1f, Math.min(1f, x / width))); + + float y = mTmpPoints[11] - mTmpPoints[9]; + float height = bottom - top; + float vecY = dampStretchVector(Math.max(-1f, Math.min(1f, y / height))); + + boolean hasValidVectors = Float.isFinite(vecX) && Float.isFinite(vecY); + if (right > left && bottom > top && mWidth > 0 && mHeight > 0 && hasValidVectors) { + renderNode.stretch( + vecX, // horizontal stretch intensity + vecY, // vertical stretch intensity + mWidth, // max horizontal stretch in pixels + mHeight // max vertical stretch in pixels + ); + } + } + } else { + // Animations have been disabled or this is TYPE_STRETCH and drawing into a Canvas + // that isn't a Recording Canvas, so no effect can be shown. Just end the effect. + mState = STATE_IDLE; + mDistance = 0; + mVelocity = 0; + } boolean oneLastFrame = false; - if (mState == STATE_RECEDE && mGlowScaleY == 0) { + if (mState == STATE_RECEDE && mDistance == 0 && mVelocity == 0) { mState = STATE_IDLE; oneLastFrame = true; } @@ -385,13 +676,25 @@ public class EdgeEffect { return mState != STATE_IDLE || oneLastFrame; } + private float min(float f1, float f2, float f3, float f4) { + float min = Math.min(f1, f2); + min = Math.min(min, f3); + return Math.min(min, f4); + } + + private float max(float f1, float f2, float f3, float f4) { + float max = Math.max(f1, f2); + max = Math.max(max, f3); + return Math.max(max, f4); + } + /** * Return the maximum height that the edge effect will be drawn at given the original * {@link #setSize(int, int) input size}. * @return The maximum height of the edge effect */ public int getMaxHeight() { - return (int) (mBounds.height() * MAX_GLOW_SCALE + 0.5f); + return (int) mHeight; } private void update() { @@ -402,6 +705,9 @@ public class EdgeEffect { mGlowAlpha = mGlowAlphaStart + (mGlowAlphaFinish - mGlowAlphaStart) * interp; mGlowScaleY = mGlowScaleYStart + (mGlowScaleYFinish - mGlowScaleYStart) * interp; + if (mState != STATE_PULL) { + mDistance = calculateDistanceFromGlowValues(mGlowScaleY, mGlowAlpha); + } mDisplacement = (mDisplacement + mTargetDisplacement) / 2; if (t >= 1.f - EPSILON) { @@ -439,4 +745,95 @@ public class EdgeEffect { } } } + + private void updateSpring() { + final long time = AnimationUtils.currentAnimationTimeMillis(); + final float deltaT = (time - mStartTime) / 1000f; // Convert from millis to seconds + if (deltaT < 0.001f) { + return; // Must have at least 1 ms difference + } + mStartTime = time; + + if (Math.abs(mVelocity) <= LINEAR_VELOCITY_TAKE_OVER + && Math.abs(mDistance * mHeight) < LINEAR_DISTANCE_TAKE_OVER + && Math.signum(mVelocity) == -Math.signum(mDistance) + ) { + // This is close. The spring will slowly reach the destination. Instead, we + // will interpolate linearly so that it arrives at its destination quicker. + mVelocity = Math.signum(mVelocity) * LINEAR_VELOCITY_TAKE_OVER; + + float targetDistance = mDistance + (mVelocity * deltaT / mHeight); + if (Math.signum(targetDistance) != Math.signum(mDistance)) { + // We have arrived + mDistance = 0; + mVelocity = 0; + } else { + mDistance = targetDistance; + } + return; + } + final double mDampedFreq = NATURAL_FREQUENCY * Math.sqrt(1 - DAMPING_RATIO * DAMPING_RATIO); + + // We're always underdamped, so we can use only those equations: + double cosCoeff = mDistance * mHeight; + double sinCoeff = (1 / mDampedFreq) * (DAMPING_RATIO * NATURAL_FREQUENCY + * mDistance * mHeight + mVelocity); + double distance = Math.pow(Math.E, -DAMPING_RATIO * NATURAL_FREQUENCY * deltaT) + * (cosCoeff * Math.cos(mDampedFreq * deltaT) + + sinCoeff * Math.sin(mDampedFreq * deltaT)); + double velocity = distance * (-NATURAL_FREQUENCY) * DAMPING_RATIO + + Math.pow(Math.E, -DAMPING_RATIO * NATURAL_FREQUENCY * deltaT) + * (-mDampedFreq * cosCoeff * Math.sin(mDampedFreq * deltaT) + + mDampedFreq * sinCoeff * Math.cos(mDampedFreq * deltaT)); + mDistance = (float) distance / mHeight; + mVelocity = (float) velocity; + if (mDistance > 1f) { + mDistance = 1f; + mVelocity = 0f; + } + if (isAtEquilibrium()) { + mDistance = 0; + mVelocity = 0; + } + } + + /** + * @return The estimated pull distance as calculated from mGlowScaleY. + */ + private float calculateDistanceFromGlowValues(float scale, float alpha) { + if (scale >= 1f) { + // It should asymptotically approach 1, but not reach there. + // Here, we're just choosing a value that is large. + return 1f; + } + if (scale > 0f) { + float v = 1f / 0.7f / (mGlowScaleY - 1f); + return v * v / mBounds.height(); + } + return alpha / PULL_DISTANCE_ALPHA_GLOW_FACTOR; + } + + /** + * @return true if the spring used for calculating the stretch animation is + * considered at rest or false if it is still animating. + */ + private boolean isAtEquilibrium() { + double displacement = mDistance * mHeight; // in pixels + double velocity = mVelocity; + + // Don't allow displacement to drop below 0. We don't want it stretching the opposite + // direction if it is flung that way. We also want to stop the animation as soon as + // it gets very close to its destination. + return displacement < 0 || (Math.abs(velocity) < VELOCITY_THRESHOLD + && displacement < VALUE_THRESHOLD); + } + + private float dampStretchVector(float normalizedVec) { + float sign = normalizedVec > 0 ? 1f : -1f; + float overscroll = Math.abs(normalizedVec); + float linearIntensity = LINEAR_STRETCH_INTENSITY * overscroll; + double scalar = Math.E / SCROLL_DIST_AFFECTED_BY_EXP_STRETCH; + double expIntensity = EXP_STRETCH_INTENSITY * (1 - Math.exp(-overscroll * scalar)); + return sign * (float) (linearIntensity + expIntensity); + } } |
