亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

如何制作改變背景圖片的動畫?

如何制作改變背景圖片的動畫?

C#
蝴蝶刀刀 2023-08-20 10:09:37
我正在使用 UWP 開發 Microsoft Store 應用程序,并且使用 XAML 和 C#。我想用不透明動畫隨機更改背景圖像。我的代碼在下面。該函數執行與Task.Run(InitializeWorks);   private async void InitializeWorks()    {        await Dispatcher.RunAsync(Windows.UI.Core.CoreDispatcherPriority.Normal, async () =>        {            BackgroundImage.Opacity = 0;            try            {                while (true)                {                    var backgroundImageFolder = await Windows.ApplicationModel.Package.Current.InstalledLocation.GetFolderAsync(@"Assets\BackgroundImages");                    var backgroundImageFiles = await backgroundImageFolder.GetFilesAsync();                    BackgroundImage.Source = new BitmapImage(new Uri(backgroundImageFiles[new Random().Next(0, backgroundImageFiles.Count)].Path));                    for (double i = BackgroundImage.Opacity; i <= 0.1; i += 0.001)                    {                        BackgroundImage.Opacity = i;                        await Task.Delay(10);                    }                    await Task.Delay(5000);                    for (double i = BackgroundImage.Opacity; i >= 0; i -= 0.001)                    {                        BackgroundImage.Opacity = i;                        await Task.Delay(10);                    }                }            }            catch (Exception e)            {                //            }        });    }我可以使用這段代碼的性能如何?這個異步任務有什么不便嗎?會出現什么錯誤嗎?如何用XAML制作變化的動畫?
查看完整描述

3 回答

?
蕭十郎

TA貢獻1815條經驗 獲得超13個贊


帶有故事板的 XML 動畫;


? ? <Storyboard x:Name="BackgroundImageStoryboardIncrease">

? ? ? ? <DoubleAnimation

? ? ? ? ? ? Storyboard.TargetName="BackgroundImage"

? ? ? ? ? ? Storyboard.TargetProperty="Opacity"

? ? ? ? ? ? From="0"

? ? ? ? ? ? To="0.15"

? ? ? ? ? ? Duration="0:0:2" />

? ? </Storyboard>

? ? <Storyboard x:Name="BackgroundImageStoryboardDecrease">

? ? ? ? <DoubleAnimation

? ? ? ? ? ? Storyboard.TargetName="BackgroundImage"

? ? ? ? ? ? Storyboard.TargetProperty="Opacity"

? ? ? ? ? ? From="0.15"

? ? ? ? ? ? To="0"

? ? ? ? ? ? Duration="0:0:2" />

? ? </Storyboard>

C# 更改事件并等待 10 秒:


? ? private async void InitializeFrontWorks()

? ? {

? ? ? ? // Animations

? ? ? ? DispatcherTimer dispatcherTimer = new DispatcherTimer

? ? ? ? {

? ? ? ? ? ? Interval = TimeSpan.FromSeconds(10)

? ? ? ? };

? ? ? ? var backgroundImageFolder = await Windows.ApplicationModel.Package.Current.InstalledLocation.GetFolderAsync(@"Assets\BackgroundImages");

? ? ? ? var backgroundImageFiles = await backgroundImageFolder.GetFilesAsync();

? ? ? ? AnimationBackgroundImage(backgroundImageFiles);

? ? ? ? dispatcherTimer.Tick += delegate

? ? ? ? {

? ? ? ? ? ? AnimationBackgroundImage(backgroundImageFiles);

? ? ? ? };

? ? ? ? dispatcherTimer.Start();

? ? }

? ? private void AnimationBackgroundImage(IReadOnlyList<StorageFile> backgroundImageFiles)

? ? {

? ? ? ? BackgroundImageStoryboardDecrease.Begin();

? ? ? ? BackgroundImageStoryboardDecrease.Completed += delegate

? ? ? ? {

? ? ? ? ? ? BackgroundImage.Source = new BitmapImage(new Uri(backgroundImageFiles[new Random().Next(0, backgroundImageFiles.Count)].Path));

? ? ? ? ? ? BackgroundImageStoryboardIncrease.Begin();

? ? ? ? };

? ? }


查看完整回答
反對 回復 2023-08-20
?
HUX布斯

TA貢獻1876條經驗 獲得超6個贊

您可以在 XAML 中設置以下動畫:


<Page.Resources>

<Storyboard x:Name="MyStoryboard">

            <DoubleAnimationUsingKeyFrames Storyboard.TargetProperty="(UIElement.Opacity)" Storyboard.TargetName="BackgroundImage">

                <EasingDoubleKeyFrame KeyTime="0" Value="0"/>

                <EasingDoubleKeyFrame KeyTime="0:0:3" Value="0.1"/>

                <EasingDoubleKeyFrame KeyTime="0:0:6" Value="0"/>

            </DoubleAnimationUsingKeyFrames>

        </Storyboard>

</Page.Resources>

隱藏代碼:


可以使用StoryBoard.Begin來啟動Opacity動畫,動畫有Completed事件??梢杂嗛喫鼇肀O聽動畫是否完成。當動畫完成后,可以調用InitializeWorks()方法再次更改BackgroundImage。這種寫法替換您的While方法。


private async void InitializeWorks()

        {

            await Dispatcher.RunAsync(Windows.UI.Core.CoreDispatcherPriority.Normal, async () =>

            {

                BackgroundImage.Opacity = 0;

                try

                {

                   

                    var backgroundImageFolder = await Windows.ApplicationModel.Package.Current.InstalledLocation.GetFolderAsync(@"Assets\BackgroundImages");

                    var backgroundImageFiles = await backgroundImageFolder.GetFilesAsync();

                    BackgroundImage.Source = new BitmapImage(new Uri(backgroundImageFiles[new Random().Next(0, backgroundImageFiles.Count)].Path));

                    MyStoryboard.Begin();

                    MyStoryboard.Completed += MyStoryboard_Completed;

                }

                catch (Exception e)

                {

                    //

                }

            });

        }


private void MyStoryboard_Completed(object sender, object e)

        {

            InitializeWorks();

        }


查看完整回答
反對 回復 2023-08-20
?
至尊寶的傳說

TA貢獻1789條經驗 獲得超10個贊

您無法在 XAML 中調用GetFolderAsyncGetFilesAsync,但我建議您將循環替換while為定期更改背景的DispatcherTimer 。

Tick事件將在 UI 線程上引發,這意味著可以安全地在事件處理程序中訪問 UI 元素。異步方法不會阻塞。


查看完整回答
反對 回復 2023-08-20
  • 3 回答
  • 0 關注
  • 190 瀏覽

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號