flutter 如何设置InteractiveViewer初始缩放级别

yeotifhr  于 2023-02-16  发布在  Flutter
关注(0)|答案(2)|浏览(330)

我正在使用InteractiveViewer显示视频。它工作正常。但我想设置初始缩放级别。
有什么办法吗?

return InteractiveViewer(
  minScale: 1,
  maxScale: 4,
  constrained: false,
  child: Container(
    color: Colors.red,
    width: 320,
    height: 180,
    child: widget.remoteVideoRenderer(),
  ),
);
92vpleto

92vpleto1#

我能够做到这一点,并取得了不同程度的成功,就像这样(感谢@pskink为我指明了正确的方向):

final viewTransformationController = TransformationController();

  @override
  void initState() {
    final zoomFactor = 2.0;
    final xTranslate = 300.0;
    final yTranslate = 300.0;
    viewTransformationController.value.setEntry(0, 0, zoomFactor);
    viewTransformationController.value.setEntry(1, 1, zoomFactor);
    viewTransformationController.value.setEntry(2, 2, zoomFactor);
    viewTransformationController.value.setEntry(0, 3, -xTranslate);
    viewTransformationController.value.setEntry(1, 3, -yTranslate);
    super.initState();
  }

  @override
  Widget build(BuildContext context) {
    return InteractiveViewer(
              transformationController: viewTransformationController,
              minScale: 0.1,
              maxScale: 6,
              child: // ....
    );
  }

viewTransformationController.value是一个应用于视图的4x 4矩阵,它对平移/旋转/缩放进行编码。矩阵中这些变换的表示可能在某个地方有文档记录(和/或只是投影/仿射几何的标准),但我找不到它们,所以我只是在缩放/平移的同时打印出来,直到每个条目的作用变清楚。
如果您只将缩放因子设置为2而不进行平移,则会放大小部件的左上角。
注意你不能使用MediaQuery.of(context)来访问initState()方法中的窗口尺寸,这在你想放大窗口中间的时候会有些不方便,我还没有找到一个好的方法来做到这一点。

nzk0hqpo

nzk0hqpo2#

根据Amos Joshua的回答,如果你想一直放大屏幕中央,我有这个公式:

zoomFactor = 1, xTrans = 0, yTrans = 0
zoomFactor = 2, xTrans = w / 2, yTrans = h / 2
zoomFactor = 2.8, xTrans = (w / 2)(1 + 0.8), yTrans = (h / 2)(1 + 0.8)

因此:

zoomFactor = z, xTrans = (w / 2)(z - 1), yTrans = (h / 2)(z - 1)

相关问题