0

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();
    }
Andrea D'Ubaldo
  • 137
  • 1
  • 6
  • SurfaceViews have two parts, the Surface and the View. Make sure you understand which part you're drawing on. Depending on your needs, a custom View may be more appropriate (http://developer.android.com/training/custom-views/index.html). – fadden Nov 24 '15 at 16:57
  • Hi, thanks for your reply. This is an augmented reality app, I actually need custom SurfaceView, in order to overlap the objects over the image acquisition. – Andrea D'Ubaldo Nov 25 '15 at 08:48
  • You're calling `setZOrderOnTop()`, which puts the Surface layer on top of the View UI layer. You can't change the Z-order on the fly. You either need to clear the Surface to transparent (by disconnecting the producer, attaching GLES, and clearing it) or destroy the Surface. It would be easier to arrange the app such that the Surface was behind the View layer; then you just need to make the View contents opaque. – fadden Nov 25 '15 at 16:34
  • Without `setZOrderOnTop()` I can only draw over the view's canvas ( `onDraw(Canvas canvas)`<-- this canvas) but, I can't keep last drawn...so i decided to use another canvas, returned by the surface holder...`surfaceHolder.lockCanvas()`(surface's canvas), it works good...now the only thing I would to know is : how can hide and...(most important) **show** the canvas again? Thank you in advice – Andrea D'Ubaldo Nov 26 '15 at 11:12
  • edit : how can hide and...(most important) show the canvas again with the **last** **drawn**? – Andrea D'Ubaldo Nov 26 '15 at 13:05
  • When you draw on the View, you're drawing on the View UI layer. When you draw on the Surface, you're drawing on a completely separate layer. The Canvas is just the software rendering mechanism, so thinking in terms of showing / hiding a Canvas doesn't make sense. The drawing cache, and notions like "visibility", are part of the View UI system, and do not interact with content drawn on a Surface. To keep the last thing you drew to a Surface, you could draw on a Canvas backed by a Bitmap, and then blit the Bitmap to the Surface. – fadden Nov 26 '15 at 16:47
  • I totally agree with you, maybe I did not explain well. My intent is to draw some shapes on the canvas and then, "onButtonClick()" event, hide and show these shapes. So I'm looking for an _easier_ and _faster_ _alternative_ (showing / hiding a surfaceView I thought) to store(into ArrayList) these shapes and **redraw** on "show" event ( it open a thousand other problem, thread problem ) – Andrea D'Ubaldo Nov 26 '15 at 18:51

0 Answers0