Methods Summary |
---|
public void | abortAnimation()Stops the animation. Contrary to {@link #forceFinished(boolean)},
aborting the animating cause the scroller to move to the final x and y
position
mCurrX = mFinalX;
mCurrY = mFinalY;
mFinished = true;
|
private float | computeDeceleration(float friction)
return SensorManager.GRAVITY_EARTH // g (m/s^2)
* 39.37f // inch/meter
* mPpi // pixels per inch
* friction;
|
public boolean | computeScrollOffset()Call this when you want to know the new location. If it returns true,
the animation is not yet finished.
if (mFinished) {
return false;
}
int timePassed = (int)(AnimationUtils.currentAnimationTimeMillis() - mStartTime);
if (timePassed < mDuration) {
switch (mMode) {
case SCROLL_MODE:
final float x = mInterpolator.getInterpolation(timePassed * mDurationReciprocal);
mCurrX = mStartX + Math.round(x * mDeltaX);
mCurrY = mStartY + Math.round(x * mDeltaY);
break;
case FLING_MODE:
final float t = (float) timePassed / mDuration;
final int index = (int) (NB_SAMPLES * t);
float distanceCoef = 1.f;
float velocityCoef = 0.f;
if (index < NB_SAMPLES) {
final float t_inf = (float) index / NB_SAMPLES;
final float t_sup = (float) (index + 1) / NB_SAMPLES;
final float d_inf = SPLINE_POSITION[index];
final float d_sup = SPLINE_POSITION[index + 1];
velocityCoef = (d_sup - d_inf) / (t_sup - t_inf);
distanceCoef = d_inf + (t - t_inf) * velocityCoef;
}
mCurrVelocity = velocityCoef * mDistance / mDuration * 1000.0f;
mCurrX = mStartX + Math.round(distanceCoef * (mFinalX - mStartX));
// Pin to mMinX <= mCurrX <= mMaxX
mCurrX = Math.min(mCurrX, mMaxX);
mCurrX = Math.max(mCurrX, mMinX);
mCurrY = mStartY + Math.round(distanceCoef * (mFinalY - mStartY));
// Pin to mMinY <= mCurrY <= mMaxY
mCurrY = Math.min(mCurrY, mMaxY);
mCurrY = Math.max(mCurrY, mMinY);
if (mCurrX == mFinalX && mCurrY == mFinalY) {
mFinished = true;
}
break;
}
}
else {
mCurrX = mFinalX;
mCurrY = mFinalY;
mFinished = true;
}
return true;
|
public void | extendDuration(int extend)Extend the scroll animation. This allows a running animation to scroll
further and longer, when used with {@link #setFinalX(int)} or {@link #setFinalY(int)}.
int passed = timePassed();
mDuration = passed + extend;
mDurationReciprocal = 1.0f / mDuration;
mFinished = false;
|
public void | fling(int startX, int startY, int velocityX, int velocityY, int minX, int maxX, int minY, int maxY)Start scrolling based on a fling gesture. The distance travelled will
depend on the initial velocity of the fling.
// Continue a scroll or fling in progress
if (mFlywheel && !mFinished) {
float oldVel = getCurrVelocity();
float dx = (float) (mFinalX - mStartX);
float dy = (float) (mFinalY - mStartY);
float hyp = FloatMath.sqrt(dx * dx + dy * dy);
float ndx = dx / hyp;
float ndy = dy / hyp;
float oldVelocityX = ndx * oldVel;
float oldVelocityY = ndy * oldVel;
if (Math.signum(velocityX) == Math.signum(oldVelocityX) &&
Math.signum(velocityY) == Math.signum(oldVelocityY)) {
velocityX += oldVelocityX;
velocityY += oldVelocityY;
}
}
mMode = FLING_MODE;
mFinished = false;
float velocity = FloatMath.sqrt(velocityX * velocityX + velocityY * velocityY);
mVelocity = velocity;
mDuration = getSplineFlingDuration(velocity);
mStartTime = AnimationUtils.currentAnimationTimeMillis();
mStartX = startX;
mStartY = startY;
float coeffX = velocity == 0 ? 1.0f : velocityX / velocity;
float coeffY = velocity == 0 ? 1.0f : velocityY / velocity;
double totalDistance = getSplineFlingDistance(velocity);
mDistance = (int) (totalDistance * Math.signum(velocity));
mMinX = minX;
mMaxX = maxX;
mMinY = minY;
mMaxY = maxY;
mFinalX = startX + (int) Math.round(totalDistance * coeffX);
// Pin to mMinX <= mFinalX <= mMaxX
mFinalX = Math.min(mFinalX, mMaxX);
mFinalX = Math.max(mFinalX, mMinX);
mFinalY = startY + (int) Math.round(totalDistance * coeffY);
// Pin to mMinY <= mFinalY <= mMaxY
mFinalY = Math.min(mFinalY, mMaxY);
mFinalY = Math.max(mFinalY, mMinY);
|
public final void | forceFinished(boolean finished)Force the finished field to a particular value.
mFinished = finished;
|
public float | getCurrVelocity()Returns the current velocity.
return mMode == FLING_MODE ?
mCurrVelocity : mVelocity - mDeceleration * timePassed() / 2000.0f;
|
public final int | getCurrX()Returns the current X offset in the scroll.
return mCurrX;
|
public final int | getCurrY()Returns the current Y offset in the scroll.
return mCurrY;
|
public final int | getDuration()Returns how long the scroll event will take, in milliseconds.
return mDuration;
|
public final int | getFinalX()Returns where the scroll will end. Valid only for "fling" scrolls.
return mFinalX;
|
public final int | getFinalY()Returns where the scroll will end. Valid only for "fling" scrolls.
return mFinalY;
|
private double | getSplineDeceleration(float velocity)
return Math.log(INFLEXION * Math.abs(velocity) / (mFlingFriction * mPhysicalCoeff));
|
private double | getSplineFlingDistance(float velocity)
final double l = getSplineDeceleration(velocity);
final double decelMinusOne = DECELERATION_RATE - 1.0;
return mFlingFriction * mPhysicalCoeff * Math.exp(DECELERATION_RATE / decelMinusOne * l);
|
private int | getSplineFlingDuration(float velocity)
final double l = getSplineDeceleration(velocity);
final double decelMinusOne = DECELERATION_RATE - 1.0;
return (int) (1000.0 * Math.exp(l / decelMinusOne));
|
public final int | getStartX()Returns the start X offset in the scroll.
return mStartX;
|
public final int | getStartY()Returns the start Y offset in the scroll.
return mStartY;
|
public final boolean | isFinished()Returns whether the scroller has finished scrolling.
return mFinished;
|
public boolean | isScrollingInDirection(float xvel, float yvel)
return !mFinished && Math.signum(xvel) == Math.signum(mFinalX - mStartX) &&
Math.signum(yvel) == Math.signum(mFinalY - mStartY);
|
public void | setFinalX(int newX)Sets the final position (X) for this scroller.
mFinalX = newX;
mDeltaX = mFinalX - mStartX;
mFinished = false;
|
public void | setFinalY(int newY)Sets the final position (Y) for this scroller.
mFinalY = newY;
mDeltaY = mFinalY - mStartY;
mFinished = false;
|
public final void | setFriction(float friction)The amount of friction applied to flings. The default value
is {@link ViewConfiguration#getScrollFriction}.
mDeceleration = computeDeceleration(friction);
mFlingFriction = friction;
|
public void | startScroll(int startX, int startY, int dx, int dy)Start scrolling by providing a starting point and the distance to travel.
The scroll will use the default value of 250 milliseconds for the
duration.
startScroll(startX, startY, dx, dy, DEFAULT_DURATION);
|
public void | startScroll(int startX, int startY, int dx, int dy, int duration)Start scrolling by providing a starting point, the distance to travel,
and the duration of the scroll.
mMode = SCROLL_MODE;
mFinished = false;
mDuration = duration;
mStartTime = AnimationUtils.currentAnimationTimeMillis();
mStartX = startX;
mStartY = startY;
mFinalX = startX + dx;
mFinalY = startY + dy;
mDeltaX = dx;
mDeltaY = dy;
mDurationReciprocal = 1.0f / (float) mDuration;
|
public int | timePassed()Returns the time elapsed since the beginning of the scrolling.
return (int)(AnimationUtils.currentAnimationTimeMillis() - mStartTime);
|