简体   繁体   中英

OpacityMask animation

This wipe animation happens once only. Why can't it be repeated?

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* * ** * ** * ** * *

I did the same test with WPF Image control and got the same result. Animation pass 1 time only.

WHY?

  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>

Have you tried:

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

This way the Offset property will be reset to its original value befor the animation starts again

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM