合成-为什么我的元素立即在动画上设置“模糊”?

时间:2019-06-14 13:30:47

标签: uwp windows-composition-api xaml-composition

我的示例应用程序具有以下Xaml来演示该问题:

<Page x:Class="App1.MainPage"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:local="using:App1"
    xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
    xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
    mc:Ignorable="d"
    Background="#F5DEB3">

    <Border x:Name="MyBorder"
            HorizontalAlignment="Center"
            VerticalAlignment="Center">
        <Image Stretch="Uniform"
            Width="165"
            Height="100"
            Source="/Assets/abstract.png" />
    </Border>
</Page>

这是我的代码背后:

using Windows.UI.Composition;
using Windows.UI.Xaml;
using Windows.UI.Xaml.Controls;
using Windows.UI.Xaml.Hosting;

namespace App1
{
    /// <summary>
    /// An empty page that can be used on its own or navigated to within a Frame.
    /// </summary>
    public sealed partial class MainPage : Page
    {
        public MainPage()
        {
            this.InitializeComponent();

            this.Loaded += MainPage_Loaded;
        }

        private void MainPage_Loaded(object sender, RoutedEventArgs e)
        {
            var scalar = Window.Current.Compositor.CreateScalarKeyFrameAnimation();

            scalar.InsertKeyFrame(1.0f, 24f);
            scalar.DelayTime = TimeSpan.FromMilliseconds(1500);
            scalar.Duration = TimeSpan.FromMilliseconds(3000);

            var effect = new GaussianBlurEffect()
            {
                Name = "Blur",
                Source = new CompositionEffectSourceParameter("Backdrop"),
                BorderMode = EffectBorderMode.Hard,
            };

            var effectFactory = Window.Current.Compositor.CreateEffectFactory(effect, new[] { "Blur.BlurAmount" });
            var brush = effectFactory.CreateBrush();
            var sprite = Window.Current.Compositor.CreateSpriteVisual();

            sprite.Brush = brush;
            sprite.Size = new Vector2((float)MyBorder.ActualWidth, (float)MyBorder.ActualHeight);

            ElementCompositionPreview.SetElementChildVisual(MyBorder, sprite);

            var destinationBrush = Window.Current.Compositor.CreateBackdropBrush();
            brush.SetSourceParameter("Backdrop", destinationBrush);

            brush.StartAnimation("Blur.BlurAmount", scalar);
        }
    }
}

应用运行时,应该等待1.5秒钟,然后开始模糊图像。但是,在运行代码时,图像上已经设置了初始模糊(轻微模糊)。有人知道为什么吗?

2 个答案:

答案 0 :(得分:1)

稍微更改一下代码。试试这个。我不知道为什么需要将BlurAmount设置为零的原因,但是也许新的GaussianBlurEffect已经将该值预置了其他值。

var effect = new GaussianBlurEffect()
{
  Name = "Blur",
   Source = new CompositionEffectSourceParameter("Backdrop"),
   BorderMode = EffectBorderMode.Hard,
   BlurAmount = 0,
};

答案 1 :(得分:0)

有关此问题的更多信息,其行为的原因以及上述答案中以BlurAmount = 0开头的原因是因为BlurAmount的默认值为3.0f:

https://microsoft.github.io/Win2D/html/P_Microsoft_Graphics_Canvas_Effects_GaussianBlurEffect_BlurAmount.htm