Getting a bitmap from a custom SurfaceView

I have this code in a class that extends surface visibility and implements runnable. I can use a class that basically allows me to paint on canvas using different colors, etc. I am trying to get a method that will allow me to save an image after drawing it, and this is a method. No matter what I do, I just get a black image without anything. Any ideas?

I have cache design enabled

The goal is to get a bitmap from a custom SurfaceView. I ran out of opportunities to look at some other entry here and did not find anything to work with. Here we hope that a new solution has recently appeared. 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; } 
+9
android extends bitmap android-canvas surfaceview
source share
2 answers

Your question became clear only by your last comment. In the code you specified above, you return using return bitmap . This will return the local bitmap variable. This local variable is completely empty. You can draw into a bitmap or associate an image with it, somewhere else in the code. But the bitmap instance in your above code is empty and only local to the function. You cannot expect it to return the updated, last bitmap.

Now, after your comment, I googled “get a bitmap of the current surface view” and this led me to this SO answer: Create a bitmap from SurfaceView

In this question, it was apparently resolved by extending the View instead of SurfaceView. The drawing cache works for presentation only.

Update: Follow the tutorials below. Based on the code you inserted, it is not clear what the error is. There is a lot to do to draw SurfaceView, and I'm not sure if you did this, and I cannot ask for every such missing item. I followed the tutorials for my main graphic projects. You should read them and see if you missed something.

Canvas Painting Tutorials:

Game with graphics in android all parts.

Android 2D

+1
source share

The third time I post this answer today in my search to do the same.

On devices with API level 24 or later, you can use the PixelCopy API to get pixels from Surface / SurfaceView to the bitmap. APIs 26 and above offer you a way to run PixelCopy on a Surface Window . Prior to API 24 though, there seems to be no way. There are many answers that offer many methods related to drawingCache representation, but none worked for me, and from what I know about SurfaceView they shouldn't work.

0
source share

All Articles