Created
August 19, 2016 17:00
-
-
Save clarkezone/7b59ce6f7ad19ffe2f2f50f4e6ec30f8 to your computer and use it in GitHub Desktop.
Use composition layer and win2d to render an image at load time on a background thread and implicitly fade in when rendered
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| using Microsoft.Graphics.Canvas.UI.Composition; | |
| using Robmikh.Util.CompositionImageLoader; | |
| using System; | |
| using System.Numerics; | |
| using System.Threading; | |
| using System.Threading.Tasks; | |
| using Windows.Foundation; | |
| using Windows.UI; | |
| using Windows.UI.Composition; | |
| using Windows.UI.Xaml; | |
| using Windows.UI.Xaml.Controls; | |
| using Windows.UI.Xaml.Hosting; | |
| namespace imageplay | |
| { | |
| /// <summary> | |
| /// Use composition layer and win2d to render an image at load time on a background thread and implicitly fade in when rendered | |
| /// </summary> | |
| public sealed partial class MainPage : Page | |
| { | |
| Compositor _compositor; | |
| SpriteVisual _drawnVisual; | |
| CompositionDrawingSurface _surface; | |
| IImageLoader _loader; | |
| AutoResetEvent _event = new AutoResetEvent(false); | |
| public MainPage() | |
| { | |
| this.InitializeComponent(); | |
| this.SizeChanged += MainPage_SizeChanged; | |
| _compositor = ElementCompositionPreview.GetElementVisual(this).Compositor; | |
| _drawnVisual = _compositor.CreateSpriteVisual(); | |
| ElementCompositionPreview.SetElementChildVisual(foo, _drawnVisual); | |
| _drawnVisual.Opacity = 0; | |
| ImplicitAnimationCollection implicitAnimationCollection = _compositor.CreateImplicitAnimationCollection(); | |
| implicitAnimationCollection["Opacity"] = CreateAnimation(); | |
| _drawnVisual.ImplicitAnimations = implicitAnimationCollection; | |
| _loader = Robmikh.Util.CompositionImageLoader.ImageLoaderFactory.CreateImageLoader(_compositor); | |
| Task.Run(() => | |
| { | |
| //Simulate long render | |
| _event.WaitOne(1000); | |
| _surface = _loader.CreateSurface(new Size(100, 100)); | |
| _loader.DrawIntoSurface(_surface, (a, o) => | |
| { | |
| using (var session = CanvasComposition.CreateDrawingSession(a)) | |
| { | |
| session.Clear(Colors.Red); | |
| } | |
| }); | |
| _drawnVisual.Brush = _compositor.CreateSurfaceBrush(_surface); | |
| _drawnVisual.Opacity = 1.0f; | |
| }); | |
| } | |
| ScalarKeyFrameAnimation CreateAnimation() | |
| { | |
| var scaleKeyFrameAnimation = _compositor.CreateScalarKeyFrameAnimation(); | |
| scaleKeyFrameAnimation.Target = "Opacity"; | |
| scaleKeyFrameAnimation.InsertExpressionKeyFrame(1.0f, "this.FinalValue"); | |
| scaleKeyFrameAnimation.Duration = TimeSpan.FromMilliseconds(400); | |
| return scaleKeyFrameAnimation; | |
| } | |
| private void MainPage_SizeChanged(object sender, SizeChangedEventArgs e) | |
| { | |
| _drawnVisual.Size = new Vector2((float)e.NewSize.Width, (float)e.NewSize.Height); | |
| } | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment