Draw From Old Canvas - Android

前端 未结 2 620
半阙折子戏
半阙折子戏 2021-02-11 09:10

I\'m making an App that needs to be able to draw new graphics on top of the last set.

This is my current onDraw() method -

protected void onDraw(Canvas          


        
相关标签:
2条回答
  • 2021-02-11 10:01

    You will have to store the previous image persistently onto a ArrayList, and during ondraw, loop through the ArrayList to redraw all items.

    something like this:

    for (Graphic graphic : _graphics) {
        bitmap = graphic.getBitmap();
        coords = graphic.getCoordinates();
        canvas.drawBitmap(bitmap, coords.getX(), coords.getY(), null);
    }
    
    0 讨论(0)
  • 2021-02-11 10:05

    Found the answer myself :)

    @Override
    protected void onDraw(Canvas c) {
    
    if(bitmap != null && canvas != null) { 
        canvas.drawLine(p1.x, p1.y, p2.x, p2.y, linePaint);
        c.drawBitmap(bitmap, 0, 0, linePaint);  
    }
    }
    
    @Override
    public void surfaceChanged(SurfaceHolder holder, int format, int width, int height) {
    
    bitmap = Bitmap.createBitmap(width, height, Config.RGB_565);
    canvas = new Canvas(bitmap);
    canvas.drawColor(Color.WHITE);
    }
    

    Works exactly as intended, it creates the effect of drawing on top of the old canvas continuously

    0 讨论(0)
提交回复
热议问题