Merge pull request #306 from Microsoft/animationcontroller

Add AnimationController
This commit is contained in:
Lindsay Kubasik 2018-12-03 22:22:11 -08:00 коммит произвёл GitHub
Родитель 969d002b4c 408a64a8d1
Коммит fcb5b85095
Не найден ключ, соответствующий данной подписи
Идентификатор ключа GPG: 4AEE18F83AFDEB23
7 изменённых файлов: 247 добавлений и 8 удалений

Двоичные данные
SampleGallery/Assets/SampleThumbnails/AnimationController.PNG Normal file

Двоичный файл не отображается.

После

Ширина:  |  Высота:  |  Размер: 13 KiB

Просмотреть файл

@ -322,7 +322,7 @@ namespace CompositionSampleGallery
_14393, // Anniversary Update (1607)
_15063, // Creators Update (1703)
_16299, // Fall Creators Update
_INSIDER // Insiders
_17134 // Version 1803
};
public RuntimeSupportedSDKs()

Просмотреть файл

@ -15,7 +15,7 @@
<FileAlignment>512</FileAlignment>
<ProjectTypeGuids>{A5A43C5B-DE2A-4C0C-9213-0A381AF9435A};{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}</ProjectTypeGuids>
<PackageCertificateKeyFile>CompositionSampleGallery_TemporaryKey.pfx</PackageCertificateKeyFile>
<TargetPlatformVersion>10.0.17763.0</TargetPlatformVersion>
<TargetPlatformVersion>10.0.17134.0</TargetPlatformVersion>
<TargetPlatformMinVersion>10.0.15063.0</TargetPlatformMinVersion>
<AppxAutoIncrementPackageRevision>True</AppxAutoIncrementPackageRevision>
<AppxBundle>Always</AppxBundle>
@ -132,6 +132,7 @@
<DependentUpon>HomePage.xaml</DependentUpon>
</Compile>
<Compile Include="Samples\SDK 14393\NineGridResizing\INineGridScenario.cs" />
<Compile Include="Samples\SDK 17134\AnimationController\AnimationControl.cs" />
<Compile Include="Shared\AppTelemetryClient.cs" />
<None Include="SampleGallery_TemporaryKey.pfx" />
</ItemGroup>
@ -325,6 +326,7 @@
<Content Include="Assets\Nature\Nature-7.jpg" />
<Content Include="Assets\NormalMapsAndMasks\bubblenm.jpg" />
<Content Include="Assets\SampleThumbnails\AdvancedShadows.PNG" />
<Content Include="Assets\SampleThumbnails\AnimationController.PNG" />
<Content Include="Assets\SampleThumbnails\BackDropControlSample.PNG" />
<Content Include="Assets\SampleThumbnails\BasicLayoutAndTransitions.PNG" />
<Content Include="Assets\SampleThumbnails\BasicXAMLInterop.PNG" />
@ -483,6 +485,10 @@
<Generator>MSBuild:Compile</Generator>
<SubType>Designer</SubType>
</Page>
<Page Include="Samples\SDK 17134\AnimationController\AnimationControl.xaml">
<Generator>MSBuild:Compile</Generator>
<SubType>Designer</SubType>
</Page>
<Page Include="Settings.xaml">
<SubType>Designer</SubType>
<Generator>MSBuild:Compile</Generator>
@ -727,9 +733,7 @@
<Name>SamplesCommon</Name>
</ProjectReference>
</ItemGroup>
<ItemGroup>
<Folder Include="Samples\SDK Insider\" />
</ItemGroup>
<ItemGroup />
<ItemGroup>
<PackageReference Include="Microsoft.ApplicationInsights">
<Version>2.8.0</Version>

Просмотреть файл

@ -0,0 +1,204 @@
//*********************************************************
//
// Copyright (c) Microsoft. All rights reserved.
// This code is licensed under the MIT License (MIT).
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED,
// INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
// IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,
// DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
// TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH
// THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
//
//*********************************************************
using System;
using System.Numerics;
using Windows.UI.Composition;
using Windows.UI.Xaml;
using Windows.UI.Xaml.Hosting;
using Windows.UI.Xaml.Controls;
using Windows.UI.Xaml.Controls.Primitives;
using Windows.UI.Xaml.Input;
namespace CompositionSampleGallery
{
public sealed partial class AnimationControl : SamplePage
{
public AnimationControl()
{
InitializeComponent();
AnimationSetup();
}
public static string StaticSampleName => "Animation Controller";
public override string SampleName => StaticSampleName;
public static string StaticSampleDescription => "Play, pause, speed up, slow down, " +
"or scrub your animation with the AnimationController API";
public override string SampleDescription => StaticSampleDescription;
private void AnimationSetup()
{
_compositor = Window.Current.Compositor;
_visual = ElementCompositionPreview.GetElementVisual(Rectangle);
_pause = true;
_interval = TimeSpan.FromMilliseconds(16); // based on 60f/sec
var linear = _compositor.CreateLinearEasingFunction();
int animationDuration = 4;
float animationMax = 250f;
// set up main animation
ElementCompositionPreview.SetIsTranslationEnabled(Rectangle, true);
_animation = _compositor.CreateVector3KeyFrameAnimation();
_animation.InsertKeyFrame(0.25f, new Vector3(animationMax, (float)Canvas.GetTop(Rectangle), 0f), linear);
_animation.InsertKeyFrame(0.5f, new Vector3(animationMax, animationMax, 0f), linear);
_animation.InsertKeyFrame(0.75f, new Vector3((float)Canvas.GetLeft(Rectangle), animationMax, 0f), linear);
_animation.InsertKeyFrame(1f, new Vector3((float)Canvas.GetLeft(Rectangle), (float)Canvas.GetTop(Rectangle), 0f), linear);
_animation.Duration = TimeSpan.FromSeconds(animationDuration);
_animation.IterationBehavior = AnimationIterationBehavior.Forever;
// set up dispatcher timer to animate slider
_sliderAnimator = new DispatcherTimer();
_sliderAnimator.Tick += SliderBehavior;
_sliderAnimator.Interval = _interval;
// initialize amount to change slider value per tick
_delta = (slider.Maximum / _animation.Duration.TotalMilliseconds) * _interval.TotalMilliseconds;
// add pointer listeners to slider for smooth scrubbing action
slider.AddHandler(PointerPressedEvent, new PointerEventHandler(PressedThumb), true);
slider.AddHandler(PointerReleasedEvent, new PointerEventHandler(ReleasedThumb), true);
}
private void EnsureController()
{
if (_controller == null)
{
// start animation on visual, and grab AnimationController from visual
_visual.StartAnimation(nameof(Visual.Offset), _animation);
_controller = _visual.TryGetAnimationController(nameof(Visual.Offset));
_controller.Pause();
}
}
private void PlayPause_Animation(object sender, RoutedEventArgs e)
{
EnsureController();
if (_pause)
{
_controller.Resume();
PlaySlider();
PlayIcon.Symbol = Symbol.Pause;
}
else
{
_controller.Pause();
StopSlider();
PlayIcon.Symbol = Symbol.Play;
}
_pause = !_pause;
}
private void Stop_Animation(object sender, RoutedEventArgs e)
{
_pause = false;
PlayPause_Animation(sender, e);
_controller.PlaybackRate = 1;
_controller.Progress = 0;
slider.Value = 0;
}
private void SpeedUp_Animation(object sender, RoutedEventArgs e)
{
EnsureController();
if (Math.Abs(_controller.PlaybackRate) < Math.Abs(AnimationController.MaxPlaybackRate) && !_pause)
{
_controller.PlaybackRate *= 2;
}
}
private void SlowDown_Animation(object sender, RoutedEventArgs e)
{
EnsureController();
if (Math.Abs(_controller.PlaybackRate) > Math.Abs(AnimationController.MinPlaybackRate) && !_pause)
{
_controller.PlaybackRate /= 2;
}
}
private void Reverse_Animation(object sender, RoutedEventArgs e)
{
if (!_pause)
{
_controller.PlaybackRate *= -1;
}
}
// helper methods to animate slider
private void OnSliderValueChanged(object sender, RangeBaseValueChangedEventArgs e)
{
EnsureController();
// this enables scrubbing
_controller.Progress = (float)slider.Value * .01f;
}
private void SliderBehavior(object sender, object e)
{
// update slider.Value based on time elapsed
DateTimeOffset currentTime = DateTimeOffset.Now;
TimeSpan elapsedTime = currentTime - lastTime;
double numTicks = elapsedTime.TotalMilliseconds / _interval.TotalMilliseconds;
slider.Value += (_delta * numTicks * _controller.PlaybackRate);
// logic to loop slider animation
// if PlaybackRate is less than 0, playing in reverse
if (_controller.PlaybackRate > 0)
{
if (slider.Value == 100)
{
slider.Value = 0;
}
}
else
{
if (slider.Value == 0)
{
slider.Value = 100;
}
}
lastTime = currentTime;
}
private void PlaySlider()
{
if (_sliderAnimator != null)
{
_sliderAnimator.Start();
lastTime = DateTimeOffset.Now;
}
}
private void StopSlider()
{
if (_sliderAnimator != null)
_sliderAnimator.Stop();
}
// helper methods for mouse input on the slider for smooth scrubbing action
private void PressedThumb(object sender, PointerRoutedEventArgs e)
{
EnsureController();
StopSlider();
_controller.Pause();
}
private void ReleasedThumb(object sender, PointerRoutedEventArgs e)
{
EnsureController();
if (!_pause)
{
PlaySlider();
_controller.Resume();
}
}
private Compositor _compositor;
private Vector3KeyFrameAnimation _animation;
private AnimationController _controller;
private Visual _visual;
private bool _pause;
private DispatcherTimer _sliderAnimator;
private TimeSpan _interval;
private DateTimeOffset lastTime;
private double _delta;
}
}

Просмотреть файл

@ -0,0 +1,30 @@
<local:SamplePage
x:Class="CompositionSampleGallery.AnimationControl"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:local="using:CompositionSampleGallery"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
mc:Ignorable="d">
<Grid Background="{ThemeResource ApplicationPageBackgroundThemeBrush}">
<Canvas>
<Rectangle x:Name="Rectangle" Width="50" Height="50" Fill="Blue" Canvas.Left="20" Canvas.Top="20"/>
</Canvas>
<Button x:Name="PlayButton" Width="50" HorizontalAlignment="Left" Margin="20,250,0,0" VerticalAlignment="Center" Click="PlayPause_Animation" >
<SymbolIcon x:Name="PlayIcon" Symbol="Play"/>
</Button>
<Button x:Name="StopButton" Width="50" HorizontalAlignment="Left" Margin="80,250,0,0" VerticalAlignment="Center" Click="Stop_Animation">
<SymbolIcon Symbol="Stop"/>
</Button>
<Button x:Name="SpeedUp" Content="2.0x" Width="50" HorizontalAlignment="Left" Margin="140,250,0,0" VerticalAlignment="Center" Click="SpeedUp_Animation"/>
<Button x:Name="SlowDown" Content="0.5x" Width="50" HorizontalAlignment="Left" Margin="200,250,0,0" VerticalAlignment="Center" Click="SlowDown_Animation"/>
<Button x:Name="Reverse" Width="50" HorizontalAlignment="Left" Margin="260,250,0,0" VerticalAlignment="Center" Click="Reverse_Animation">
<SymbolIcon Symbol="Back"/>
</Button>
<StackPanel HorizontalAlignment="Left" Margin="20,150,0,0" VerticalAlignment="Center" Width="290" >
<Slider x:Name="slider" Minimum="0" Maximum="100" TickFrequency="25" TickPlacement="BottomRight" ValueChanged="OnSliderValueChanged" />
</StackPanel>
</Grid>
</local:SamplePage>

Просмотреть файл

@ -179,10 +179,11 @@ namespace CompositionSampleGallery
#endif
#if SDKVERSION_16299
new SampleDefinition(SpringyImage.StaticSampleName, typeof(SpringyImage), SampleType.Reference, SampleCategory.Motion, false, false, "ms-appx:///Assets/SampleThumbnails/SpringyImage.PNG", description: SpringyImage.StaticSampleDescription, sdkVersion: RuntimeSupportedSDKs.SDKVERSION._16299, dateAdded: new DateTime(2017,08,7), tags: new string[1]{"ExpressionBuilder"}),
new SampleDefinition(SpringyImage.StaticSampleName, typeof(SpringyImage), SampleType.Reference, SampleCategory.Motion, false, false, "ms-appx:///Assets/SampleThumbnails/SpringyImage.PNG", description: SpringyImage.StaticSampleDescription, sdkVersion: RuntimeSupportedSDKs.SDKVERSION._16299, dateAdded: new DateTime(2017,08,7), tags: new string[1]{"ExpressionBuilder"}),
#endif
#if SDKVERSION_INSIDER
#if SDKVERSION_17134
new SampleDefinition(AnimationControl.StaticSampleName, typeof(AnimationControl), SampleType.Reference, SampleCategory.APIReference, false, false, "ms-appx:///Assets/SampleThumbnails/AnimationController.PNG", description: AnimationControl.StaticSampleDescription, sdkVersion: RuntimeSupportedSDKs.SDKVERSION._17134, dateAdded: new DateTime(2018,12,3)),
#endif
};

Просмотреть файл

@ -12,7 +12,7 @@
<DefineConstants Condition="$(TargetPlatformBuild) &gt;=14393">$(DefineConstants);SDKVERSION_14393</DefineConstants>
<DefineConstants Condition="$(TargetPlatformBuild) &gt;=15063">$(DefineConstants);SDKVERSION_15063</DefineConstants>
<DefineConstants Condition="$(TargetPlatformBuild) &gt;=16299">$(DefineConstants);SDKVERSION_16299</DefineConstants>
<DefineConstants Condition="$(TargetPlatformBuild) &gt; 16299">$(DefineConstants);SDKVERSION_INSIDER</DefineConstants>
<DefineConstants Condition="$(TargetPlatformBuild) &gt; 17134">$(DefineConstants);SDKVERSION_17134</DefineConstants>
</PropertyGroup>
</Project>