浏览代码

优化player

gavin.chen 2 年之前
父节点
当前提交
9779cdee84

+ 3 - 3
lib/pages/canvas_player/view.dart

@@ -38,7 +38,7 @@ class _CanvasPlayerViewState extends State<CanvasPlayerView> {
         if (value == null) {
           // TOOO: add log
         } else {
-          playerController.setLoadState(value != null);
+          playerController.setLoadState(true);
           Future.delayed(const Duration(milliseconds: 100), () {
             playerController.play();
           });
@@ -82,8 +82,8 @@ class _CanvasPlayerViewState extends State<CanvasPlayerView> {
                 },
                 child: const Text('Pause')),
             SizedBox(
-              width: 500,
-              height: 500,
+              width: 600,
+              height: 600,
               child: RepaintBoundary(
                 child: VidCanvasPlayer(
                   _playerController as VidPlayerController,

+ 11 - 11
lib/pages/canvas_player/widgets/canvas_player.dart

@@ -63,6 +63,9 @@ class _VidCanvasPlayerState extends State<VidCanvasPlayer> {
   }
 
   void loadFrame(Uint8List bytes) async {
+    // List<StackTrace>? debugList = image?.debugGetOpenHandleStackTraces();
+    // print("${DateTime.now()} Load frame \n ${debugList.toString()}");
+    // image?.dispose();
     image = await decodeImageFromList(bytes);
     setState(() {});
   }
@@ -76,25 +79,21 @@ class _VidCanvasPlayerState extends State<VidCanvasPlayer> {
     Widget? child;
     switch (widget.controller.status) {
       case VidPlayStatus.init:
-        child = Container(child: const Text("Loading"));
+        child = const Text("Loading");
         break;
       case VidPlayStatus.ready:
-        child = Container(child: const Text("Ready"));
+        child = const Text("Ready");
         break;
       case VidPlayStatus.loadFail:
-        child = Container(child: const Text("Load fail"));
+        child = const Text("Load fail");
         break;
       case VidPlayStatus.play:
-        child = buildFrameView(context);
-        break;
       case VidPlayStatus.pause:
-        child = Text(
-          widget.controller.currentFrameIndex.toString(),
-        );
+        child = buildFrameView(context);
         break;
       case VidPlayStatus.stop:
       case VidPlayStatus.dispose:
-        child = Container(child: const Text("Closed"));
+        child = const Text("Closed");
         break;
     }
     return buildBox(context, child);
@@ -109,10 +108,11 @@ class _VidCanvasPlayerState extends State<VidCanvasPlayer> {
 
   Widget buildFrameView(BuildContext context) {
     if (image != null) {
-      // final size = MediaQuery.of(context).size;
+      final size = MediaQuery.of(context).size;
       return CustomPaint(
-        painter: VidPainter(image: image),
+        painter: VidPainter(image: image!, contextSize: size),
         isComplex: false, //是否为复杂图像(true会缓存)
+        size: size,
       );
     } else {
       return Container();

+ 23 - 4
lib/pages/canvas_player/widgets/vid_painter.dart

@@ -2,14 +2,33 @@ import 'package:flutter/material.dart';
 import 'dart:ui' as ui;
 
 class VidPainter extends CustomPainter {
-  VidPainter({required this.image});
-  ui.Image? image;
+  VidPainter({required this.image, required this.contextSize});
+  ui.Image image;
+  Size contextSize;
 
   @override
   void paint(Canvas canvas, Size size) async {
-    Paint greenBrush = Paint()..color = Colors.greenAccent;
+    final double scale = size.width / image.width;
+    final double offsetY = (size.height - image.height * scale) / 2;
+
+    // print("${DateTime.now()}容器大小 ContextSize = $contextSize");
+    // print("${DateTime.now()}图片大小 ImageSize = ${image.width}x${image.height}");
+    // print("${DateTime.now()}画布大小 CanvasSize = $size");
+    // print("${DateTime.now()}缩放比例 Scale = $scale");
+    // print(
+    //     "${DateTime.now()}缩放后宽高 ScaledImageSize = ${image.width * scale}x${image.height * scale}");
+    // print("${DateTime.now()}偏移量 Offset = $offsetY");
+
+    Paint paint = Paint();
     canvas.save();
-    canvas.drawImage(image!, Offset(0, 0), greenBrush);
+    // canvas.scale(scale);
+    // canvas.drawImage(image, Offset(0, offsetY), paint);
+    canvas.drawImageRect(
+        image,
+        Rect.fromLTWH(0, 0, image.width.toDouble(), image.height.toDouble()),
+        Rect.fromLTWH(0, offsetY, size.width, image.height * scale),
+        paint);
+
     canvas.restore();
   }
 

+ 2 - 2
lib/pages/image_player/view.dart

@@ -82,8 +82,8 @@ class _ImagePlayerViewState extends State<ImagePlayerView> {
                 },
                 child: const Text('Pause')),
             SizedBox(
-              width: 500,
-              height: 500,
+              width: 600,
+              height: 600,
               child: RepaintBoundary(
                 child: VidImagePlayer(
                   _playerController as VidPlayerController,