2

In a current test project I have a custom View, that I have named SVGView, that simply paints some vector graphics (Paths, etc.) to the Canvas.

The XML layout file consists of nothing more than a FrameLayout, which contains a single child SVGView. The android:layout_width and android:layout_height attributes for the SVGView in the current example are both set to 300px (which the containing FrameLayout has more than enough room to accommodate). In the SVGView's onMeasure(), the call to setMeasuredDimension() is given the arguments 300, 300.

When the onDraw() method is called to draw the graphics to the Canvas object, I find that the Canvas doesn't have an identity matrix; rather, the Canvas has some amount of scale transformation already applied. What's the reason for this? I (wrongly) assumed that the Canvas would have a scaling of 1, because the actual dimensions of the View, its Canvas, and its representation on the screen all match.

I'm asking this in relation to the problem I have described in detail in my earlier question here: Canvas drawing cache bitmap is slightly blurred or has anti-alias. I'm finding that when I draw the cache bitmap back to the Canvas, it's slightly blurred compared to the original vector graphics, despite the fact that I'm drawing the bitmap back using no rescaling and using a Paint with anti-alias turned off. I'm now just absolutely convinced there there has to be some scaling process that I'm unaware of causing this to happen to the bitmap. To discover that the Canvas argument to onDraw() has some scaling transformation already applied is possibly a big clue to solving this problem -- but I can't understand why it has such scaling, when surely the Canvas' dimensions perfectly match the area of the screen it's ultimately painting to.

Thanks,

Trev

Community
  • 1
  • 1
Trevor
  • 10,903
  • 5
  • 61
  • 84
  • I'm not going to answer because I don't feel like an absolute authority. It would help to see your layout. Also, what is coming into the onMeasure? Are you verifying you are being offered your full requested space? What is the scaling factor present in the view? – Nick Campion Jun 08 '11 at 23:38
  • Thanks, Nick - I'll post the layout and also verify what you've queried. Might have to wait until morning now - wife has demanded computer goes off :-) – Trevor Jun 08 '11 at 23:44

1 Answers1

5

Have you declared a minSdkVersion or targetSdkVersion in your manifest? If you are targeting an API level lower than 4 your app will run in scaled compatibility mode as if it were targeting a G1 screen.

adamp
  • 28,862
  • 9
  • 81
  • 69
  • 1
    Awesome. Bingo. This is exactly the reason why. Once I had set the minSdkVersion to 8, the scale at which my Canvas is represented on the screen is now what it should be. Quite incredibly, I hadn't realised until now that it was in scaled compatibility mode. Now my use of the bitmap cache looks as crisp and sharp as the original vector graphics that were drawn. You're also welcome to put a similar answer in my other question if you like as you've solved that one too and I'll also accept it there. Many thanks again! – Trevor Jun 09 '11 at 06:46