From 42186097c42d2d878d53835075bbee41233d8fe7 Mon Sep 17 00:00:00 2001 From: Mario Zechner Date: Thu, 24 Nov 2022 13:03:10 +0100 Subject: [PATCH] [flutter] Added additional SkeletonDrawable.renderToXXX() methods for simple offscreen rendering. --- spine-flutter/example/lib/dress_up.dart | 5 +++-- spine-flutter/lib/spine_flutter.dart | 13 ++++++++++++- 2 files changed, 15 insertions(+), 3 deletions(-) diff --git a/spine-flutter/example/lib/dress_up.dart b/spine-flutter/example/lib/dress_up.dart index 559d1df53..5e2962757 100644 --- a/spine-flutter/example/lib/dress_up.dart +++ b/spine-flutter/example/lib/dress_up.dart @@ -67,7 +67,7 @@ class DressUpState extends State { ? const SizedBox() : Row( children: [ - Container(width: thumbnailSize, child: + SizedBox(width: thumbnailSize, child: ListView( children: _skinImages.keys.map((skinName) { var rawImageData = _skinImages[skinName]!; @@ -80,7 +80,7 @@ class DressUpState extends State { }, child: _selectedSkins[skinName] == true ? box - : ColorFiltered(colorFilter: ColorFilter.mode(Colors.grey, painting.BlendMode.saturation,), child: box) + : ColorFiltered(colorFilter: const ColorFilter.mode(Colors.grey, painting.BlendMode.saturation,), child: box) ); }).toList() ), @@ -97,5 +97,6 @@ class DressUpState extends State { void dispose() { super.dispose(); _drawable?.dispose(); + _customSkin?.dispose(); } } \ No newline at end of file diff --git a/spine-flutter/lib/spine_flutter.dart b/spine-flutter/lib/spine_flutter.dart index 95182b325..31fb47c2f 100644 --- a/spine-flutter/lib/spine_flutter.dart +++ b/spine-flutter/lib/spine_flutter.dart @@ -3308,7 +3308,7 @@ class SkeletonDrawable { } } - Future renderToRawImageData(double width, double height) async { + PictureRecorder renderToPictureRecorder(double width, double height) { var bounds = skeleton.getBounds(); var scale = 1 / (bounds.width > bounds.height ? bounds.width / width : bounds.height / height); @@ -3324,6 +3324,17 @@ class SkeletonDrawable { canvas.translate(-(bounds.x + bounds.width / 2), -(bounds.y + bounds.height / 2)); canvas.drawRect(const Rect.fromLTRB(-5, -5, 5, -5), paint..color = material.Colors.red); renderToCanvas(canvas); + return recorder; + } + + Future renderToPng(double width, double height) async { + final recorder = renderToPictureRecorder(width, height); + final image = await recorder.endRecording().toImage(width.toInt(), height.toInt()); + return (await image.toByteData(format: ImageByteFormat.png))!.buffer.asUint8List(); + } + + Future renderToRawImageData(double width, double height) async { + final recorder = renderToPictureRecorder(width, height); var rawImageData = (await (await recorder.endRecording().toImage(width.toInt(), height.toInt())).toByteData(format: ImageByteFormat.rawRgba))!.buffer.asUint8List(); return RawImageData(rawImageData, width.toInt(), height.toInt()); }