2012-12-11 135 views
0

此擦除动画只发生一次。 为什么不能重复?不透明度蒙版动画

XAML

<Grid> 
     <MediaElement Source="" x:Name="Player1" Stretch="Fill" /> 
</Grid> 

C#

DispatcherTimer timer = new DispatcherTimer(priority: DispatcherPriority.Background); 

    public MainWindow() 
    { 
       InitializeComponent(); 
       timer.Tick += timer_Tick; 
       timer.Interval = TimeSpan.FromSeconds(10); 
       timer.Start(); 

       Loaded += MainWindow_Loaded; 
       Player1.LoadedBehavior = MediaState.Manual; 

       Player1.Source = new Uri(@"C:\Temp\3.mov"); 
    } 

    void timer_Tick(object sender, EventArgs e) 
    { 

       Player1.Stop(); 
       Player1.Play(); 
       WipeAnimation(Player1); 
    } 

    GradientStop BlackStop = new GradientStop(Colors.Black, 0); 
    GradientStop TransparentStop = new GradientStop(Colors.Transparent, 0); 

    public void WipeAnimation(FrameworkElement ObjectToAnimate) 
    { 
       ObjectToAnimate.OpacityMask = null; 
       LinearGradientBrush OpacityBrush = new LinearGradientBrush(); 

       OpacityBrush.GradientStops.Add(BlackStop); 
       OpacityBrush.GradientStops.Add(TransparentStop); 

       ObjectToAnimate.OpacityMask = OpacityBrush; 

       Duration d = TimeSpan.FromSeconds(4); 
       Storyboard sb = new Storyboard() { Duration = d }; 
       DoubleAnimation DA = new DoubleAnimation() { By = 1, Duration = d }; 
       DoubleAnimation DA2 = new DoubleAnimation() { By = 1, Duration = d }; 
       this.RegisterName("TransparentStop", TransparentStop); 
       this.RegisterName("BlackStop", BlackStop); 

       sb.Children.Clear(); 
       sb.Children.Add(DA); sb.Children.Add(DA2); 
       Storyboard.SetTargetName(DA, "TransparentStop"); 
       Storyboard.SetTargetName(DA2, "BlackStop"); 
       Storyboard.SetTargetProperty(DA, new PropertyPath("Offset")); 
       Storyboard.SetTargetProperty(DA2, new PropertyPath("Offset")); 
       sb.Completed += sb_Completed; 
       sb.Begin(this); 
    } 

    void sb_Completed(object sender, EventArgs e) 
    { 
    } 

** * UPDATE * * ** * ** * ** * *

我做了WPF Image控件同样的试验,得到了相同的结果。动画仅传递一次。

为什么?

public partial class MainWindow : Window 
    { 
     DispatcherTimer timer = new DispatcherTimer(DispatcherPriority.Background); 
     GradientStop BlackStop = new GradientStop(Colors.Black, 0); 
     GradientStop TransparentStop = new GradientStop(Colors.Transparent, 0); 

     public MainWindow() 
     { 
      InitializeComponent(); 

      imgFace.CacheMode = new BitmapCache(); 


      this.RegisterName("TransparentStop", TransparentStop); 
      this.RegisterName("BlackStop", BlackStop); 

      timer.Interval = TimeSpan.FromSeconds(10); 
      timer.Tick += timer_Tick; 
      timer.Start(); 


      LinearGradientBrush OpacityBrushReset = new LinearGradientBrush(); 
     GradientStop TransparentStopReset = new GradientStop(Colors.Transparent, 0); 
     OpacityBrushReset.GradientStops.Add(TransparentStopReset); 
     imgFace.OpacityMask = OpacityBrushReset; 
     } 

     void timer_Tick(object sender, EventArgs e) 
     {   
      WipeAnimation(imgFace); 
      Debug.WriteLine("A"); 
     } 

     public void WipeAnimation(FrameworkElement ObjectToAnimate) 
     { 
      LinearGradientBrush OpacityBrush = new LinearGradientBrush(); 
      OpacityBrush.StartPoint = new Point(1, 0); 
      OpacityBrush.EndPoint = new Point(0, 0); 

      OpacityBrush.GradientStops.Add(BlackStop); 
      OpacityBrush.GradientStops.Add(TransparentStop); 
      ObjectToAnimate.OpacityMask = OpacityBrush;   

      Duration d = TimeSpan.FromSeconds(4); 
      Storyboard sb = new Storyboard() { Duration = d }; 
      DoubleAnimation DA = new DoubleAnimation() { By = 1, Duration = d }; 
      DoubleAnimation DA2 = new DoubleAnimation() { By = 1, Duration = d }; 
      sb.Children.Add(DA); sb.Children.Add(DA2); 
      Storyboard.SetTargetName(DA, "TransparentStop"); 
      Storyboard.SetTargetName(DA2, "BlackStop"); 
      Storyboard.SetTargetProperty(DA, new PropertyPath("Offset")); 
      Storyboard.SetTargetProperty(DA2, new PropertyPath("Offset")); 
      sb.Completed += sb_Completed; 
      sb.Begin(this); 
     } 

     void sb_Completed(object sender, EventArgs e) 
     { 
      imgFace.OpacityMask = null; 
     } 
    } 



<Grid> 

    <Image x:Name="imgFace" Source="face_avril.jpg" Stretch="Fill" HorizontalAlignment="Left" Height="176" Margin="24,34,0,0" VerticalAlignment="Top" Width="319"> 
     <!--<Image.OpacityMask> 
      <LinearGradientBrush StartPoint="0,0" EndPoint="1,0"> 
       <GradientStop Offset="0" Color="Black" x:Name="BlackStop"/> 
       <GradientStop Offset="0" Color="Transparent" x:Name="TransparentStop"/> 
      </LinearGradientBrush> 
      </Image.OpacityMask>--> 
    </Image> 
</Grid> 

回答

1

你试过:

DoubleAnimation DA = new DoubleAnimation() { From = 0, To = 1, Duration = d }; 
DoubleAnimation DA2 = new DoubleAnimation() { From = 0, To = 1, Duration = d }; 

这样的Offset属性将被重置为初始值以前生产的动画重新开始

+0

1 0000000谢谢你,哥哥!!!! –