9

I have this code in a class that extends surface view and implements runnable I am able to use the class basically allows you to draw to the canvas using different colors and such. I'm trying to get a method that will allow me to save the image after it is drawn and this is the method. No matter what i do i just get a black image with nothing on it. Any ideas?

I have cache drawing enabled

Objective get a bitmap image from a custom SurfaceView I have exhausted options of looking at some other post on here and didn't find anything to work. Here is hoping that there is recently a new solution to this. Many thanks

public Bitmap getImage() {
            Bitmap bitmap = Bitmap.createBitmap(this.getWidth(),
                    this.getHeight(), Bitmap.Config.ARGB_8888);
            Canvas canvas = new Canvas(bitmap);
            this.draw(canvas);
            return bitmap;
        }
Loktar
  • 34,764
  • 7
  • 90
  • 104
Lpc_dark
  • 2,834
  • 7
  • 32
  • 49
  • What do you expect `this.getWidth()` and `this.getHeight()` to give? – sanjeev mk Jan 06 '13 at 11:17
  • You are creating a new Bitmap, that has nothing on it. Then you create a Canvas from that blank bitmap. You have not done any drawing, how can you expect a drawing? Just a note: if you are overriding the onDraw() method of the SurfaceView, be sure to call `setWillNotDraw(false)` in your class constructor. – sanjeev mk Jan 06 '13 at 11:27
  • i expected to get the surfaceview width and height since that is the class i am extending. – Lpc_dark Jan 06 '13 at 11:57
  • What i thought was happening was that i create a new blank bitmap that has the dimension of my surfaceview. The i have a new canvas that will draw to my bitmap. Then i call this.draw to draw to my canvas which then draws to the bitmap? am i wrong in thinking? im guessing i must be – Lpc_dark Jan 06 '13 at 12:00
  • from developer.android.com Construct a canvas with the specified bitmap to draw into. The bitmap must be mutable. The initial target density of the canvas is the same as the given bitmap's density. Parameters bitmap Specifies a mutable bitmap for the canvas to draw into. – Lpc_dark Jan 06 '13 at 12:02
  • This is the definition for draw-----Manually render this view (and all of its children) to the given Canvas. The view must have already done a full layout before this function is called. When implementing a view, implement onDraw(android.graphics.Canvas) instead of overriding this method. If you do need to override this method, call the superclass version. – Lpc_dark Jan 06 '13 at 12:07
  • Yes, you are right. What I'm saying is, your new Bitmap is blank, hence black. Try loading an image resource to your Bitmap first, using `BitmapFactory.decodeResource()`. What if your code is working correctly, loading the Bitmap, but you are seeing a blank screen, because you have drawn nothing so far. Check this http://stackoverflow.com/questions/3693060/loading-a-resource-to-a-mutable-bitmap – sanjeev mk Jan 06 '13 at 12:10
  • but i know i have drawn to my view because i can see the drawing that i made. My surface View has an image on it. With different colours and such but it's the retrieving the image that isn't working. – Lpc_dark Jan 06 '13 at 12:13
  • Finally founds a solution that works. My God will post 2moro – Lpc_dark Jan 07 '13 at 04:30

1 Answers1

1

Your question became clear only by your last comment. In the code that you posted above, you are returning with return bitmap. That will return the local variable bitmap. This local variable is totally blank. You may be drawing to your bitmap or associating an image to it, somewhere else in the code. But the instance of bitmap in your above code, is blank and only local to the function. You cannot expect it to return your updated, latest bitmap.

Now, after your comment I googled "getting a bitmap of current surfaceview" and it led me to this SO answer: Create Bitmap from SurfaceView

In that question, it was apparently solved by extending View instead of SurfaceView. Drawing cache only works for View.

Update: Follow the below tutorials. Based on the code pasted by you, it's not clear what the error is. There are a lot of things that need be done for drawing to SurfaceView, and I'm not sure if you have done those, and I cannot ask for every such missing item. I followed below tutorials for my basic graphics projects. You need to read them and see if you have missed anything.

Tutorials on Canvas drawing:

Playing with Graphics in android all parts.

Android 2D

Community
  • 1
  • 1
sanjeev mk
  • 4,276
  • 6
  • 44
  • 69
  • There must be a way. I've seen others use it successfully. Just not the coded. I used a view before it didn't work because the performance of it wasn't as quick. What i tried to do now was to make a field Image and canvas. Then i placed the image into the canvas and when i call onDraw i just send in my canvas instead of the one they wanted to. Still didn't work... any idea what draws to the surfaceView and when it is called. So i can insert my canvas instead of the one that it uses. – Lpc_dark Jan 06 '13 at 12:34
  • For not letting SurfaceView itself draw to the canvas, and letting your own `onDraw()` to do the task, call `setWillNotDraw(false)` from your class constructor. – sanjeev mk Jan 06 '13 at 12:36
  • Thanks ye i already included it. Sigh still can't get the image out of it. I have a log inside onDraw and im only seeing it called when i first open the application. It's not called when i am drawing to the surface view? when is it called by anychance? – Lpc_dark Jan 06 '13 at 12:41
  • When you call `invalidate()` your `onDraw()` is called again. What I'd suggest is, do your drawings in a new Thread. In the `run()` method of the Thread, you can update the Canvas and call `invalidate()`. This will call `onDraw()` and show updated canvas. `run()` is also called once on thread creation, so you have to implement a `while(true)` infinite loop inside `run()` to keep updating your drawing. – sanjeev mk Jan 06 '13 at 12:51
  • I wont be able to call invalidate because only the Thread that created it will be able to access it. just tried – Lpc_dark Jan 06 '13 at 13:08