I am creating a drawing application for Android. An extension of the FingerPaint sample application provided by the SDK. However, unlike FingerPaint, I use SurfaceView with a separate rendering stream to paint on the surface. All this is very standard and simple. It works well. However, I want the background color to be white on the surface of the painting, so he wanted to draw white paper. The default background is black. The thread of my surface view calls the onDraw () method to represent.
The problem I am facing is that if I set the background color or background resource for my SurfaceView, this background overwrites the previous surface drawing the next time the view is displayed. I will explain this with an example:
Suppose I set the background to white. Now the application starts and my SurfaceView is white. Ok, so far so good. Now I draw a red line on this surface with my finger. Now the line is shown on a white surface. Good. Now this should be a drawing application, and suppose I want to draw a car. So then I draw my second red line with my finger. The second line is drawn on the screen, but the first one I drew earlier disappears. That is, since I set some background for SurfaceView, the background is drawn again, thus overwriting the first line that was drawn. Now only the second line is displayed on the screen.
Obviously, I do not want this to happen. The code works fine when I am not trying to change the background (that is, both lines are displayed on a black background by default, without a background effectively). But when I set some desired background, this happens. Is there a way to somehow have a static background that is not drawn every time? I want the background to be drawn only once, all subsequent drawings should occur against this background. I do not want the android runtime to draw a background, every time it draws my view, thus rewriting the entire drawing present in this view from previous visualizations. Any way around this?
What I tried to achieve this goal:
Setting the background color of my SurfaceView using android: background in XML.
Doing the above using a style concept. (indicating the value of the style and referring to it in the layout file). The style defines the background color as #FFFFFFFF (white).
Set style 2 to the parent SurfaceView (RelativeLayout).
Setting style 2 for the entire application as a theme using the android: theme in my manifest file.
Setting the background image of my SurfaceView to an image that is simple and white.
Call setBackgroundColor for my SurfaceView from as, this.setBackgroundColor (Color.WHITE) code.
Thanks.
source share