Canvas To Bitmap And Back Again

Problems with Canvas, OpenGL, etc...

Canvas To Bitmap And Back Again

Postby Marky.d » Thu Oct 28, 2010 7:44 pm

Hey guys,

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 -

Code: Select all
protected void onDraw(Canvas canvas) {

    canvas.drawColor(Color.WHITE);

    //do some stuff here - this is all working ok

    canvas.drawLine(p1.x, p1.y, p2.x, p2.y, linePaint); 
}

Basically, I need to draw the new graphics as a layer on top of the last, so what I'm looking for is a way to carry the image of the last canvas to the current. There is a LOT of lines drawn every frame so simple drawing them all again isn't an option.

I have tried to figure it out myself using the canvas.setBitmap() method but it acts very funny.
This is what I put at the start of the onDraw() method, in my head it makes sense but it doesn't seem to do anything

Code: Select all
if(bitmap != null) {
        canvas.drawBitmap(bitmap, 0, 0, paint);
        canvas.setBitmap(bitmap);   
}


Any help is greatly appreciated

P.S if it's needed, the the class extends SurfaceView and implements SurfaceHolder.Callback
Marky.d
Freshman
Freshman
 
Posts: 2
Joined: Thu Oct 28, 2010 7:39 pm

Top

Re: Canvas To Bitmap And Back Again

Postby Tage Nielsen » Sat Oct 30, 2010 7:40 pm

Is your 'bitmap' object a member variable that persists between onDraw calls?
Tage Nielsen
Junior Developer
Junior Developer
 
Posts: 17
Joined: Wed Oct 13, 2010 8:02 pm

Re: Canvas To Bitmap And Back Again

Postby Schermvlieger » Sat Oct 30, 2010 9:48 pm

Hi Marky, did you check the fingerpaint activity in apidemos?
Schermvlieger
Senior Developer
Senior Developer
 
Posts: 159
Joined: Fri Feb 26, 2010 1:37 pm

Re: Canvas To Bitmap And Back Again

Postby Marky.d » Sun Oct 31, 2010 1:22 am

Found the answer myself :)

Syntax: [ Download ] [ Hide ]
Using java Syntax Highlighting
  1. @Override
  2. protected void onDraw(Canvas c) {
  3.     if(bitmap != null && canvas != null) {
  4.  
  5.         //do some stuff here to find the points I need
  6.         canvas.drawLine(p1.x, p1.y, p2.x, p2.y, linePaint);
  7.  
  8.         c.drawBitmap(bitmap, 0, 0, linePaint);  
  9.     }
  10. }
  11.  
  12. @Override
  13. public void surfaceChanged(SurfaceHolder holder, int format, int width, int height) {
  14.     bitmap = Bitmap.createBitmap(width, height, Config.RGB_565);
  15.     canvas = new Canvas(bitmap);
  16.     canvas.drawColor(Color.WHITE);
  17. }
  18.  
Parsed in 0.031 seconds, using GeSHi 1.0.8.4



This seems to work exactly as intended, it creates the effect of drawing on top of the old canvas continuously

Thanks for your help guys :)
Marky.d
Freshman
Freshman
 
Posts: 2
Joined: Thu Oct 28, 2010 7:39 pm

Top

Return to Android 2D/3D Graphics - OpenGL Problems

Who is online

Users browsing this forum: Google [Bot] and 1 guest