Search code examples
androidxmldrawingsurfaceview

Android - Drawing simple sine wave on SurfaceView


I have an activity with a view, which contains a few UI elements like a slider, some textViews and a surfaceView. I want to draw a simple sine wave on the surfaceView, except I have got no idea on how to do that. The tutorials I could found, did not solve my problem.

So basically, how can I draw a sine wave on a surfaceView defined in my xml layout file?


Solution

  • To draw on a SurfaceView, you need to write your own class which extends SurfaceView. Use this class in your XML layout file. Then you have to override onDraw(Canvas canvas) to do the drawing. Use Math.sin(double a) to get the sinus.