我试图找出设置任意时间的逻辑,然后以不同的速度(如 0.5 倍或 4 倍实时)“回放”该时间。
这是我到目前为止的逻辑,它将以正常速度播放时间:
import java.util.Calendar;
public class Clock {
long delta;
private float speed = 1f;
public Clock(Calendar startingTime) {
delta = System.currentTimeMillis()-startingTime.getTimeInMillis();
}
private Calendar adjustedTime() {
Calendar cal = Calendar.getInstance();
cal.setTimeInMillis(System.currentTimeMillis()-delta);
return cal;
}
public void setPlaybackSpeed(float speed){
this.speed = speed;
}
public static void main(String[] args){
Calendar calendar = Calendar.getInstance();
calendar.set(2010, 4, 4, 4, 4, 4);
Clock clock = new Clock(calendar);
while(true){
System.out.println(clock.adjustedTime().getTime());
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
}
我无法确定逻辑中需要在何处使用“速度”属性。