0

Should I take view translation into account when overriding onDraw()? Or it is already applied to canvas translation?

I.e. should I execute

canvas.translate(getTranslationX(), getTranslationY());

in first line of onDraw()?

Do all transformations applied to canvas in onDraw() call persist in subsequent calls? I.e. should I suppose canvas is each time untransformed?

Suzan Cioc
  • 29,281
  • 63
  • 213
  • 385

2 Answers2

1

As far as I know the canvas is not persistant therefore you should execute the translate. However you can save the canvas and restore it:

http://maohao.wordpress.com/2009/09/30/canvas-save-canvas-restore/

Android View.onDraw() always has a clean Canvas

http://blahti.wordpress.com/2010/12/20/moving-views-in-android-part-1/

Community
  • 1
  • 1
HardCoder
  • 3,026
  • 6
  • 32
  • 52
1

No, you do not need to do this. A View's translation is applied before onDraw is called.

As HardCoder points out, state changes that you make to the Canvas passed to onDraw will not persist to the next call to onDraw.

adamp
  • 28,862
  • 9
  • 81
  • 69