Double buffering in Java on Android with canvas and surfaceview

后端 未结 2 523
耶瑟儿~
耶瑟儿~ 2020-12-09 13:39

How does one go about doing this? Could somebody give me an outline?

From what I\'ve found online, it seems like in my run() function:

  1. create a bitmap<
2条回答
  •  借酒劲吻你
    2020-12-09 13:48

    The steps from Android Developers Group say that you need a buffer-canvas, to which all the renders are drawn onto.

    Bitmap buffCanvasBitmap;
    Canvas buffCanvas;
    
    // Creating bitmap with attaching it to the buffer-canvas, it means that all the changes // done with the canvas are captured into the attached bitmap
    tempCanvasBitmap = Bitmap.createBitmap(getWidth(), getHeight(), Bitmap.Config.ARGB_8888);
    tempCanvas = new Canvas();
    tempCanvas.setBitmap(tempCanvasBitmap);
    
    // and then you lock main canvas
    canvas = getHolder().lockCanvas();              
    // draw everything you need into the buffer
    tempCanvas.drawRect.... // and etc
    // then you draw the attached bitmap into the main canvas
    canvas.drawBitmap(tempCanvasBitmap, 0, 0, drawView.getPaint());
    // then unlocking canvas to let it be drawn with main mechanisms
    getHolder().unlockCanvasAndPost(canvas);
    

    You are getting the main buffer, which you are drawing into without getting different double-buffer canvas' on each holder's lock.

提交回复
热议问题