在ArcGIS的接口里,提供了IAnimationTracks,通過它我們可以實現三維動畫軌跡的創建,刪除或在播放當前軌跡的三維場景效果,進而可以生成三維場景的錄像輸出處理方法(這個我在下一篇再給大家介紹),還是看看具體實現代碼吧--
/// <summary>
/// 創建動畫軌跡
/// </summary>
public void CreateAnimationTrack( string trackName )
{
IAnimationTrack pAnimationTrack = new AnimationTrackClass();
IAnimationType pAnimationType = new AnimationTypeCameraClass();
pAnimationTrack.AnimationType = pAnimationType;
pAnimationTrack.Name = trackName;
pAnimationTrack.AttachObject( pSceneGraph.ActiveViewer.Camera );
pAnimationTrack.ApplyToAllViewers = true;
pAnimationTrack.EvenTimeStamps = false;
pAnimationTracks.AddTrack( pAnimationTrack );
}
/// <summary>
/// 刪除動畫軌跡
/// </summary>
public void RemoveAnimationTrack( string trackName )
{
IAnimationTrack pAnimationTrack;
pAnimationTracks.FindTrack( trackName, out pAnimationTrack );
pAnimationTracks.RemoveTrack( pAnimationTrack );
}
/// <summary>
/// 播放當前動畫軌跡
/// </summary>
public void PlayAnimationTrack( string trackName )
{
Hashtable htKeyTime = null;
bool[] TracksEnable = new Boolean[pAnimationTracks.TrackCount];
IAnimationTrack pAnimationTrack;
for( int index = 0; index < pAnimationTracks.TrackCount; index++ )
{
pAnimationTrack = pAnimationTracks.Tracks.get_Element( index ) as IAnimationTrack;
TracksEnable[index] = pAnimationTrack.IsEnabled;
if( pAnimationTrack.Name == trackName )
{
pAnimationTrack.IsEnabled = true;
htKeyTime = ClsAnimationTracks.GetKeyTimeTable( pAnimationTrack.Name );
}
else
pAnimationTrack.IsEnabled = false;
}
int sumTime = 0;
foreach( object obj in htKeyTime.Values )
{
sumTime += Convert.ToInt32( obj );
}
double duration = (double)sumTime / 1000;
TimeSpan timeSpan;
double elapsedTime;
DateTime startTime = DateTime.Now;
int j = 0;
do
{
timeSpan = (DateTime.Now).Subtract(startTime);
elapsedTime = timeSpan.TotalSeconds;
if (elapsedTime > duration) elapsedTime = duration;
pAnimationTracks.ApplyTracks(pSceneGraph.ActiveViewer, elapsedTime, duration);
pSceneGraph.RefreshViewers();
j = j + 1;
}
for( int index = 0; index < pAnimationTracks.TrackCount; index++ )
{
pAnimationTrack = pAnimationTracks.Tracks.get_Element( index ) as IAnimationTrack;
pAnimationTrack.IsEnabled = TracksEnable[index];
}
}
是不是很簡單呢?記住喲,在播放軌跡的時候,有個時間的處理方法,具體在程序調試時,多試幾次就知道是怎么回事了,我就不多說了。。。
當然,還有動畫幀的處理方法,ArcGIS也提供了一個很方便的接口IKeyframe來實現的,再通過動畫軌跡接口IAnimationTrack來實現動畫幀的創建,刪除等功能,代碼實現如下:
/// <summary>
/// 創建動畫幀
/// </summary>
public void CreateKeyFrame( IAnimationTrack pAnimationTrack, string keyName, int timeSpan )
{
IKeyframe pKeyframe = new Bookmark3DClass();
pKeyframe.Name = keyName;
pKeyframe.CaptureProperties( pScene, pSceneGraph.ActiveViewer.Camera );
pAnimationTrack.InsertKeyframe( pKeyframe, pAnimationTrack.KeyframeCount );
Hashtable htKeyTime = ClsAnimationTracks.GetKeyTimeTable( pAnimationTrack.Name );
int sumTime = 0;
foreach( object obj in htKeyTime.Values )
{
sumTime += Convert.ToInt32( obj );
}
double dblTime = 0;
for( int index = 0; index < pAnimationTrack.KeyframeCount; index++ )
{
pKeyframe = pAnimationTrack.get_Keyframe( index );
dblTime += Convert.ToDouble( htKeyTime[pKeyframe.Name] ) / sumTime;
pKeyframe.TimeStamp = dblTime;
}
}
/// <summary>
/// 刪除動畫幀
/// </summary>
public void RemoveKeyFrame( IAnimationTrack pAnimationTrack, string keyName )
{
for( int index = 0; index < pAnimationTrack.KeyframeCount; index++ )
{
if( pAnimationTrack.get_Keyframe( index ).Name == keyName )
{
pAnimationTrack.RemoveKeyframe( index );
break;
}
}
}
/// <summary>
/// 刪除當前軌跡的所有幀
/// </summary>
public void RemoveAllKeyFrame( IAnimationTrack pAnimationTrack )
{
pAnimationTrack.RemoveAllKeyframes();
}