Methods Summary |
---|
public void | abortAnimation()
mCurrX = mFinalX;
mCurrY = mFinalY;
mFinished = true;
|
public boolean | computeScrollOffset()Call this when you want to know the new location. If it returns true,
the animation is not yet finished. loc will be altered to provide the
new location.
if (mFinished) {
return false;
}
int timePassed = (int)(AnimationUtils.currentAnimationTimeMillis() - mStartTime);
if (timePassed < mDuration) {
switch (mMode) {
case SCROLL_MODE:
float x = (float)timePassed * mDurationReciprocal;
if (mInterpolator == null)
x = viscousFluid(x);
else
x = mInterpolator.getInterpolation(x);
mCurrX = mStartX + Math.round(x * mDeltaX);
mCurrY = mStartY + Math.round(x * mDeltaY);
if ((mCurrX == mFinalX) && (mCurrY == mFinalY)) {
mFinished = true;
}
break;
case FLING_MODE:
float timePassedSeconds = timePassed / 1000.0f;
float distance = (mVelocity * timePassedSeconds)
- (mDeceleration * timePassedSeconds * timePassedSeconds / 2.0f);
mCurrX = mStartX + Math.round(distance * mCoeffX);
// Pin to mMinX <= mCurrX <= mMaxX
mCurrX = Math.min(mCurrX, mMaxX);
mCurrX = Math.max(mCurrX, mMinX);
mCurrY = mStartY + Math.round(distance * mCoeffY);
// 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 setFinalX() or setFinalY().
int passed = timePassed();
mDuration = passed + extend;
mDurationReciprocal = 1.0f / (float)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.
mMode = FLING_MODE;
mFinished = false;
float velocity = (float)Math.hypot(velocityX, velocityY);
mVelocity = velocity;
mDuration = (int) (1000 * velocity / mDeceleration); // Duration is in
// milliseconds
mStartTime = AnimationUtils.currentAnimationTimeMillis();
mStartX = startX;
mStartY = startY;
mCoeffX = velocity == 0 ? 1.0f : velocityX / velocity;
mCoeffY = velocity == 0 ? 1.0f : velocityY / velocity;
int totalDistance = (int) ((velocity * velocity) / (2 * mDeceleration));
mMinX = minX;
mMaxX = maxX;
mMinY = minY;
mMaxY = maxY;
mFinalX = startX + Math.round(totalDistance * mCoeffX);
// Pin to mMinX <= mFinalX <= mMaxX
mFinalX = Math.min(mFinalX, mMaxX);
mFinalX = Math.max(mFinalX, mMinX);
mFinalY = startY + Math.round(totalDistance * mCoeffY);
// 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 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;
|
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 void | setFinalX(int newX)
mFinalX = newX;
mDeltaX = mFinalX - mStartX;
mFinished = false;
|
public void | setFinalY(int newY)
mFinalY = newY;
mDeltaY = mFinalY - mStartY;
mFinished = false;
|
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 and the distance to travel.
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;
// This controls the viscous fluid effect (how much of it)
mViscousFluidScale = 8.0f;
// must be set to 1.0 (used in viscousFluid())
mViscousFluidNormalize = 1.0f;
mViscousFluidNormalize = 1.0f / viscousFluid(1.0f);
|
public int | timePassed()
return (int)(AnimationUtils.currentAnimationTimeMillis() - mStartTime);
|
private float | viscousFluid(float x)
x *= mViscousFluidScale;
if (x < 1.0f) {
x -= (1.0f - (float)Math.exp(-x));
} else {
float start = 0.36787944117f; // 1/e == exp(-1)
x = 1.0f - (float)Math.exp(1.0f - x);
x = start + x * (1.0f - start);
}
x *= mViscousFluidNormalize;
return x;
|