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;
}
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With