[flutter] Added additional SkeletonDrawable.renderToXXX() methods for simple offscreen rendering.

This commit is contained in:
Mario Zechner 2022-11-24 13:03:10 +01:00
parent b0c63b8fa8
commit 42186097c4
2 changed files with 15 additions and 3 deletions

View File

@ -67,7 +67,7 @@ class DressUpState extends State<DressUp> {
? 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<DressUp> {
},
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<DressUp> {
void dispose() {
super.dispose();
_drawable?.dispose();
_customSkin?.dispose();
}
}

View File

@ -3308,7 +3308,7 @@ class SkeletonDrawable {
}
}
Future<RawImageData> 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<Uint8List> 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<RawImageData> 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());
}