Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android: Canvas vs OpenGL

I have a drawing app where the user can draw lines with their finger, adjust the color, thickness, etc. As the user is drawing, I am converting the massed X/Y points from MotionEvent into SVG Paths, as well as creating Android Path's and then drawing the Android Path's to the screen via a Canvas, and committing the SVG Path's to the app's database.

I am following the model used in FingerPaint, in that the 'in progress' lines are drawn on the fly by repeated calls to invalidate() (and thus, onDraw()), and once the line is complete and a new line is started, the previous line(s) are drawn in onDraw() from the underlying Canvas Bitmap, with in progress lines again generating repeated re-draws.

This works fine in this application - until you start rotating the underlying Bitmap to compensate for device rotation, supporting the ability to 'zoom in' on the drawing surface and thus having to scale the underlying Bitmap, etc. So for example, with the device rotated and the drawing scaled in, when the user is drawing, we need to scale AND rotate our Bitmap in onDraw(), and this is absolutely crawling.

I've looked at a SurfaceView, but as this still uses the same Canvas mechanism, I'm not sure I'll see noticeable improvement... so my thoughts turn to OpenGL. I have read somewhere that OpenGL can do rotations and scaling essentially 'for free', and even seen rumors (third comment) that Canvas may be disappearing in future versions.

Essentially, I am a little stuck between the Canvas and OpenGL solutions... I have a 2D drawing app that seems to fit the Canvas model perfectly when in one state, as there are not constant re-draws going on like a game (for instance when the user is not drawing I don't need any re-drawing), but when the user IS drawing, I need the maximum performance necessary to do some increasingly complex things with the surface...

Would welcome any thoughts, pointers and suggestions.

like image 225
Unpossible Avatar asked Jun 10 '11 15:06

Unpossible


People also ask

Can I use OpenGL on Android?

Android includes support for high performance 2D and 3D graphics with the Open Graphics Library (OpenGL®), specifically, the OpenGL ES API. OpenGL is a cross-platform graphics API that specifies a standard software interface for 3D graphics processing hardware.

What is Android canvas?

Canvas is a class in Android that performs 2D drawing of different objects onto the screen. The saying “a blank canvas” is very similar to what a Canvas object is on Android. It is basically, an empty space to draw onto. The Canvas class is not a new concept, this class is actually wrapping a SKCanvas under the hood.

How does OpenGL work in Android?

OpenGL in Android is a graphics library that is used for 2D and 3D graphics development in Android applications. OpenGL provides cross-platform APIs. It processes high-performance data transfer between CPU and GPU. OpenGL supports the android native development kit(NDK) for graphics features development.

What is canvas and Bitmap in Android?

Canvas is the place or medium where perfroms/executes the operation of drawing, and Bitmap is responsible for storing the pixel of the picture you draw.


2 Answers

OpenGL would be able to handle the rotations and scaling easily.

Honestly, you would probably need to learn a lot of OpenGL to do this, specifically related to the topics of:

  • Geometry
  • Lighting (or just disabling it)
  • Picking (selecting geometry to draw on it)
  • Pixel Maps
  • Texture Mapping
  • Mipmapping

Also, learning OpenGL for this might be overkill, and you would have to be pretty good at it to make it efficient.

Instead, I would recommend using the graphic components of a game library built on top of openGL, such as:

  • Cocos2d
  • libgdx
  • any of the engines listed here
like image 62
maher.cs Avatar answered Oct 11 '22 20:10

maher.cs


Well, this question was asked 6 years ago. Maybe Android 4.0 has not come up? Actually, after Android 4.0 the Canvas at android.view.View is a hardware accelerated canvas, which means it is implementd by OpenGL, so you do not need to use another way for performance.

You can see the https://github.com/ChillingVan/android-openGL-canvas/blob/master/canvasglsample/src/main/java/com/chillingvan/canvasglsample/comparePerformance/ComparePerformanceActivity.java to compare the performance of normal canvas in view with GLSurfaceView.

like image 39
ChillingVan Avatar answered Oct 11 '22 21:10

ChillingVan