What is the alternative for deprecated Canvas.getMatrix()?
Asked Answered
H

3

25

I have the following code snippet, that transforms a set of bounds using the canvas' current transformation matrix.

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

However, with the latest API level (16) I get a warning stating

The method getMatrix() from the type Canvas is deprecated

as confirmed by the API 16 Diff Specification.

Which is fine and all, but the current documentation on Canvas.getMatrix() doesn't mention the deprecation, nor does it offer an alternative. As a workaround I now simply suppress this warning, but I would really like to know what the new and improved (tm) way of doing this looks like.

Hierology answered 3/11, 2012 at 15:1 Comment(1)
use canvas.concat(matrix)Calva
F
9

The Matrix is now handled by the view rather than the Canvas. I unfortunately can't explain you Google's decision on this one, but you should be able to reproduce the exact same things with the 2 ways.

Fulvi answered 6/11, 2012 at 1:2 Comment(5)
This seems to be true, although the getMatrix, getRotation... were introduced in api 11. Anyway, TransformationInfo is introduced in View to hold this information. Why? I am unaware, but I suspect it has to do with the new animation apiPandanus
I think the main reason why is that working with a Canvas brings more complexity than working directly with the View. The logic of dealing with the View makes the code more uniform I'd say, and it's probably the logic they try to achieve.Fulvi
But a canvas is still very useful when drawing/generating bitmaps.Scraper
but canvas doesn't always belong to a view. there should be an alternative for when you use it alone, right?Cornelius
With what "the 2 ways"?Midland
T
9

I think because of the issue with getMatrix when hardwareAcceleration is enabled, they deprecated it, as kouray said now matrix is handled by the view.

Tantivy answered 9/11, 2012 at 12:43 Comment(0)
R
4

If you don't have access to a view:

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

From the Google issue with getMatrix when hardwareAcceleration is enabled, as referenced by Sandeep.

Rosenberg answered 13/4, 2015 at 17:20 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.