public class Motion extends Object
This class relies on the System.currentTimeMillis() method to provide transitions between coordinates. The motion can be subclassed to provide every type of motion feel from parabolic motion to spline and linear motion. The default implementation provides a simple algorithm giving the feel of acceleration and deceleration.
Modifier | Constructor and Description |
---|---|
protected |
Motion(int sourceValue,
double initVelocity,
double friction) |
protected |
Motion(int sourceValue,
float initVelocity,
float friction)
Construct a velocity motion
|
protected |
Motion(int sourceValue,
int destinationValue,
int duration)
Construct a point/destination motion
|
Modifier and Type | Method and Description |
---|---|
int |
countAvailableVelocitySamplingPoints()
Gets the number of sampling points that can be used by
getVelocity() . |
static Motion |
createCubicBezierMotion(int sourceValue,
int destinationValue,
int duration,
float p0,
float p1,
float p2,
float p3)
Creates a standard Cubic Bezier motion to implement functions such as ease-in/out etc.
|
static Motion |
createDecelerationMotion(int sourceValue,
int destinationValue,
int duration)
Creates a deceleration motion starting from source value all the way to destination value
|
static Motion |
createDecelerationMotionFrom(Motion motion,
int maxDestinationValue,
int maxDuration)
Creates a deceleration motion starting from the current position of another motion.
|
static Motion |
createEaseInMotion(int sourceValue,
int destinationValue,
int duration)
Equivalent to createCubicBezierMotion with 0f, 0.42f, 1f, 1f as arguments.
|
static Motion |
createEaseInOutMotion(int sourceValue,
int destinationValue,
int duration)
Equivalent to createCubicBezierMotion with 0, 0.42, 0.58, 1.0 as arguments.
|
static Motion |
createEaseMotion(int sourceValue,
int destinationValue,
int duration)
Equivalent to createCubicBezierMotion with 0f, 0.25f, 0.25f, 1 as arguments.
|
static Motion |
createEaseOutMotion(int sourceValue,
int destinationValue,
int duration)
Equivalent to createCubicBezierMotion with 0f, 0f, 0.58f, 1.0f as arguments.
|
static Motion |
createExponentialDecayMotion(int sourceValue,
int maxValue,
double initVelocity,
double timeConstant) |
static Motion |
createFrictionMotion(int sourceValue,
int maxValue,
float initVelocity,
float friction)
Creates a friction motion starting from source with initial speed and the friction
|
static Motion |
createLinearColorMotion(int sourceValue,
int destinationValue,
int duration)
Creates a linear motion starting from source value all the way to destination value for a color value.
|
static Motion |
createLinearMotion(int sourceValue,
int destinationValue,
int duration)
Creates a linear motion starting from source value all the way to destination value
|
static Motion |
createSplineMotion(int sourceValue,
int destinationValue,
int duration)
Creates a spline motion starting from source value all the way to destination value
|
void |
finish()
Sends the motion to the end time instantly which is useful for flushing an animation
|
long |
getCurrentMotionTime()
Returns the current time within the motion relative to start time
|
int |
getDestinationValue()
The number to which we will reach when the motion is finished
|
int |
getDuration()
Returns the animation duration
|
int |
getSourceValue()
The number from which we are starting (usually indicating animation start position)
|
protected long |
getStartTime()
The value of System.currentTimemillis() when motion was started
|
int |
getValue()
Returns the value for the motion for the current clock time.
|
double |
getVelocity()
Gets an approximation of the current velocity in pixels per millisecond.
|
boolean |
isDecayMotion() |
boolean |
isFinished()
Returns true if the motion has run its course and has finished meaning the current
time is greater than startTime + duration.
|
static boolean |
isSlowMotion()
Allows debugging motion behavior by slowing motions down 50 fold, doesn't apply to friction motion
|
void |
setCurrentMotionTime(long currentMotionTime)
Allows overriding the getCurrentMotionTime method value with a manual value
to provide full developer control over animation speed/position.
|
static void |
setSlowMotion(boolean aSlowMotion)
Allows debugging motion behavior by slowing motions down 50 fold, doesn't apply to friction motion
|
void |
setSourceValue(int sourceValue)
The number from which we are starting (usually indicating animation start position)
|
void |
setStartTime(long startTime)
Sets the start time of the motion
|
void |
start()
Sets the start time to the current time
|
protected Motion(int sourceValue, int destinationValue, int duration)
sourceValue
- starting valuedestinationValue
- destination valueduration
- motion durationprotected Motion(int sourceValue, float initVelocity, float friction)
sourceValue
- starting valueinitVelocity
- initial velocityfriction
- degree of frictionprotected Motion(int sourceValue, double initVelocity, double friction)
public static boolean isSlowMotion()
public static void setSlowMotion(boolean aSlowMotion)
aSlowMotion
- the slowMotion to setpublic void finish()
public static Motion createCubicBezierMotion(int sourceValue, int destinationValue, int duration, float p0, float p1, float p2, float p3)
sourceValue
- starting valuedestinationValue
- destination valueduration
- motion durationp0
- argument to the bezier functionp1
- argument to the bezier functionp2
- argument to the bezier functionp3
- argument to the bezier functionpublic static Motion createEaseInOutMotion(int sourceValue, int destinationValue, int duration)
sourceValue
- starting valuedestinationValue
- destination valueduration
- motion durationpublic static Motion createEaseMotion(int sourceValue, int destinationValue, int duration)
sourceValue
- starting valuedestinationValue
- destination valueduration
- motion durationpublic static Motion createEaseInMotion(int sourceValue, int destinationValue, int duration)
sourceValue
- starting valuedestinationValue
- destination valueduration
- motion durationpublic static Motion createEaseOutMotion(int sourceValue, int destinationValue, int duration)
sourceValue
- starting valuedestinationValue
- destination valueduration
- motion durationpublic static Motion createLinearMotion(int sourceValue, int destinationValue, int duration)
sourceValue
- the number from which we are starting (usually indicating animation start position)destinationValue
- the number to which we are heading (usually indicating animation destination)duration
- the length in milliseconds of the motion (time it takes to get from sourceValue to
destinationValue)public static Motion createLinearColorMotion(int sourceValue, int destinationValue, int duration)
sourceValue
- the color from which we are startingdestinationValue
- the destination colorduration
- the length in milliseconds of the motion (time it takes to get from sourceValue to
destinationValue)public static Motion createSplineMotion(int sourceValue, int destinationValue, int duration)
sourceValue
- the number from which we are starting (usually indicating animation start position)destinationValue
- the number to which we are heading (usually indicating animation destination)duration
- the length in milliseconds of the motion (time it takes to get from sourceValue to
destinationValue)public static Motion createDecelerationMotion(int sourceValue, int destinationValue, int duration)
sourceValue
- the number from which we are starting (usually indicating animation start position)destinationValue
- the number to which we are heading (usually indicating animation destination)duration
- the length in milliseconds of the motion (time it takes to get from sourceValue to
destinationValue)public static Motion createDecelerationMotionFrom(Motion motion, int maxDestinationValue, int maxDuration)
motion
- the number from which we are starting (usually indicating animation start position)maxDestinationValue
- The farthest position to allow motion to go.maxDuration
- The longest that the duration is allowed to proceed for.public static Motion createFrictionMotion(int sourceValue, int maxValue, float initVelocity, float friction)
sourceValue
- the number from which we are starting (usually indicating animation start position)maxValue
- the maximum value for the frictioninitVelocity
- the starting velocityfriction
- the motion frictionpublic static Motion createExponentialDecayMotion(int sourceValue, int maxValue, double initVelocity, double timeConstant)
public void start()
public long getCurrentMotionTime()
public void setCurrentMotionTime(long currentMotionTime)
currentMotionTime
- the time in milliseconds for the motion.public boolean isDecayMotion()
public void setStartTime(long startTime)
startTime
- the starting timepublic boolean isFinished()
public int getValue()
public double getVelocity()
NOTE: If countAvailableVelocitySamplingPoints()
<= 1, then this method will always output 0.
Therefore the output of this method only has meaning if countAvailableVelocitySamplingPoints()
> 0
public int countAvailableVelocitySamplingPoints()
getVelocity()
. A minimum of 2 sampling
points are required for the result of getVelocity()
to have any meaning.getVelocity()
.public int getSourceValue()
public int getDestinationValue()
public void setSourceValue(int sourceValue)
sourceValue
- the source valueprotected long getStartTime()
public int getDuration()