2

Little prologue: I'm creating a some kind of drawing application for Android (API 14 and higher). Few months ago I've started working on it and decided to use SurfaceView as a canvas to draw on. I thought that this a good decision cause SurfaceView works directly with Graphics. And everything seemed to work fine until one day I've noticed that the drawing process is a little bit laggy. There're probably a lot of weird code down there.

Anyway, now I'm optimizing that code and stuff, and I thought, do I really need to use SurfaceView for such scenario? The main things I need from my "canvas" is to draw smooth and be able to save all the drawings to Bitmap->File on External storage (this works fine).

So, should I use the simple View or the SurfaceView? Also, it would be great to hear props and cons of your decision/proposition.

Thanks

Paul Freez
  • 1,059
  • 15
  • 27
  • TextureView will not be laggy. – Whitney Dec 01 '14 at 16:36
  • @Whitney - TextureView has the same surface-rendering performance as SurfaceView, plus the added overhead of pushing the Surface to a texture and rendering the texture. So I'm not sure why you think it will not be laggy if SurfaceView is. – fadden Dec 01 '14 at 17:23
  • @fadden surfaceview can't be hardware accelerated like a textureview – Whitney Dec 01 '14 at 17:39
  • 1
    Rendering on a TextureView with Canvas is not hardware accelerated. See e.g. Romain Guy's answer here: http://stackoverflow.com/questions/9966228/android-textureview-hardware-acceleration-with-lockcanvas – fadden Dec 01 '14 at 21:40

1 Answers1

7

If you want to use Canvas, you are increasingly better off with a custom View, rather than a SurfaceView. The simple reason is that Canvas rendering on a View can be hardware accelerated, while Canvas rendering on a SurfaceView's surface is always done in software (still true as of Android 5.0).

By drawing "smooth" I assume you want some anti-aliasing effects. Check the chart on the hardware acceleration page to confirm that the effects you want are supported for the Android releases you want to ship on.

As device display pixel counts get steadily higher, software rendering gets increasingly expensive, and on some devices the CPU or bus isn't fast enough to keep frame rates high. Fortunately on these the pixel density is so high that you don't really need anti-aliasing, so even if it's not supported you could ignore it until you generate your software-rendered bitmap.

Before you do anything, though, it would be wise to figure out what your source of sluggishness is. It's possible you're being slowed down by inefficiencies in your drawing code rather than pixel fill rate. Check it with some of the profiling tools.

(See also the graphics architecture doc.)

fadden
  • 51,356
  • 5
  • 116
  • 166