【问题标题】:What is the alternative for deprecated Canvas.getMatrix()?已弃用的 Canvas.getMatrix() 的替代方案是什么?
【发布时间】:2012-11-03 15:01:45
【问题描述】:

我有以下代码 sn-p,它使用画布的当前转换矩阵转换一组边界。

    final RectF bounds = renderer.computeBounds
    activeCanvas.getMatrix().mapRect(result, bounds);
    return bounds;

但是,对于最新的 API 级别 (16),我会收到一条警告说明

The method getMatrix() from the type Canvas is deprecated

API 16 Diff Specification 确认。

这很好,但是Canvas.getMatrix() 上的当前文档没有提到弃用,也没有提供替代方案。作为一种解决方法,我现在只是取消了这个警告,但我真的很想知道新的和改进的 (tm) 方法是什么样的。

【问题讨论】:

  • 使用 canvas.concat(matrix)

标签: android android-4.2-jelly-bean


【解决方案1】:

矩阵现在由视图而不是画布处理。很遗憾,我无法向您解释 Google 对此的决定,但您应该能够通过 2 种方式重现完全相同的内容。

【讨论】:

  • 这似乎是真的,虽然getMatrixgetRotation... 是在 api 11 中引入的。无论如何,在 View 中引入了 TransformationInfo 来保存这些信息。为什么?我不知道,但我怀疑它与新的动画 api 有关
  • 我认为主要原因是使用 Canvas 会比直接使用 View 带来更多的复杂性。处理View的逻辑让代码更统一我想说,这大概就是他们试图实现的逻辑吧。
  • 但画布在绘制/生成位图时仍然非常有用。
  • 但画布并不总是属于视图。单独使用时应该有替代方案吧?
  • 有什么“两种方式”?
【解决方案2】:

我认为由于issue with getMatrix when hardwareAcceleration is enabled,他们弃用了它,正如 kouray 所说,现在矩阵由视图处理。

【讨论】:

    【解决方案3】:

    如果您无权访问视图:

    要解决此问题,在大多数情况下,您可以使用 canvas.scale()、canvas.translate() 等应用任何转换,而不是检索矩阵、修改它然后再次设置矩阵。

    来自 Google issue with getMatrix when hardwareAcceleration is enabled,由 Sandeep 引用。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-11-21
      • 1970-01-01
      • 1970-01-01
      • 2022-06-11
      • 1970-01-01
      • 2016-08-16
      相关资源
      最近更新 更多