Android: Text not visible on writing using drawText()

前端 未结 1 1537
情书的邮戳
情书的邮戳 2021-01-25 06:58
          @Override 
          protected void onDraw(Canvas canvas) 
          {
            //Note:I do not  want to use the canvas object from this function param
             


        
相关标签:
1条回答
  • 2021-01-25 07:58

    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.

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