关于Canvas.getMatrix()的一些问题收集

Canvas.getMatrix() Deprecated之后的建议

https://code.google.com/p/android/issues/detail?id=93367
When using Canvas.getMarix Android Studio stikes it out and hints as deprecated. In Canvas.class it’s really declared with @java.lang.Deprecated. But in documentation on page http://developer.android.com/intl/ru/reference/android/graphics/Canvas.html#getMatrix(android.graphics.Matrix) nothing is written about it, so developer does not know why it is deprecated and what is better to use instead.

The same situation with Canvas.drawPosText

Canvas.getMatrix Doc

https://developer.android.com/reference/android/graphics/Canvas.html?hl=ru#getMatrix(android.graphics.Matrix)
getMatrix
Added in API level 1
void getMatrix (Matrix ctm)
This method was deprecated in API level 16.
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.

Return, in ctm, the current transformation matrix. This does not alter the matrix in the canvas, but just returns a copy of it.

canvas.setMatrix(canvas.getMatrix())会改变绘制结果

https://code.google.com/p/android/issues/detail?id=24517
Canvas getMatrix() and setMatrix() do not correspond when using hardware acceleration (hardwareAcceleration=“true” in Android manifest), i.e. calling canvas.setMatrix(canvas.getMatrix()) in onDraw changes the resulting drawing. This does not happen when hardwareAcceleration is switched off.

The same problem occurs, when using canvas.getMatrix(myMatrix); canvas.setMatrix(myMatrix), and when, between the calls to getMatrix() and setMatrix(), transformations are applied to the matrix. Simply applying transformations to the canvas (e.g. canvas.translate(10, 10)) work as expected.

It seems that the call to getMatrix() returns the matrix relative to the view the canvas belongs to, whereas setMatrix() set’s the matrix relative to the entire screen (including status bar, title bar, etc.). When, for example, drawing to an empty screen with just the Android status bar (the one that indicates notifications, battery level, time, etc.), coordinate (0, 0) seems to lie just below the status bar when not applying any matrices (or switching off hardware acceleration), but it seems to lie in the very top-left corner of the screen (i.e. the top left corner of the status bar) when doing described matrix operation and using hardware acceleration.

The expected behavior would be to have (0,0) lie right below the status bar, such as when not doing any matrix operations or switching off hardware acceleration.

I’ve attached a sample program that illustrates the problem. I’ve run the program on my Nexus Galaxy running Android 4.0.1.

To work around the problem, in most situations (such as mine), you can apply any transformations using canvas.scale(), canvas.translate(), etc. rather than retrieving the matrix, modifying it and then setting the matrix again.

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值