Table of Contents

Class SkiaSprite

Namespace
DrawnUi.Controls
Assembly
DrawnUi.Maui.dll

Renders animated sprite sheets by subclassing AnimatedFramesRenderer

public class SkiaSprite : AnimatedFramesRenderer, INotifyPropertyChanged, IEffectControlProvider, IToolTipElement, IContextFlyoutElement, IAnimatable, IVisualElementController, IElementController, IView, ITransform, IElement, IVisualTreeElement, IContainer, IList<IView>, ICollection<IView>, IEnumerable<IView>, IEnumerable, ISkiaGestureListener, IHasAfterEffects, ISkiaControl, IDrawnBase, ICanBeUpdatedWithContext, ICanBeUpdated, ISkiaDisposable, IDisposable
Inheritance
SkiaSprite
Implements
Inherited Members
Extension Methods

Constructors

SkiaSprite()

For standalone use

public SkiaSprite()

SkiaSprite(SkiaImage)

For building custom controls

public SkiaSprite(SkiaImage display)

Parameters

display SkiaImage

Existing SkiaImage to use for display

Fields

AnimationNameProperty

Bindable property for AnimationName

public static readonly BindableProperty AnimationNameProperty

Field Value

BindableProperty

CachedSpriteSheets

Cache for loaded spritesheets to avoid reloading the same image multiple times

public static ConcurrentDictionary<string, SKBitmap> CachedSpriteSheets

Field Value

ConcurrentDictionary<string, SKBitmap>

ColumnsProperty

Bindable property for Columns

public static readonly BindableProperty ColumnsProperty

Field Value

BindableProperty

CurrentFrameProperty

Bindable property for CurrentFrame

public static readonly BindableProperty CurrentFrameProperty

Field Value

BindableProperty

FrameSequenceProperty

Bindable property for FrameSequence

public static readonly BindableProperty FrameSequenceProperty

Field Value

BindableProperty

FramesPerSecondProperty

Bindable property for FramesPerSecond

public static readonly BindableProperty FramesPerSecondProperty

Field Value

BindableProperty

MaxFramesProperty

Bindable property for MaxFrames

public static readonly BindableProperty MaxFramesProperty

Field Value

BindableProperty

RegisteredAnimations

Dictionary of named animation sequences that can be reused

public static ConcurrentDictionary<string, int[]> RegisteredAnimations

Field Value

ConcurrentDictionary<string, int[]>

RowsProperty

Bindable property for Rows

public static readonly BindableProperty RowsProperty

Field Value

BindableProperty

SourceProperty

Bindable property for Source

public static readonly BindableProperty SourceProperty

Field Value

BindableProperty

Properties

AnimationName

Name of a predefined animation sequence to use

public string AnimationName { get; set; }

Property Value

string

Columns

Number of columns in the spritesheet

public int Columns { get; set; }

Property Value

int

CurrentFrame

Current frame being displayed

public int CurrentFrame { get; set; }

Property Value

int

Display

Internal SkiaImage control used to display the current frame

public SkiaImage Display { get; protected set; }

Property Value

SkiaImage

DurationMs

Duration of the animation in milliseconds

public double DurationMs { get; protected set; }

Property Value

double

FrameDurationMs

Duration of each frame in milliseconds

public double FrameDurationMs { get; protected set; }

Property Value

double

FrameHeight

Height of each frame in pixels

public int FrameHeight { get; protected set; }

Property Value

int

FrameSequence

Optional custom sequence of frames to play from the spritesheet. If set, this overrides the default sequential frame playback.

public int[] FrameSequence { get; set; }

Property Value

int[]

FrameWidth

Width of each frame in pixels

public int FrameWidth { get; protected set; }

Property Value

int

FramesPerSecond

Frames per second for the animation

public double FramesPerSecond { get; set; }

Property Value

double

MaxFrames

Maximum number of frames to use from the spritesheet (0 means use all)

public int MaxFrames { get; set; }

Property Value

int

Rows

Number of rows in the spritesheet

public int Rows { get; set; }

Property Value

int

Source

Source of the spritesheet image

public string Source { get; set; }

Property Value

string

SpriteSheet

The full sprite sheet bitmap

public SKBitmap SpriteSheet { get; protected set; }

Property Value

SKBitmap

TotalFrames

Total number of frames in the spritesheet

public int TotalFrames { get; protected set; }

Property Value

int

Methods

ApplySpeed()

Applies the current speed ratio to the animator

protected override void ApplySpeed()

CheckCanStartAnimator()

Checks if the animator can start

protected override bool CheckCanStartAnimator()

Returns

bool

True if animator can start

ClearCache()

Clears the spritesheet cache completely

public static void ClearCache()

CreateAnimationSequence(string, int[])

Creates a named animation from a frame sequence

public static int[] CreateAnimationSequence(string name, int[] frameSequence)

Parameters

name string

Name of the animation

frameSequence int[]

Array of frame indices

Returns

int[]

The frame sequence array

GetAnimatorParent()

Gets the parent control for animator operations

protected override SkiaControl GetAnimatorParent()

Returns

SkiaControl

The control to use as animator parent

GetFrameNumberFromTime(double)

Gets frame number from time based on current frame rate

protected int GetFrameNumberFromTime(double msTime)

Parameters

msTime double

Time in milliseconds

Returns

int

Frame number

InvalidateMeasure()

Updates the layout of the Display when measurement needs to be invalidated

protected override void InvalidateMeasure()

LayoutDisplay()

Updates the layout options for the Display based on auto-sizing settings

protected virtual void LayoutDisplay()

LoadLocalImageAsync(string)

Loads a spritesheet image from a local or embedded resource

public Task<SKBitmap> LoadLocalImageAsync(string source)

Parameters

source string

Path to the image

Returns

Task<SKBitmap>

SKBitmap of the loaded spritesheet

LoadSourceAsync(string)

Loads a spritesheet image from a source URL, using cache when available

public Task<SKBitmap> LoadSourceAsync(string source)

Parameters

source string

URL of the spritesheet

Returns

Task<SKBitmap>

SKBitmap of the loaded spritesheet

OnAnimatorInitializing()

Initializes the animator with the correct timing values

protected override void OnAnimatorInitializing()

OnAnimatorSeeking(double)

Called by Seek

protected override void OnAnimatorSeeking(double time)

Parameters

time double

Time position to seek to

OnAnimatorUpdated(double)

Invoked by internal animator

protected override void OnAnimatorUpdated(double value)

Parameters

value double

Current animation time value

OnDisposing()

Handles cleanup when the control is being disposed

public override void OnDisposing()

Paint(DrawingContext)

This is the main drawing routine you should override to draw something. Base one paints background color inside DrawingRect that was defined by Arrange inside base.Draw. Pass arguments if you want to use some time-frozen data for painting at any time from any thread..

protected override void Paint(DrawingContext ctx)

Parameters

ctx DrawingContext

RecalculateFrames()

Recalculates frame information based on current properties

protected void RecalculateFrames()

ReloadSource()

Reloads the sprite sheet from the Source property

public virtual void ReloadSource()

RemoveFromCache(string)

Removes a specific item from the spritesheet cache

public static void RemoveFromCache(string key)

Parameters

key string

The source key to remove

RenderFrame(DrawingContext)

Renders the current frame of the animation

protected override void RenderFrame(DrawingContext ctx)

Parameters

ctx DrawingContext

Drawing context

SetCurrentFrame(int)

Sets the current frame and updates the display

protected void SetCurrentFrame(int frameNumber)

Parameters

frameNumber int

Frame number to display

SetSpriteSheet(SKBitmap, bool)

Sets the sprite sheet bitmap and initializes the animation

public void SetSpriteSheet(SKBitmap bitmap, bool disposePrevious)

Parameters

bitmap SKBitmap

Spritesheet bitmap

disposePrevious bool

If true, disposes previous bitmap

Start(int)

Starts the animation with optional delay

public override void Start(int delayMs = 0)

Parameters

delayMs int

Delay in milliseconds before starting

Update()

Main method to invalidate cache and invoke rendering

public override void Update()