我現在正在處理以下問題一週。請解釋一下我的看法。WPF:沿路徑幾何動畫
我正在努力實現的是,對於我們中的一些人來說,非常簡單,但對於WPF來說新手會讓事情變得艱難。
上面的圖片是通過轉換渲染(已有)svg
到xaml
事後其導入到Visual Studio。這就是xaml的樣子。
<Canvas x:Name="Main" Margin="158,-223,-148,233">
<Canvas.RenderTransform>
<MatrixTransform Matrix="1.25 0 0 -1.25 -197.1231 961.58875"/>
</Canvas.RenderTransform>
<Path xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" Name="path139746" Fill="#FFD3BC5F" Canvas.Left="399.4" Canvas.Top="-172.6">
<Path.Data>
<PathGeometry Figures="m 66.611178 552.33853 c -4.36996 0.6305 -8.8334 0.97293 -13.3752 0.97293 -18.11465 0 -35.027 -5.21161 -49.34949 -14.18974 l 21.64732 -36.67733 c 8.2963 5.25686 18.12667 8.31294 28.67512 8.31294 2.7121 0 5.37755 -0.20365 7.98106 -0.59283 l 2.23539 -0.38916 c 4.72288 -0.91109 9.2226 -2.43913 13.41755 -4.49964 l 35.933612 77.65818 c -12.53504 5.94167 -26.095762 10.06571 -40.347392 12.0342 0 0 -2.27764 -21.55688 -4.54327 -42.98252 l -2.2747 0.35297 z" FillRule="nonzero"/>
</Path.Data>
</Path>
<Path xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" Fill="#FFD3BC5F" Canvas.Left="401" Canvas.Top="-173" >
<Path.Data>
<PathGeometry x:Name="path139750" Figures="m -21.311892 515.98804 c -11.7053 -15.57594 -18.6547 -34.92004 -18.6622 -55.85694 l 35.53987 0 5.06525 -0.024 c 0.63507 11.4535 4.86922 21.94 11.58023 30.3706 l 1.44647 1.7423 c 2.94008 3.3894 6.29329 6.4077 9.98888 8.9736 l -43.53754 73.76488 c -13.18956 -8.30539 -24.88746 -18.76176 -34.60186 -30.87743 0 0 17.3968 -13.23944 34.61076 -26.3386 l -1.42986 -1.75428 z" FillRule="nonzero"/>
</Path.Data>
</Path>
<Path xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" Name="path139780" Fill="#FFD3BC5F" Canvas.Left="401.4" Canvas.Top="-167.6">
<Path.Data>
<PathGeometry Figures="m 214.75854 460.1016 94.92962 0 0 69.86866 -94.92962 0 0 -69.86866 z" FillRule="nonzero"/>
</Path.Data>
</Path>
<Path xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" Name="path139784" Fill="#FFD3BC5F" Canvas.Left="401.4" Canvas.Top="-167.6">
<Path.Data>
<PathGeometry Figures="m 211.64803 529.825 -36.87191 0 -62.32503 -69.72236 100.95576 0 0 69.74499 -1.75882 -0.0226 z" FillRule="nonzero"/>
</Path.Data>
</Path>
<Path xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" Fill="#FFD3BC5F" Canvas.Top="-173" Canvas.Left="401">
<Path.Data>
<PathGeometry x:Name="path139798" Figures="m -41.482092 460.131 -43.5377 0 c 0.6744 30.93022 11.4414 59.36853 29.1278 82.17137 l 33.3799 -25.40184 c -11.9001 -15.82789 -18.9641 -35.4887 -18.97 -56.76953" FillRule="nonzero"/>
</Path.Data>
</Path>
<Path xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" Name="path139802" Fill="#FFD3BC5F" Canvas.Left="399.4" Canvas.Top="-172.6">
<Path.Data>
<PathGeometry Figures="m 139.82292 494.54117 c -13.01467 -14.5593 -25.97803 -29.06131 -30.40076 -34.00892 0 0 -2.72123 28.49563 -29.566602 43.70507 l 17.54746 37.92328 c 19.158502 -10.35383 34.278952 -27.22704 42.419902 -47.61943" FillRule="nonzero"/>
</Path.Data>
</Path>
<Path xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" Name="path139806" Fill="#FFD3BC5F" Canvas.Left="399.4" Canvas.Top="-172.6">
<Path.Data>
<PathGeometry Figures="m 98.037008 543.53024 17.776782 38.41802 c 23.1211 -11.42479 42.62202 -29.06882 56.29587 -50.76297 l 0.18403 -0.31676 c 0 0 -15.53072 -17.37553 -31.34655 -35.06934 -8.34762 20.43762 -23.62341 37.33197 -42.910132 47.73105" FillRule="nonzero"/>
</Path.Data>
</Path>
<Path xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" Name="path139810" Fill="#FFD3BC5F" Canvas.Left="399.4" Canvas.Top="-172.6">
<Path.Data>
<PathGeometry Figures="m 53.235488 554.82033 c -18.39362 0 -35.56859 -5.28853 -50.11595 -14.3994 l -21.08756 35.73002 c 21.04842 12.78086 45.75185 20.14348 72.17809 20.14348 5.74697 0 11.40815 -0.36201 16.97265 -1.03931 l -4.37747 -41.4213 c -4.43321 0.63805 -8.96144 0.98651 -13.56976 0.98651" FillRule="nonzero"/>
</Path.Data>
</Path>
<Rectangle Name="object_to_move" Fill="LightBlue" Height="36" Stroke="Black" Width="41" Canvas.Left="330" Canvas.Top="229"/>
我要的是動畫/沿着路徑移動的方塊(矩形),使得它(箱子)看起來像旅行和使用它(路徑)的道路繼續前進。
我試過使用DoubleAnimationUsingPath
。但是我很困惑,我認爲我沒有正確地使用東西。
我發現了下面的代碼snippt,並試圖操縱它並將其應用於我的示例,但無法使其工作。
// path139798.Freeze(); // For performance benefits.
DoubleAnimationUsingPath daPath = new DoubleAnimationUsingPath();
daPath.Duration = TimeSpan.FromSeconds(5);
daPath.RepeatBehavior = RepeatBehavior.Forever;
daPath.AccelerationRatio = 0.6;
daPath.DecelerationRatio = 0.4;
daPath.AutoReverse = true;
daPath.PathGeometry = path139798;
daPath.Source = PathAnimationSource.X;
circle2.BeginAnimation(Canvas.LeftProperty, daPath);
daPath = new DoubleAnimationUsingPath();
daPath.Duration = TimeSpan.FromSeconds(5);
daPath.RepeatBehavior = RepeatBehavior.Forever;
daPath.AccelerationRatio = 0.6;
daPath.DecelerationRatio = 0.4;
daPath.AutoReverse = true;
daPath.PathGeometry = path139798;
daPath.Source = PathAnimationSource.Y;
circle2.BeginAnimation(Canvas.TopProperty, daPath);
}
有人可以幫忙。謝謝!
Ooooh,這看起來很有趣。所以我看到的全部是path139798。這是否意味着你只是試圖讓它遵循那個有這個名字的左下第一個形狀的路徑?它幾乎看起來像你有一個與其他形狀的迷宮的開始,所以我想我會問,因爲我很難根據你的解釋來想象你想要的結果。 –
@ChrisW。嘿,感謝您抽出寶貴時間並提問。我試圖讓盒子沿着上面描述的形狀走向前進,只有在代碼示例中只有一個形狀(您是對的),但是代碼示例不起作用。我只是想通過只使用1個路徑名來確定我有沒有正確的東西。所以,我試圖讓盒子沿着形狀移動。 – user3641381
好的,你需要一個一致的路徑來定義你的運動路徑,注意把你的圖片加載到繪畫或者其他東西上,然後畫出一個粗糙的線條,矩形應該採取的路線? –