1
          @Override 
          protected void onDraw(Canvas canvas) 
          {
            //Note:I do not  want to use the canvas object from this function param
            //If i do so its working , But i would like to understand why the following is not working

         Canvas c =new Canvas();
             Paint paint = new Paint();
             paint.setStyle(Paint.Style.FILL);
             paint.setAntiAlias(true);
             paint.setColor(Color.WHITE);
             c.drawText("HELLO CANVAS",200,300,paint);
        }

MORE CODE

public class graphicProj extends Activity {

   private Canvas canvas;

    @Override
    public void onCreate(Bundle savedInstanceState) {
    {
        ....

        SimpleView simpleview_obj = new SimpleView(this);
        setContentView(simpleview_obj);
        simpleview_obj.onDraw(canvas); 
         .....

     new GetData().execute();

     }
    private static class SimpleView extends View {
        private ShapeDrawable mDrawable = new ShapeDrawable();
....    
    protected void onDraw(Canvas canvas) {

    //draw graphic objects
    ....
        }
     }

    public class GetData extends AsyncTask<Void, String, Void> {

         @Override
        protected void onPreExecute() {
             Log.d("PROJ","STARTIN");
        }

        @Override
        protected Void doInBackground(Void... unused) {
        ////My calculation and reading frm DataStream

         }

        @Override
        protected void onProgressUpdate(String... data) {

           //I Keep updating the result...
            Paint paint = new Paint();
            paint.setStyle(Paint.Style.FILL);
            paint.setAntiAlias(true);
            paint.setColor(Color.WHITE);
            canvas.drawText(result, 200, 300, paint);


        }

        @Override
        protected void onPostExecute(Void unused) {
             Log.d("PROJ","END");
        }
    }

}
m4n07
  • 2,267
  • 12
  • 50
  • 65
  • How do you know it's not working? – Rajath Apr 12 '11 at 12:58
  • If i change the last line to canvas.drawText("HELLO CANVAS",200,300,paint); It works. But i would like to know why the above one is not working. Not working in the sense ,Its not printing any characters on the screen !!! – m4n07 Apr 12 '11 at 12:59
  • 1
    Because the screen uses `canvas`, not `c` ... which is why the system is providing you with that specific canvas to draw on. – Rajath Apr 12 '11 at 13:01
  • 1
    Your line of questioning doesn't make sense from a practical point of view. Why would you need two canvases? – Rajath Apr 12 '11 at 13:07
  • Because i'm doing a calculation outside onDraw() and this calculation function gets called from onCreate() . So in onDraw() i display a graphic object ,followed by that i wanted to print the calculated value. Since i dont have this canvas object as its local to onDraw() – m4n07 Apr 12 '11 at 13:10
  • Do all your printing on the Canvas object provided by `OnDraw()` – Rajath Apr 12 '11 at 13:15
  • Sorry i cant do that ! Any other ideas are welcome ! – m4n07 Apr 12 '11 at 13:36
  • Well actually it's not really local to onDraw, as it's likely a ref to the activities canvas. (just a guess).. The way to do what you're after is to set the text of the object outside (before or after) on draw, and then force the draw. – baash05 Sep 23 '11 at 12:35

1 Answers1

4

Not here or in your other question have you provided enough information on why you can't do that. There is no reason to draw on a new canvas instead of the already existing one.

The code is not working because your new Canvas c isn't assigned to anything. Its like creating a String myString for a log but never using Log.d(tag, myString)

edit (after reading all the comments)

If you calculate a value in your onCreate() and want to display that value in your onDraw(), that simply do that. Store the result in a member variable and you can access it in the onDraw().

Otherwise: Please provide your complete code. I guess you just do it way more complex than it should be...

edit2

Your code is a bit messy and does a lot of stuff in areas where you shouldn't do it. So drawing inside the onProgressUpdate() is seriously wrong. You should encapsulate your calculation and drawing.

What you should do (I recommend using SurfaceView instead of View, anyway...):

You should start your AsynchTask which updates the string you want to draw. The string should be a variable inside your View, where you use it for drawing. The drawing itself should be called by a drawing thread (I remember: use the SurfaceView instead of the View as a parent class). Inside that onDraw() you should just use your paint object, the given canvas and the string you want to draw (don't forget to make the paint variable also a member variable to prevent recreating the same object over and over again for performance/memory reasons).

If you do not know how to work with a SurfaceView or if you want to learn how you could work with a drawing thread please read my tutorial about 2d drawing: www.droidnova.com/2d-tutorial-series

A short last sentence: You did a lot of things in the right way, you just mixed up with the places where you do it. You should try to rethink what you really want to achieve and how it could be done the easiest way. Maybe my tutorial helps to clear your mind a bit.

WarrenFaith
  • 57,492
  • 25
  • 134
  • 150
  • Updated the code ! First i call onDraw() to draw graphic objects in the screen. Then after that i try to do calculation continuously(hence Async activity) and keep displaying the result. – m4n07 Apr 12 '11 at 17:35
  • I was trying the surface view example from this link . But unfortunately it runs for only a minute or so.http://stackoverflow.com/questions/5650810/canvas-and-surfaceview-example-crash-freeze-memory-leak – m4n07 Apr 14 '11 at 05:48
  • @WarrenFaith can you help me on this [displaying the text in Canvas ShapeDrawable with RectShape?](http://stackoverflow.com/questions/25401981/how-to-display-text-in-android-canvas-shapedrawable-with-in-the-rectshape-or-ova) no effects, failed to display text on Shape after doing this _sDrawableRef.getPaint().getTextBounds(s, 0, s.length(), bounds);_ – LOG_TAG Aug 21 '14 at 06:11