Fade animations

This commit is contained in:
ClemensFischer 2025-01-05 10:31:15 +01:00
parent b9a34fd5e4
commit 4c669f4df0
7 changed files with 45 additions and 39 deletions

View file

@ -10,7 +10,7 @@ namespace MapControl
{ {
public static void FadeOver(Image topImage, Image bottomImage) public static void FadeOver(Image topImage, Image bottomImage)
{ {
var animation = new Animation var fadeInAnimation = new Animation
{ {
FillMode = FillMode.Forward, FillMode = FillMode.Forward,
Duration = MapBase.ImageFadeDuration, Duration = MapBase.ImageFadeDuration,
@ -24,7 +24,7 @@ namespace MapControl
} }
}; };
_ = animation.RunAsync(topImage).ContinueWith( _ = fadeInAnimation.RunAsync(topImage).ContinueWith(
_ => bottomImage.Opacity = 0d, _ => bottomImage.Opacity = 0d,
TaskScheduler.FromCurrentSynchronizationContext()); TaskScheduler.FromCurrentSynchronizationContext());
} }

View file

@ -8,9 +8,9 @@ namespace MapControl
{ {
public partial class Tile public partial class Tile
{ {
private void AnimateImageOpacity() private void FadeIn()
{ {
var animation = new Animation var fadeInAnimation = new Animation
{ {
Duration = MapBase.ImageFadeDuration, Duration = MapBase.ImageFadeDuration,
Children = Children =
@ -28,7 +28,7 @@ namespace MapControl
} }
}; };
_ = animation.RunAsync(Image); _ = fadeInAnimation.RunAsync(Image);
} }
} }
} }

View file

@ -43,7 +43,7 @@ namespace MapControl
if (image != null && MapBase.ImageFadeDuration > TimeSpan.Zero) if (image != null && MapBase.ImageFadeDuration > TimeSpan.Zero)
{ {
AnimateImageOpacity(); FadeIn();
} }
} }
} }

View file

@ -12,18 +12,22 @@ namespace MapControl
{ {
public static void FadeOver(Image topImage, Image bottomImage) public static void FadeOver(Image topImage, Image bottomImage)
{ {
topImage.BeginAnimation(OpacityProperty, new DoubleAnimation var fadeInAnimation = new DoubleAnimation
{ {
To = 1d, To = 1d,
Duration = MapBase.ImageFadeDuration Duration = MapBase.ImageFadeDuration
}); };
bottomImage.BeginAnimation(OpacityProperty, new DoubleAnimation var fadeOutAnimation = new DoubleAnimation
{ {
To = 0d, To = 0d,
BeginTime = MapBase.ImageFadeDuration, BeginTime = MapBase.ImageFadeDuration,
Duration = TimeSpan.Zero Duration = TimeSpan.Zero,
}); FillBehavior = FillBehavior.Stop
};
topImage.BeginAnimation(OpacityProperty, fadeInAnimation);
bottomImage.BeginAnimation(OpacityProperty, fadeOutAnimation);
} }
} }
} }

View file

@ -13,18 +13,19 @@ namespace MapControl
{ {
public partial class Tile public partial class Tile
{ {
private void BeginOpacityAnimation() private void BeginFadeInAnimation()
{ {
Image.BeginAnimation(UIElement.OpacityProperty, var fadeInAnimation = new DoubleAnimation
new DoubleAnimation
{ {
From = 0d, From = 0d,
Duration = MapBase.ImageFadeDuration, Duration = MapBase.ImageFadeDuration,
FillBehavior = FillBehavior.Stop FillBehavior = FillBehavior.Stop
}); };
Image.BeginAnimation(UIElement.OpacityProperty, fadeInAnimation);
} }
private void AnimateImageOpacity() private void FadeIn()
{ {
if (Image.Source is BitmapSource bitmap && bitmap.IsDownloading && !bitmap.IsFrozen) if (Image.Source is BitmapSource bitmap && bitmap.IsDownloading && !bitmap.IsFrozen)
{ {
@ -33,7 +34,7 @@ namespace MapControl
} }
else else
{ {
BeginOpacityAnimation(); BeginFadeInAnimation();
} }
} }
@ -44,7 +45,7 @@ namespace MapControl
bitmap.DownloadCompleted -= BitmapDownloadCompleted; bitmap.DownloadCompleted -= BitmapDownloadCompleted;
bitmap.DownloadFailed -= BitmapDownloadFailed; bitmap.DownloadFailed -= BitmapDownloadFailed;
BeginOpacityAnimation(); BeginFadeInAnimation();
} }
private void BitmapDownloadFailed(object sender, ExceptionEventArgs e) private void BitmapDownloadFailed(object sender, ExceptionEventArgs e)

View file

@ -17,28 +17,29 @@ namespace MapControl
{ {
public static void FadeOver(Image topImage, Image bottomImage) public static void FadeOver(Image topImage, Image bottomImage)
{ {
var topImageAnimation = new DoubleAnimation var fadeInAnimation = new DoubleAnimation
{ {
To = 1d, To = 1d,
Duration = MapBase.ImageFadeDuration Duration = MapBase.ImageFadeDuration
}; };
var bottomImageAnimation = new DoubleAnimation var fadeOutAnimation = new DoubleAnimation
{ {
To = 0d, To = 0d,
BeginTime = MapBase.ImageFadeDuration, BeginTime = MapBase.ImageFadeDuration,
Duration = TimeSpan.Zero Duration = TimeSpan.Zero,
FillBehavior = FillBehavior.Stop
}; };
Storyboard.SetTargetProperty(topImageAnimation, nameof(Opacity)); Storyboard.SetTargetProperty(fadeInAnimation, nameof(Opacity));
Storyboard.SetTarget(topImageAnimation, topImage); Storyboard.SetTarget(fadeInAnimation, topImage);
Storyboard.SetTargetProperty(bottomImageAnimation, nameof(Opacity)); Storyboard.SetTargetProperty(fadeOutAnimation, nameof(Opacity));
Storyboard.SetTarget(bottomImageAnimation, bottomImage); Storyboard.SetTarget(fadeOutAnimation, bottomImage);
var storyboard = new Storyboard(); var storyboard = new Storyboard();
storyboard.Children.Add(topImageAnimation); storyboard.Children.Add(fadeInAnimation);
storyboard.Children.Add(bottomImageAnimation); storyboard.Children.Add(fadeOutAnimation);
storyboard.Begin(); storyboard.Begin();
} }
} }

View file

@ -16,24 +16,24 @@ namespace MapControl
{ {
public partial class Tile public partial class Tile
{ {
private void BeginOpacityAnimation() private void BeginFadeInAnimation()
{ {
var animation = new DoubleAnimation var fadeInAnimation = new DoubleAnimation
{ {
From = 0d, From = 0d,
Duration = MapBase.ImageFadeDuration, Duration = MapBase.ImageFadeDuration,
FillBehavior = FillBehavior.Stop FillBehavior = FillBehavior.Stop
}; };
Storyboard.SetTargetProperty(animation, nameof(UIElement.Opacity)); Storyboard.SetTargetProperty(fadeInAnimation, nameof(UIElement.Opacity));
Storyboard.SetTarget(animation, Image); Storyboard.SetTarget(fadeInAnimation, Image);
var storyboard = new Storyboard(); var storyboard = new Storyboard();
storyboard.Children.Add(animation); storyboard.Children.Add(fadeInAnimation);
storyboard.Begin(); storyboard.Begin();
} }
private void AnimateImageOpacity() private void FadeIn()
{ {
if (Image.Source is BitmapImage bitmap && bitmap.UriSource != null) if (Image.Source is BitmapImage bitmap && bitmap.UriSource != null)
{ {
@ -42,7 +42,7 @@ namespace MapControl
} }
else else
{ {
BeginOpacityAnimation(); BeginFadeInAnimation();
} }
} }
@ -53,7 +53,7 @@ namespace MapControl
bitmap.ImageOpened -= BitmapImageOpened; bitmap.ImageOpened -= BitmapImageOpened;
bitmap.ImageFailed -= BitmapImageFailed; bitmap.ImageFailed -= BitmapImageFailed;
BeginOpacityAnimation(); BeginFadeInAnimation();
} }
private void BitmapImageFailed(object sender, ExceptionRoutedEventArgs e) private void BitmapImageFailed(object sender, ExceptionRoutedEventArgs e)