我正在尝试实现自定义SurfaceView 的简单“显示/隐藏”功能。 这个论坛上的许多建议方法对我不起作用......
例如:
1) surfaceView.setVisibility( SurfaceView。 GONE ); //或不可见或可见
2) relativeLayout.removeView(surfaceView);它可以工作,但是,破坏视图。
3) surfaceView.setZOrderOnTop(true)或false,dinamically changed不起作用
4) surfaceView.getDrawingCache():此方法将SurfaceView作为位图返回,因此序列为
隐藏
从surfaceView获取位图并保存(Bitmap btm = surfaceView.getDrawingCache())
使用保存的位图创建新的虚拟画布,dummyCanvas = new Canvas(btm);
清除画布(Canvas实例位于MainActivity中,并在SurfaceView线程的run()方法中初始化。)
显示
最后一种方法也不起作用,它返回黑色(jpeg)或空(png)图像。
这是代码的一部分:
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();
}