зеркало из https://github.com/mono/SkiaSharp.git
Added an animated gif sample
This commit is contained in:
Родитель
38413b6377
Коммит
e9af1e11ce
|
@ -23,12 +23,25 @@ namespace SkiaSharpSample.FormsSample
|
|||
get { return sample; }
|
||||
set
|
||||
{
|
||||
// clean up the old sample
|
||||
if (sample != null)
|
||||
{
|
||||
sample.RefreshRequested -= OnRefreshRequested;
|
||||
sample.Destroy();
|
||||
}
|
||||
|
||||
sample = value;
|
||||
Title = sample?.Title;
|
||||
|
||||
sample.Init(RefreshSamples);
|
||||
// prepare the sample
|
||||
if (sample != null)
|
||||
{
|
||||
sample.RefreshRequested += OnRefreshRequested;
|
||||
sample.Init();
|
||||
}
|
||||
|
||||
Title = sample.Title;
|
||||
RefreshSamples();
|
||||
// refresh the view
|
||||
OnRefreshRequested(null, null);
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -120,6 +133,11 @@ namespace SkiaSharpSample.FormsSample
|
|||
}
|
||||
}
|
||||
|
||||
private void OnRefreshRequested(object sender, EventArgs e)
|
||||
{
|
||||
RefreshSamples();
|
||||
}
|
||||
|
||||
private void RefreshSamples()
|
||||
{
|
||||
if (canvas.IsVisible)
|
||||
|
|
|
@ -18,6 +18,13 @@ namespace SkiaSharpSample.MacSample
|
|||
|
||||
public void SetSample(SampleBase newSample)
|
||||
{
|
||||
// clean up the old sample
|
||||
if (sample != null)
|
||||
{
|
||||
sample.RefreshRequested -= OnRefreshRequested;
|
||||
sample.Destroy();
|
||||
}
|
||||
|
||||
sample = newSample;
|
||||
|
||||
// set the title
|
||||
|
@ -30,14 +37,18 @@ namespace SkiaSharpSample.MacSample
|
|||
}
|
||||
|
||||
// prepare the sample
|
||||
sample?.Init(() =>
|
||||
if (sample != null)
|
||||
{
|
||||
// refresh the view
|
||||
canvas.SetNeedsDisplayInRect(canvas.Bounds);
|
||||
glview.SetNeedsDisplayInRect(glview.Bounds);
|
||||
});
|
||||
sample.RefreshRequested += OnRefreshRequested;
|
||||
sample.Init();
|
||||
}
|
||||
|
||||
// refresh the view
|
||||
OnRefreshRequested(null, null);
|
||||
}
|
||||
|
||||
private void OnRefreshRequested(object sender, EventArgs e)
|
||||
{
|
||||
canvas.SetNeedsDisplayInRect(canvas.Bounds);
|
||||
glview.SetNeedsDisplayInRect(glview.Bounds);
|
||||
}
|
||||
|
|
|
@ -36,7 +36,7 @@ namespace SkiaSharpSample
|
|||
|
||||
protected abstract void OnDrawSample(SKCanvas canvas, int width, int height);
|
||||
|
||||
public async void Init(Action callback = null)
|
||||
public async void Init()
|
||||
{
|
||||
// reset the matrix for the new sample
|
||||
Matrix = SKMatrix.MakeIdentity();
|
||||
|
@ -47,7 +47,17 @@ namespace SkiaSharpSample
|
|||
|
||||
IsInitialized = true;
|
||||
|
||||
callback?.Invoke();
|
||||
Refresh();
|
||||
}
|
||||
}
|
||||
|
||||
public void Destroy()
|
||||
{
|
||||
if (IsInitialized)
|
||||
{
|
||||
OnDestroy();
|
||||
|
||||
IsInitialized = false;
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -56,6 +66,10 @@ namespace SkiaSharpSample
|
|||
return Task.FromResult(true);
|
||||
}
|
||||
|
||||
protected virtual void OnDestroy()
|
||||
{
|
||||
}
|
||||
|
||||
public void Tap()
|
||||
{
|
||||
if (IsInitialized)
|
||||
|
@ -120,6 +134,13 @@ namespace SkiaSharpSample
|
|||
Title.IndexOf(searchText, StringComparison.OrdinalIgnoreCase) != -1 ||
|
||||
Description.IndexOf(searchText, StringComparison.OrdinalIgnoreCase) != -1;
|
||||
}
|
||||
|
||||
public event EventHandler RefreshRequested;
|
||||
|
||||
protected void Refresh()
|
||||
{
|
||||
RefreshRequested?.Invoke(this, EventArgs.Empty);
|
||||
}
|
||||
}
|
||||
|
||||
public enum GestureState
|
||||
|
|
|
@ -0,0 +1,85 @@
|
|||
using System;
|
||||
using System.Threading;
|
||||
using System.Threading.Tasks;
|
||||
using SkiaSharp;
|
||||
|
||||
namespace SkiaSharpSample.Samples
|
||||
{
|
||||
[Preserve(AllMembers = true)]
|
||||
public class DecodeGifFramesSample : SampleBase
|
||||
{
|
||||
private int currentFrame = 0;
|
||||
private SKCodec codec = null;
|
||||
private SKImageInfo info = SKImageInfo.Empty;
|
||||
private SKBitmap bitmap = null;
|
||||
private CancellationTokenSource cts;
|
||||
private SKCodecFrameInfo[] frames;
|
||||
private TaskScheduler scheduler;
|
||||
|
||||
[Preserve]
|
||||
public DecodeGifFramesSample()
|
||||
{
|
||||
}
|
||||
|
||||
public override string Title => "Decode Gif Frames";
|
||||
|
||||
public override SampleCategories Category => SampleCategories.BitmapDecoding;
|
||||
|
||||
protected override async Task OnInit()
|
||||
{
|
||||
await base.OnInit();
|
||||
|
||||
var stream = new SKManagedStream(SampleMedia.Images.AnimatedHeartGif, true);
|
||||
codec = SKCodec.Create(stream);
|
||||
frames = codec.FrameInfo;
|
||||
|
||||
info = codec.Info;
|
||||
info = new SKImageInfo(info.Width, info.Height, SKImageInfo.PlatformColorType, SKAlphaType.Premul);
|
||||
|
||||
bitmap = new SKBitmap(info);
|
||||
|
||||
cts = new CancellationTokenSource();
|
||||
|
||||
scheduler = TaskScheduler.Current;
|
||||
var loop = Task.Run(async () =>
|
||||
{
|
||||
while (!cts.IsCancellationRequested)
|
||||
{
|
||||
var duration = frames[currentFrame].Duration;
|
||||
if (duration <= 0)
|
||||
duration = 100;
|
||||
|
||||
await Task.Delay(duration, cts.Token);
|
||||
|
||||
// next frame
|
||||
currentFrame++;
|
||||
if (currentFrame >= frames.Length)
|
||||
currentFrame = 0;
|
||||
|
||||
new Task(Refresh).Start(scheduler);
|
||||
}
|
||||
}, cts.Token);
|
||||
}
|
||||
|
||||
protected override void OnDestroy()
|
||||
{
|
||||
base.OnDestroy();
|
||||
|
||||
codec?.Dispose();
|
||||
codec = null;
|
||||
|
||||
cts.Cancel();
|
||||
}
|
||||
|
||||
protected override void OnDrawSample(SKCanvas canvas, int width, int height)
|
||||
{
|
||||
canvas.Clear(SKColors.Black);
|
||||
|
||||
var opts = new SKCodecOptions(currentFrame, false);
|
||||
if (codec?.GetPixels(info, bitmap.GetPixels(), opts) == SKCodecResult.Success)
|
||||
{
|
||||
canvas.DrawBitmap(bitmap, 0, 0);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
|
@ -17,6 +17,7 @@
|
|||
<Compile Include="$(MSBuildThisFileDirectory)SampleBase.cs" />
|
||||
<Compile Include="$(MSBuildThisFileDirectory)SampleMedia.cs" />
|
||||
<Compile Include="$(MSBuildThisFileDirectory)Samples\BitmapAnnotationSample.cs" />
|
||||
<Compile Include="$(MSBuildThisFileDirectory)Samples\DecodeGifFramesSample.cs" />
|
||||
<Compile Include="$(MSBuildThisFileDirectory)Samples\BitmapSubsetDecoderSample.cs" />
|
||||
<Compile Include="$(MSBuildThisFileDirectory)Samples\ManipulatedBitmapShaderSample.cs" />
|
||||
<Compile Include="$(MSBuildThisFileDirectory)Samples\PathMeasureSample.cs" />
|
||||
|
|
|
@ -69,11 +69,29 @@ namespace SkiaSharpSample.TvSample
|
|||
|
||||
private void OnSampleSelected(SampleBase sample)
|
||||
{
|
||||
// clean up the old sample
|
||||
if (currentSample != null)
|
||||
{
|
||||
currentSample.RefreshRequested -= OnRefreshRequested;
|
||||
currentSample.Destroy();
|
||||
}
|
||||
|
||||
// update the selected sample
|
||||
currentSample = sample;
|
||||
currentSample?.Init(canvas.SetNeedsDisplay);
|
||||
|
||||
// refresh the canvas
|
||||
// prepare the sample
|
||||
if (sample != null)
|
||||
{
|
||||
sample.RefreshRequested += OnRefreshRequested;
|
||||
sample.Init();
|
||||
}
|
||||
|
||||
// refresh the view
|
||||
OnRefreshRequested(null, null);
|
||||
}
|
||||
|
||||
private void OnRefreshRequested(object sender, EventArgs e)
|
||||
{
|
||||
canvas.SetNeedsDisplay();
|
||||
}
|
||||
}
|
||||
|
|
|
@ -163,20 +163,31 @@ namespace SkiaSharpSample.UWPSample
|
|||
|
||||
private void SetSample(SampleBase newSample)
|
||||
{
|
||||
// clean up the old sample
|
||||
if (sample != null)
|
||||
{
|
||||
sample.RefreshRequested -= OnRefreshRequested;
|
||||
sample.Destroy();
|
||||
}
|
||||
|
||||
sample = newSample;
|
||||
|
||||
// set the title
|
||||
titleBar.Text = sample?.Title ?? "SkiaSharp for Windows";
|
||||
|
||||
// prepare the sample
|
||||
sample?.Init(() =>
|
||||
if (sample != null)
|
||||
{
|
||||
// refresh the view
|
||||
canvas.Invalidate();
|
||||
glview.Invalidate();
|
||||
});
|
||||
sample.RefreshRequested += OnRefreshRequested;
|
||||
sample.Init();
|
||||
}
|
||||
|
||||
// refresh the view
|
||||
OnRefreshRequested(null, null);
|
||||
}
|
||||
|
||||
private void OnRefreshRequested(object sender, EventArgs e)
|
||||
{
|
||||
canvas.Invalidate();
|
||||
glview.Invalidate();
|
||||
}
|
||||
|
|
|
@ -176,20 +176,31 @@ namespace SkiaSharpSample.WPFSample
|
|||
|
||||
private void SetSample(SampleBase newSample)
|
||||
{
|
||||
// clean up the old sample
|
||||
if (sample != null)
|
||||
{
|
||||
sample.RefreshRequested -= OnRefreshRequested;
|
||||
sample.Destroy();
|
||||
}
|
||||
|
||||
sample = newSample;
|
||||
|
||||
// set the title
|
||||
Title = sample?.Title ?? "SkiaSharp for WPF";
|
||||
|
||||
// prepare the sample
|
||||
sample?.Init(() =>
|
||||
if (sample != null)
|
||||
{
|
||||
// refresh the view
|
||||
canvas.InvalidateVisual();
|
||||
glhost.Child?.Invalidate();
|
||||
});
|
||||
sample.RefreshRequested += OnRefreshRequested;
|
||||
sample.Init();
|
||||
}
|
||||
|
||||
// refresh the view
|
||||
OnRefreshRequested(null, null);
|
||||
}
|
||||
|
||||
private void OnRefreshRequested(object sender, EventArgs e)
|
||||
{
|
||||
canvas.InvalidateVisual();
|
||||
glhost.Child?.Invalidate();
|
||||
}
|
||||
|
|
|
@ -41,7 +41,6 @@ namespace SkiaSharpSample.WindowsSample
|
|||
{
|
||||
// create the sample item
|
||||
var menuItem = new ToolStripMenuItem(sample.Title, null, OnSampleSelected) { Tag = sample };
|
||||
menuItem.Click += OnSampleSelected;
|
||||
menu.DropDownItems.Add(menuItem);
|
||||
}
|
||||
// add to the menu bar
|
||||
|
@ -157,20 +156,31 @@ namespace SkiaSharpSample.WindowsSample
|
|||
|
||||
private void SetSample(SampleBase newSample)
|
||||
{
|
||||
// clean up the old sample
|
||||
if (sample != null)
|
||||
{
|
||||
sample.RefreshRequested -= OnRefreshRequested;
|
||||
sample.Destroy();
|
||||
}
|
||||
|
||||
sample = newSample;
|
||||
|
||||
// set the title
|
||||
Text = sample?.Title ?? "SkiaSharp for Windows";
|
||||
|
||||
// prepare the sample
|
||||
sample?.Init(() =>
|
||||
if (sample != null)
|
||||
{
|
||||
// refresh the view
|
||||
canvas.Invalidate();
|
||||
glview.Invalidate();
|
||||
});
|
||||
sample.RefreshRequested += OnRefreshRequested;
|
||||
sample.Init();
|
||||
}
|
||||
|
||||
// refresh the view
|
||||
OnRefreshRequested(null, null);
|
||||
}
|
||||
|
||||
private void OnRefreshRequested(object sender, EventArgs e)
|
||||
{
|
||||
canvas.Invalidate();
|
||||
glview.Invalidate();
|
||||
}
|
||||
|
|
Загрузка…
Ссылка в новой задаче