0
@Override
        protected void onDraw(Canvas canvas) {
        // Draw graphic objects
         .....
             }

         public void displayCalc(){

           //Do some calculation & display results near these graphic objects
            String result = String.valueOf(value);

             //Do some calculation

              //Display Calculated values
             Canvas c =new Canvas();
             Paint paint = new Paint();
             paint.setStyle(Paint.Style.FILL);
             paint.setAntiAlias(true);
             paint.setColor(Color.WHITE);
             c.drawText(result,200,300,paint);
                     }

But if i have the same thing in the function onDraw it works fine. I would like to know why or what changes i have to make to get it working

   @Override
    protected void onDraw(Canvas canvas) {

    // Draw graphic objects 
    //Do some calculation & display results near these graphic objects
     .....
     String result = String.valueOf(value);

        //Display Calculated values
         Paint paint = new Paint();
         paint.setStyle(Paint.Style.FILL);
         paint.setAntiAlias(true);
         paint.setColor(Color.WHITE);
         canvas.drawText(result,200,300,paint);
}
m4n07
  • 2,267
  • 12
  • 50
  • 65
  • Which one doesn't work? The first one looks like you're drawing to a canvas that isn't associated with a surface, so I'd be expecting that to be the one that fails (well... doesn't display anything). – forsvarir Apr 12 '11 at 09:46
  • sorry i missed out some thing ! I have edited now. Its the first one doesnt display anything – m4n07 Apr 12 '11 at 09:56
  • How is displayCalc called? Is it called on the UI thread? – forsvarir Apr 12 '11 at 09:59
  • As you've said below, duplicate of your own question http://stackoverflow.com/questions/5635434/android-text-not-visible-on-writing-using-drawtext . Try to avoid asking the same question twice... – forsvarir Apr 13 '11 at 05:27
  • @forsvarir, Its not exactly the same question, thats a problem i was facing with AsyncTask . (or) how to use canvas with AsyncTasks ! Btw my displayCalc is called from Oncreate() – m4n07 Apr 13 '11 at 05:52

2 Answers2

1

If you're trying to implement some kind of double buffering, you might want to take a look at this

I think your problem is that you need to create a bitmap, then attach the canvas to it, something like:

Bitmap bitmap = Bitmap.createBitmap(width, height, Config.RGB_565);
Canvas c = new Canvas(bitmap);

// then draw to the canvas..
// and when you're happy, draw the bitmap onto the canvas supplied to onDraw.

Just creating a canvas, doesn't make it appear on the screen.

You may also want to take a look at: this tutorial

If you have a surfaceView, then you can do something like this (don't have a compiler, but hopefully you get the gist):

SurfaceView view = (SurfaceView)findViewById(R.id.view);

SurfaceHolder holder = view.getHolder(); // save this where it can be accessed by your function

Canvas c 
try  {
    c = holder.lockCanvas();
    // draw stuff
}
finally {
    if(null != c) {
        holder.unlockCanvasAndPost(c);
    }
}
forsvarir
  • 10,749
  • 6
  • 46
  • 77
  • I'm trying to use surfaceview for the first time. I wil let you know if it works. Thanks for the pointers ! – m4n07 Apr 13 '11 at 05:53
  • I tried the tutorial which you pointed out. I downloaded the code given in the tutorial and i see on running for a minute it crashes/freeze. As it runs out of memory. Any ideas ? http://stackoverflow.com/questions/5650810/canvas-and-surfaceview-example-crash-freeze-memory-leak – m4n07 Apr 14 '11 at 05:50
0

If you want to have two SurfaceViews, than simply add them to your layout and give them both an own thread (or combine both drawings in one thread).

I don't get it why you want to draw on a separate canvas in your first method sample... Maybe you provide more information for what you try to achieve.

WarrenFaith
  • 57,492
  • 25
  • 134
  • 150
  • http://stackoverflow.com/questions/5635434/android-text-not-visible-on-writing-using-drawtext has some more information – m4n07 Apr 12 '11 at 17:04
  • 1
    OnDraw(Canvas canvas) gets called and i try to draw a graphic object. Then After that i keep retrieving data from Dataoutputstream and i try to display next to the graphic object. – m4n07 Apr 12 '11 at 17:06