Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[问答] 打开硬件加速时,Canvas.getMatrix() 方法为什么也能返回正常的矩阵 #734

Open
RubiTree opened this issue Dec 26, 2018 · 1 comment

Comments

@RubiTree
Copy link

RubiTree commented Dec 26, 2018

Canvas.getMatrix()中有标记这个方法被废弃,因为:

Hardware accelerated canvases may have any matrix when passed to a View or Drawable, as it is implementation defined where in the hierarchy such canvases are created. It is recommended in such cases to either draw contents irrespective of the current matrix, or to track relevant transform state outside of the canvas.

(渣翻):在Canvas开启硬件加速后,getMatrix获取到的矩阵可能完全不是你预期的样子,因为这个方法的具体实现被Canvas创建的层级所决定,所以建议你要么不用获取到的matrix做事情,要么在画布之外跟踪相关的变换状态

同时在这个Issue Tracker里,有开发者反馈:在开启硬件加速之后,做这样一个无害操作canvas.setMatrix(canvas.getMatrix())居然会改变Canvas的状态,也就是getMatrix()setMatrix()不对应
谷歌员工对此的回复是:

When hardware acceleration is enabled, a View's drawing commands are recorded in a display list. At this point, a call to getMatrix() will only return the matrix built by the View itself. Any operation performed by the parents will not be taken into account (and cannot be taken into account, a display list can be generated before a View even have a parent.)

(渣翻):启用硬件加速后,View的绘图命令将被记录在Display List中。此时,getMatrix()将仅返回由View本身构建的矩阵。父View执行的任何操作都不会被考虑在内(也没法考虑,因为Display List在View拥有父View之前就已经生成了)

谷歌员工的解释补充了源码注释里一些晦涩的地方,整个机制看上去还是比较合理的,但我在进行验证的时候,却发现开启硬件加速后,无害操作canvas.setMatrix(canvas.getMatrix())并没有改变Canvas的状态(是在一个有状态栏标题栏的页面中,理论上父view对子view的canvas有平移操作),非常奇怪
而且运行Issue Tracker里开发者提供的代码,也能正常工作,没有出现该开发者描述的问题,如下图:
image
(运行环境为Android 8.0 虚拟机,并且能看到canvas.isHardwareAccelerated返回true也就是开启了硬件加速)

@RubiTree
Copy link
Author

我终于在一个Android 6.0.1的手机上复现了这个现象,但是为什么都是开启硬件加速,大家的表现却不一样呢,是因为这取决于不同Android版本的具体实现吗?API上的废弃只是表明Android不保证它的实现,而不是说一定实现得跟关闭硬件加速不同?

如图:
image

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant