优化AnimationTimer部分判断
This commit is contained in:
parent
3492be3ce5
commit
b496608531
1 changed files with 9 additions and 7 deletions
|
@ -13,7 +13,9 @@ public class AnimationTimer {
|
||||||
private long startTime;
|
private long startTime;
|
||||||
private boolean reversed;
|
private boolean reversed;
|
||||||
private boolean initialized;
|
private boolean initialized;
|
||||||
private boolean isStart;
|
|
||||||
|
// 未初始化状态下,动画进度是否从0开始
|
||||||
|
private boolean playFromStart;
|
||||||
|
|
||||||
private Function<Double, Double> forwardAnimationCurve = AnimationCurves.LINEAR;
|
private Function<Double, Double> forwardAnimationCurve = AnimationCurves.LINEAR;
|
||||||
private Function<Double, Double> backwardAnimationCurve = AnimationCurves.LINEAR;
|
private Function<Double, Double> backwardAnimationCurve = AnimationCurves.LINEAR;
|
||||||
|
@ -110,7 +112,7 @@ public class AnimationTimer {
|
||||||
}
|
}
|
||||||
|
|
||||||
private long getElapsedTime(long currentTime) {
|
private long getElapsedTime(long currentTime) {
|
||||||
if (!initialized) return 0;
|
if (!initialized) return playFromStart ? 0 : duration;
|
||||||
|
|
||||||
if (reversed) {
|
if (reversed) {
|
||||||
return Math.min(duration, Math.max(0, startTime - currentTime));
|
return Math.min(duration, Math.max(0, startTime - currentTime));
|
||||||
|
@ -123,7 +125,7 @@ public class AnimationTimer {
|
||||||
* 当前动画是否已经结束
|
* 当前动画是否已经结束
|
||||||
*/
|
*/
|
||||||
public boolean finished(long currentTime) {
|
public boolean finished(long currentTime) {
|
||||||
return getProgress(currentTime) >= 1;
|
return getElapsedTime(currentTime) >= duration;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
@ -131,7 +133,7 @@ public class AnimationTimer {
|
||||||
*/
|
*/
|
||||||
public void begin() {
|
public void begin() {
|
||||||
initialized = false;
|
initialized = false;
|
||||||
isStart = true;
|
playFromStart = true;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
@ -139,7 +141,7 @@ public class AnimationTimer {
|
||||||
*/
|
*/
|
||||||
public void end() {
|
public void end() {
|
||||||
initialized = false;
|
initialized = false;
|
||||||
isStart = false;
|
playFromStart = false;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
@ -148,7 +150,7 @@ public class AnimationTimer {
|
||||||
public void forward(long currentTime) {
|
public void forward(long currentTime) {
|
||||||
if (!initialized) {
|
if (!initialized) {
|
||||||
initialized = true;
|
initialized = true;
|
||||||
startTime = currentTime + (isStart ? 0 : duration);
|
startTime = currentTime + (playFromStart ? 0 : duration);
|
||||||
} else if (reversed) {
|
} else if (reversed) {
|
||||||
startTime = currentTime - getElapsedTime(currentTime);
|
startTime = currentTime - getElapsedTime(currentTime);
|
||||||
}
|
}
|
||||||
|
@ -177,7 +179,7 @@ public class AnimationTimer {
|
||||||
public void backward(long currentTime) {
|
public void backward(long currentTime) {
|
||||||
if (!initialized) {
|
if (!initialized) {
|
||||||
initialized = true;
|
initialized = true;
|
||||||
startTime = currentTime + (isStart ? duration : 0);
|
startTime = currentTime + (playFromStart ? duration : 0);
|
||||||
} else if (!reversed) {
|
} else if (!reversed) {
|
||||||
startTime = currentTime + getElapsedTime(currentTime);
|
startTime = currentTime + getElapsedTime(currentTime);
|
||||||
}
|
}
|
||||||
|
|
Loading…
Add table
Reference in a new issue