There was a question last week in the discussion group about animated bitmaps. The main problem was with chaining single bitmaps from resources into one animation. In one of my old projects I was using simple image strip, where frames were stored in the single image. I think, this might be useful also in the .NET Micro Framework.

Heating spiral 560x100 Animation strip

The AnimatedIcon class is very simple and it’s constructor takes three arguments. First is the bitmap strip, second is the width of one frame and third argument is animation interval. Jan Kučera also came with solution for animated icons for .NET Micro Framework. His class enables starting and stopping of animation.

I believe that usage of AnimatedIcon is pretty straight-forward. The only issue I see is the speed of animation for large images.

mainWindow = new Window();
mainWindow.Height = SystemMetrics.ScreenHeight;
mainWindow.Width = SystemMetrics.ScreenWidth;

AnimatedIcon image = new AnimatedIcon(Resources.GetBitmap(Resources.BitmapResources.spirala),
                                      70,   // Width of one frame
                                      200); // Animation interval
mainWindow.Child = image;
public class AnimatedIcon : UIElement
{
    /// <summary>
    /// Creates new instance of AnimatedIcon
    /// </summary>
    /// <param name="bitmap">Source bitmap strip</param>
    /// <param name="width">Width of one frame</param>
    /// <param name="interval">Animation interval in miliseconds</param>
    public AnimatedIcon(Bitmap bitmap, int width, int interval)
    {
        _bitmap = bitmap;
        _width = width;            
        _totalCount = (bitmap.Width / _width);
        _timer = new DispatcherTimer(this.Dispatcher);
        _timer.Interval = new TimeSpan(0, 0, 0, 0, interval);
        _timer.IsEnabled = true;
        _timer.Start();
        _timer.Tick += new EventHandler(_timer_Tick);
    }

    void _timer_Tick(object sender, EventArgs e)
    {
        if (_index < _totalCount - 1)
            _index++;
        else
            _index = 0;

        this.Invalidate();
    }

    public override void OnRender(Microsoft.SPOT.Presentation.Media.DrawingContext dc)
    {
         dc.DrawImage(_bitmap,0, 0, _index * _width, 0, _width, _bitmap.Height);
         base.OnRender(dc);
    }

    private DispatcherTimer _timer;
    private Bitmap _bitmap;        
    private int _width, _index, _totalCount;
}