It is definitely the animation(s).   I can leave my timer loop in place
with no animations and the cpu usage is negligible.

 

I tried to diagnose whether it was the specific property (ScaleX vs
Opacity) but removing one or the other didn't help.

 

Thanks for the code, I'll check it out.

 

 

From: John OBrien [mailto:[email protected]] 
Sent: Monday, 21 September 2009 2:14 PM
To: [email protected]
Subject: RE: animation cpu usage

 

Double check your windowless property like Miguel mentioned, also I had
an issue with the Implicitstylemanager firing continuously in one app,
had to set it to runonce. 

 

I guess the first thing is to identify that indeed it is your animation
causing the CPU usage.

 

 

This is my quick sprite code if you need it in Silverlight2, Jose
mentioned that in SL3 this is now achievable in XAML only, from memory
there is now an instant easing property.

 

XAML:

 

<UserControl x:Class="WebCameraMap.controls.Sprite"

    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"; 

    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml";>

    <Grid x:Name="LayoutRoot">

                </Grid>

</UserControl>

 

CODE:

 

using System;

using System.Windows;

using System.Windows.Media;

using System.Windows.Media.Animation;

using System.Windows.Shapes;

 

namespace WebCameraMap.controls

{

    public partial class Sprite : IDisposable

    {

        private Storyboard sb;

 

        public Sprite()

        {

            InitializeComponent();

            Loaded += Sprite_Loaded;

        }

 

        public int Frames { get; set; }

        public int MilliSecondsPerFrame { get; set; }

        public ImageSource ImageSource { get; set; }

 

        private void Sprite_Loaded(object sender, RoutedEventArgs e)

        {

            Begin();

        }

 

        private void Begin()

        {

            var element = new Rectangle

                              {

                                  Width = Width,

                                  Height = Height

                              };

 

            var spriteSheet = new ImageBrush

                                  {

                                      Stretch = Stretch.None,

                                      AlignmentX = AlignmentX.Left,

                                      AlignmentY = AlignmentY.Top

                                  };

 

            var sprite_sheet_position = new TranslateTransform();

            spriteSheet.Transform = sprite_sheet_position;

            spriteSheet.ImageSource = ImageSource;

 

            element.Fill = spriteSheet;

 

            var sprite_anim = new DoubleAnimationUsingKeyFrames();

            for (int i = 0; i < Frames; i++)

            {

                var frame_span = new TimeSpan(0, 0, 0, 0,
i*MilliSecondsPerFrame);

                sprite_anim.KeyFrames.Add(new DiscreteDoubleKeyFrame

                                              {

                                                  Value =
(-Width*((Frames-i)-1)),

                                                  KeyTime =
KeyTime.FromTimeSpan(frame_span)

                                              });

            }

 

            sb = new Storyboard {RepeatBehavior =
RepeatBehavior.Forever};

            sb.Children.Add(sprite_anim);

            Storyboard.SetTarget(sprite_anim, sprite_sheet_position);

            Storyboard.SetTargetProperty(sprite_anim,

                                         new
PropertyPath(TranslateTransform.XProperty));

            sb.Begin();

 

            LayoutRoot.Children.Add(element);

        }

 

        public void Dispose()

        {

            if (sb !=null)

            {

                sb.Stop();

                sb = null;

            }

        }

    }

}

 

 

From: [email protected]
[mailto:[email protected]] On Behalf Of Ross
Jempson
Sent: Monday, 21 September 2009 2:00 PM
To: [email protected]
Subject: RE: animation cpu usage

 

Thanks John.  

 

I can't see it on his blog but I understand the concept.

 

 In my case, I should create an image that is twice as wide as the
original, and then show/hide the relevant part of the image by animating
the X of a translatetransform.

 

From: John OBrien [mailto:[email protected]] 
Sent: Monday, 21 September 2009 1:43 PM
To: [email protected]
Subject: RE: animation cpu usage

 

Ross, I used Jose Fajardo's sprite concept from Remix to change a high
cpu repeating animation to a simple sprite (set of frames in one image,
repeating animation is just translate x).

It may not be ideal but for me it took the animation down to 1-2% cpu.

 

His blog is here, but I'm not sure if he ever posted about it:

http://www.cynergysystems.com/blogs/page/josefajardo

 

John.

 

From: [email protected]
[mailto:[email protected]] On Behalf Of Ross
Jempson
Sent: Monday, 21 September 2009 12:06 PM
To: [email protected]
Subject: animation cpu usage

 

Hi there,

 

This is an old question I posted when the list was down.

 

Does anyone have any suggestions as to how I can minimise cpu usage when
using a storyboard/animation(s) that repeat constantly.

 

To explain the scenario, I have a screen that has a background that sort
of looks like a night sky.  There are say 100 little stars (the same png
scattered around with various sizes).  I animate the opacity / ScaleX /
ScaleY of a random png every 1 second to create a twinkling effect that
keeps running the whole time you view the screen.

 

The effect is very nice, however I notice it hogs the cpu.

 

I have tried 2 implementations.  

 

In the first implementation I created a storyboard with 3 animations.  I
created a timer that fired every 1 second, and on the fly set the
animations to target one of the pngs randomly (stopping and starting the
storyboard each time).   This was consuming on average 80% of my CPU.

 

I created a second implementation to see if I could improve the
situation.  In the second implementation I decided to build up the
storyboard in code up front.  I create 40 animations up front targeting
random pngs, each with a begintime 1 second later than the previous and
add them all to the one master storyboard.  I then just run that
storyboard over and over.  However, this approach made no difference to
the cpu being consumed.

 

Cheers.


______________________________________________________________________
This email has been scanned by the MessageLabs Email Security System.
For more information please visit http://www.messagelabs.com/email 
______________________________________________________________________


______________________________________________________________________
This email has been scanned by the MessageLabs Email Security System.
For more information please visit http://www.messagelabs.com/email 
______________________________________________________________________

_______________________________________________
ozsilverlight mailing list
[email protected]
http://prdlxvm0001.codify.net/mailman/listinfo/ozsilverlight

Reply via email to