using System; using System.Diagnostics; namespace PCL.Core.Utils; /// /// 帮助动画帧的工具类。 /// public static class FrameUtils { public static long Frequency { get; } static FrameUtils() { Frequency = Stopwatch.Frequency; } /// /// 获取现在的时间戳。 /// /// 时间戳。 public static long NowStamp() => Stopwatch.GetTimestamp(); /// /// 将时间戳转换为帧索引。 /// /// 开始时的时间戳。 /// 帧率。 /// 帧索引。 public static long StampToFrameIndex(long startStamp, int fps) { // 计算经过的时间戳 var durationStamp = NowStamp() - startStamp; if (durationStamp <= 0) return 0; // 计算帧索引 var index = durationStamp * fps / Frequency; return index; } /// /// 将时间跨度转换为帧索引。 /// /// 开始时的时间。 /// 当前时间。 /// 帧率。 /// 帧索引。 public static long TimeSpanToFrameIndex(TimeSpan startTime, TimeSpan currentTime, int fps) { // 如果当前时间早于开始时间,则返回0 if (currentTime < startTime) return 0; // 计算经过的时间 var duration = currentTime - startTime; // 计算帧索引 var index = (long)(duration.TotalSeconds * fps); return index; } }