[Solved] Problem with repainting the Canvas of View.

Put problem concerning Views, Layouts and other XML-Resources (like AndroidManifest) here.

[Solved] Problem with repainting the Canvas of View.

Postby martin_nuke » Thu Dec 13, 2007 5:16 am

I have a problem with the Thread refreshing the Canvas of the View.

This is the code it displays the Canvas but is does not Refresh or Repaint it. What seems to be the Problem?

Syntax: [ Download ] [ Hide ]
Using java Syntax Highlighting
  1. package com.google.android.graphicsapp;
  2.  
  3.  
  4.  
  5. import android.app.Activity;
  6.  
  7. import android.os.Bundle;
  8.  
  9.  
  10.  
  11. public class GraphicsApp extends Activity implements Runnable
  12.  
  13. {
  14.  
  15.     /** Called when the activity is first created. */
  16.  
  17.        
  18.  
  19.     Thread thread = null;
  20.  
  21.        
  22.  
  23.         GraphicsView graphicsView = null;
  24.  
  25.        
  26.  
  27.     @Override
  28.  
  29.     public void onCreate(Bundle icicle) {
  30.  
  31.         super.onCreate(icicle);
  32.  
  33.  
  34.  
  35.         this.graphicsView = new GraphicsView(this);
  36.  
  37.         setContentView(this.graphicsView);
  38.  
  39.        
  40.  
  41.         this.thread = new Thread();
  42.  
  43.         this.thread.start();
  44.  
  45.     }
  46.  
  47.    
  48.  
  49.     public void run()
  50.  
  51.     {
  52.  
  53.         while(true)
  54.  
  55.         {
  56.  
  57.                  this.graphicsView.invalidate();
  58.  
  59.                
  60.  
  61.              try
  62.  
  63.              {
  64.  
  65.                  Thread.sleep(1000);
  66.  
  67.              }
  68.  
  69.              catch (InterruptedException e)
  70.  
  71.              {
  72.  
  73.                  
  74.  
  75.              }
  76.  
  77.         }    
  78.  
  79.     }
  80.  
  81. }
Parsed in 0.033 seconds, using GeSHi 1.0.8.4
Last edited by martin_nuke on Thu Dec 13, 2007 9:28 am, edited 2 times in total.
martin_nuke
Junior Developer
Junior Developer
 
Posts: 15
Joined: Sat Dec 08, 2007 7:44 am

Top

Postby martin_nuke » Thu Dec 13, 2007 8:53 am

I also tried this code the Graphics appear but the Canvas does not refresh

Pls. help me

Syntax: [ Download ] [ Hide ]
Using java Syntax Highlighting
  1.  
  2. package com.google.android.graphicsapp;
  3.  
  4.  
  5.  
  6. import android.app.Activity;
  7.  
  8. import android.os.Bundle;
  9.  
  10. import android.view.Window;
  11.  
  12. import android.os.Handler;
  13.  
  14. import android.os.Message;
  15.  
  16.  
  17.  
  18. public class GraphicsApp extends Activity implements Runnable
  19.  
  20. {
  21.  
  22.     /** Called when the activity is first created. */
  23.  
  24.        
  25.  
  26.         protected static final int GUIUPDATEIDENTIFIER = 0x101;
  27.  
  28.        
  29.  
  30.         Handler handler = null;
  31.  
  32.        
  33.  
  34.     Thread thread = null;
  35.  
  36.        
  37.  
  38.     GraphicsView graphicsView = null;
  39.  
  40.        
  41.  
  42.     @Override
  43.  
  44.     public void onCreate(Bundle icicle) {
  45.  
  46.         super.onCreate(icicle);
  47.  
  48.  
  49.  
  50.         requestWindowFeature(Window.FEATURE_NO_TITLE);
  51.  
  52.        
  53.  
  54.         handler = new Handler();
  55.  
  56.                
  57.  
  58.         this.graphicsView = new GraphicsView(this);
  59.  
  60.         setContentView(this.graphicsView);
  61.  
  62.        
  63.  
  64.         this.thread = new Thread();
  65.  
  66.         this.thread.start();
  67.  
  68.     }
  69.  
  70.  
  71.  
  72.     public void run()
  73.  
  74.     {
  75.  
  76.          while(!Thread.currentThread().isInterrupted())
  77.  
  78.          {
  79.  
  80.                   Message message = new Message();
  81.  
  82.               message.what = GraphicsApp.GUIUPDATEIDENTIFIER;
  83.  
  84.               GraphicsApp.this.handler.sendMessage(message);
  85.  
  86.            
  87.  
  88.               try
  89.  
  90.               {
  91.  
  92.                   Thread.sleep(1000);
  93.  
  94.               }
  95.  
  96.               catch (InterruptedException e)
  97.  
  98.               {
  99.  
  100.                  
  101.  
  102.               }
  103.  
  104.          }    
  105.  
  106.     }
  107.  
  108. }
  109.  
  110.  
Parsed in 0.036 seconds, using GeSHi 1.0.8.4
martin_nuke
Junior Developer
Junior Developer
 
Posts: 15
Joined: Sat Dec 08, 2007 7:44 am

Postby plusminus » Thu Dec 13, 2007 9:50 am

Hello martin_nuke,

you second approach was pretty good, but you forgot do your implementation on the Hanlder.
The Message you send goes to the digital nirvana, because the 'empty' default Handler doesn't know how to handle the message :!:

:idea: Note, that the GUIUPDATEIDENTIFIER is a random int I defined, just to recognize the messages sent :!:

This is how you implement the
Excerpt from the Pizza-View handler.
Syntax: [ Download ] [ Hide ]
Using java Syntax Highlighting
  1.      /* The Handler that receives the messages
  2.       * sent out by myRefreshThread every second */
  3.      Handler myPizzaViewUpdateHandler = new Handler(){
  4.           // @Override
  5.           public void handleMessage(Message msg) {
  6.                switch (msg.what) {
  7.                     case PizzaTimer.SECONDPASSEDIDENTIFIER:
  8.                          // ....
  9.                     case PizzaTimer.GUIUPDATEIDENTIFIER:
  10.                          // Redraw our Pizza !!
  11.                          // ....
  12.                          myPizzaView.invalidate();
  13.                          break;
  14.                }
  15.                super.handleMessage(msg);
  16.           }
  17.      };
Parsed in 0.035 seconds, using GeSHi 1.0.8.4


Regards,
plusminus
Image
Image | Android Development Community / Tutorials
User avatar
plusminus
Site Admin
Site Admin
 
Posts: 2688
Joined: Wed Nov 14, 2007 8:37 pm
Location: Schriesheim, Germany

Postby martin_nuke » Fri Dec 14, 2007 5:45 am

Thanks plusminus

This code works

Syntax: [ Download ] [ Hide ]
Using java Syntax Highlighting
  1. package com.google.android.graphicsapp;
  2.  
  3.  
  4.  
  5. import android.app.Activity;
  6.  
  7. import android.os.Bundle;
  8.  
  9. import android.view.Window;
  10.  
  11. import android.os.Handler;
  12.  
  13. import android.os.Message;
  14.  
  15.  
  16.  
  17. public class GraphicsApp extends Activity
  18.  
  19. {
  20.  
  21.     /** Called when the activity is first created. */
  22.  
  23.      
  24.  
  25.     protected static final int GUIUPDATEIDENTIFIER = 0x101;
  26.  
  27.        
  28.  
  29.     Thread thread = null;
  30.  
  31.      
  32.  
  33.     GraphicsView graphicsView = null;
  34.  
  35.  
  36.  
  37.     Handler handler = new Handler()
  38.  
  39.     {
  40.  
  41.         /** Gets called on every message that is received */
  42.  
  43.         // @Override
  44.  
  45.         public void handleMessage(Message msg)
  46.  
  47.         {
  48.  
  49.              switch (msg.what)
  50.  
  51.              {
  52.  
  53.                   case GraphicsApp.GUIUPDATEIDENTIFIER:
  54.  
  55.                        graphicsView.invalidate();
  56.  
  57.                        break;
  58.  
  59.              }
  60.  
  61.              super.handleMessage(msg);
  62.  
  63.         }
  64.  
  65.     };
  66.  
  67.    
  68.  
  69.     @Override
  70.  
  71.     public void onCreate(Bundle icicle) {
  72.  
  73.         super.onCreate(icicle);
  74.  
  75.  
  76.  
  77.         requestWindowFeature(Window.FEATURE_NO_TITLE);
  78.  
  79.                
  80.  
  81.         this.graphicsView = new GraphicsView(this);
  82.  
  83.         setContentView(this.graphicsView);
  84.  
  85.        
  86.  
  87.         this.thread = new Thread(new thread());  
  88.  
  89.         this.thread.start();
  90.  
  91.     }
  92.  
  93.  
  94.  
  95.     class thread implements Runnable
  96.  
  97.     {
  98.  
  99.  //       @Override
  100.  
  101.         public void run()
  102.  
  103.         {
  104.  
  105.              while(!Thread.currentThread().isInterrupted())
  106.  
  107.              {
  108.  
  109.                   Message message = new Message();
  110.  
  111.                   message.what = GraphicsApp.GUIUPDATEIDENTIFIER;
  112.  
  113.                   GraphicsApp.this.handler.sendMessage(message);
  114.  
  115.                  
  116.  
  117.                   try
  118.  
  119.                   {
  120.  
  121.                        Thread.sleep(100);
  122.  
  123.                   }
  124.  
  125.                   catch (InterruptedException e)
  126.  
  127.                   {
  128.  
  129.                        Thread.currentThread().interrupt();
  130.  
  131.                   }
  132.  
  133.              }
  134.  
  135.         }
  136.  
  137.     }
  138.  
  139. }
Parsed in 0.042 seconds, using GeSHi 1.0.8.4
martin_nuke
Junior Developer
Junior Developer
 
Posts: 15
Joined: Sat Dec 08, 2007 7:44 am

Postby cbraun75 » Tue Jun 03, 2008 11:41 am

Hi made this like you did with the Handler.
But I have a problem stopping this thread - see this new topic
see this new topic

Thanks a lot!
cbraun75
Junior Developer
Junior Developer
 
Posts: 23
Joined: Mon Jan 28, 2008 9:12 pm
Location: Munich

Top

Return to View, Layout & Resource Problems

Who is online

Users browsing this forum: No registered users and 6 guests