【问题标题】:image transition图像过渡
【发布时间】:2011-06-07 13:59:23
【问题描述】:

我又卡住了。

我正在用 C# 创建一个 WPF 项目...

我有一张图片(专辑封面),我将在后面的代码中进行更改,并希望基本上执行以下操作。

确定并获取新专辑封面图像后,我希望图像控件中的当前图像淡出,使用新封面进行更新,然后再淡入。

我没有看到很多关于如何在后面的代码中实现这一点的好例子。

以下是我最近一次失败的尝试...

if (currentTrack != previousTrack) 
{
    BitmapImage image = new BitmapImage();
    image.BeginInit();
    image.CacheOption = BitmapCacheOption.OnLoad;
    image.CreateOptions = BitmapCreateOptions.IgnoreImageCache;
    image.UriSource = new Uri(Address, UriKind.Absolute);
    image.EndInit();

    Storyboard MyStoryboard = new Storyboard();

    DoubleAnimation FadeOut = new DoubleAnimation();
    FadeOut.From = 1.0;
    FadeOut.To = 0.0;
    FadeOut.Duration = new Duration(TimeSpan.FromSeconds(.5));

    MyStoryboard.Children.Add(FadeOut);
    Storyboard.SetTargetName(FadeOut, CoverArt.Name);
    Storyboard.SetTargetProperty(FadeOut, new PropertyPath(Rectangle.OpacityProperty));

    CoverArt.Source = image;

    DoubleAnimation Fadein = new DoubleAnimation();
    Fadein.From = 0.0;
    Fadein.To = 1.0;
    Fadein.Duration = new Duration(TimeSpan.FromSeconds(.5));

    MyStoryboard.Children.Add(Fadein);
    Storyboard.SetTargetName(Fadein, CoverArt.Name);
    Storyboard.SetTargetProperty(Fadein, new PropertyPath(Rectangle.OpacityProperty));

    MyStoryboard.Begin(this);
}

我更喜欢在后面的代码中执行此操作,因为这是我获取图像的地方。否则,我不确定如何触发它。

一个例子将不胜感激。

【问题讨论】:

  • 这是 Windows 窗体或 ASP.NET Web 窗体或 MVC 还是 ...?
  • 对不起,这是一个使用 C# 代码的 WPF 项目。

标签: c# wpf image transition storyboard


【解决方案1】:

如果您希望旧图像淡出,然后新图像使用相同的图像目标淡入,那么您必须等待第一个情节提要完成,然后才能更改图像并开始新的情节。为此,请使用Storyboard.Completed 事件。

另一方面,如果您希望旧图像淡出新图像淡入,您需要更改您的设计以包含两个占用相同屏幕空间的图像,并且将您的故事板更改为使用ParallelTimeline

【讨论】:

  • 对不起,但我想我需要逐步完成这个,因为目前,我已经加载了第一张图片,并且在情节提要淡出第一张图片之前加载了第二张图片。它在代码中向前跳跃。
【解决方案2】:

能够弄清楚瑞克的意思。我确实使用下面的代码使图像淡出、替换和淡入......

        private void CoverArt_MouseDown(object sender, MouseButtonEventArgs e)
    {
        Storyboard AnimFadeOut = new Storyboard();
        AnimFadeOut.Completed += new EventHandler(ImageFadeOut_Completed);

        DoubleAnimation FadeOut = new DoubleAnimation();
        FadeOut.From = 1.0;
        FadeOut.To = 0.0;
        FadeOut.Duration = new Duration(TimeSpan.FromSeconds(.5));

        AnimFadeOut.Children.Add(FadeOut);
        Storyboard.SetTargetName(FadeOut, CurrentCover.Name);
        Storyboard.SetTargetProperty(FadeOut, new PropertyPath(Image.OpacityProperty));


        AnimFadeOut.Begin(this);
    }

    void ImageFadeOut_Completed(object sender, EventArgs e)
    {
        BitmapImage image = new BitmapImage();
        image.BeginInit();
        image.CacheOption = BitmapCacheOption.OnLoad;
        image.CreateOptions = BitmapCreateOptions.IgnoreImageCache;
        image.UriSource = new Uri("Minogue.jpg", UriKind.Relative);
        image.EndInit();

        CurrentCover.Source = image;
        Storyboard ImageFadeIn = new Storyboard();

        DoubleAnimation FadeIn = new DoubleAnimation();
        FadeIn.From = 0.0;
        FadeIn.To = 1.0;
        FadeIn.Duration = new Duration(TimeSpan.FromSeconds(.5));

        ImageFadeIn.Children.Add(FadeIn);
        Storyboard.SetTargetName(FadeIn, CurrentCover.Name);
        Storyboard.SetTargetProperty(FadeIn, new PropertyPath(Image.OpacityProperty));
        ImageFadeIn.Begin(this);
    }

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2021-05-04
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多