I'm trying to implement an easy "show/hide" function of a custom SurfaceView. Many of suggested methods on this forum doesn't work for me...
for example:
1) surfaceView.setVisibility(SurfaceView.GONE);// or INVISIBLE or VISIBLE
2) relativeLayout.removeView(surfaceView); it works but, destroy the view.
3) surfaceView.setZOrderOnTop(true) or false, dinamically changed doesn't works
4) surfaceView.getDrawingCache(): this method return the SurfaceView as a bitmap so , the sequence is
Hide
get the bitmap from surfaceView and save it (Bitmap btm = surfaceView.getDrawingCache())
create new dummy canvas with saved bitmap, dummyCanvas = new Canvas(btm);
clear the canvas ( Canvas instance is in the MainActivity and initialized in the run() mehtod of the SurfaceView thread.)
Show
- surfaceView.draw(dummyCanvas);
Also the last one method don't work, it return a black(jpeg) or empty(png) image.
This is a part of the code:
class CustomSurfaceView extends SurfaceView implements Runnable{
Thread thread = null;
SurfaceHolder surfaceHolder;
volatile boolean running = false;
public CustomSurfaceView(Context context) {
super(context);
this.setWillNotDraw(false);
this.setBackgroundColor(Color.TRANSPARENT);
this.setZOrderOnTop(true);
this.setDrawingCacheEnabled(true);
this.buildDrawingCache();
// get the holder to create the canvas into the Thread.run() method:
// "canvas = surfaceHolder.lockCanvas(null)"
surfaceHolder = getHolder();
surfaceHolder.setFormat(PixelFormat.TRANSPARENT);
}
//getDrawingCache() seems to be the only way to get a bitmap from the
//SurfaceView, because my canvas is not "Canvas c = new Canvas(Bitmap)
//BUT this method only return EMPTY BUT NOT NULL image.
public Bitmap getBitmap(){
return this.getDrawingCache();
}