Updating view via runnable

Put your problem here if it does not fit any of the other categories.

Updating view via runnable

Postby sasuke » Thu Mar 27, 2008 2:42 pm

Hi guys,

i have a view that i want to invalidate x (a variable) times (with a delay).

every iteration, i check the value from other variable (lets say var. direction),
if direction is up, then draw_up
if direction is down, then draw_down

etc...

i have done it with handler (handle message), thread (delay and send message). Is there any better way to do it :?:

here's my code:

The runnable
Syntax: [ Download ] [ Hide ]
Using java Syntax Highlighting
  1.  
  2.             public synchronized void run() {
  3.  
  4.                         x = some_value;
  5.  
  6.                         for(int i = 0;i<x;i++) {
  7.  
  8.                            msg = new Message();
  9.  
  10.                            // check the value of x, and set the msg (msg.what) according to the value of x
  11.  
  12.                    try {
  13.  
  14.                            mMovementHandler.sendMessage(msg);
  15.  
  16.                                    //i want the invalidate process visible to user, so i put some delay
  17.  
  18.                        Thread.sleep(300);
  19.  
  20.                     } catch (InterruptedException e) {
  21.  
  22.                         Thread.currentThread().interrupt();
  23.  
  24.                     }
  25.  
  26.                         }
  27.  
  28.                         Thread.currentThread().interrupt();
  29.  
  30.             }
  31.  
  32.  
Parsed in 0.032 seconds, using GeSHi 1.0.8.4


The handler
Syntax: [ Download ] [ Hide ]
Using java Syntax Highlighting
  1.  
  2.         private Handler mMovementHandler = new Handler(){
  3.  
  4.         @Override
  5.  
  6.         public void handleMessage(Message msg) {
  7.  
  8.              switch (msg.what) {
  9.  
  10.                 case MSG_UP:
  11.  
  12.                         if (moveUp()) invalidateScreen(UP);
  13.  
  14.                         break;
  15.  
  16.                 case MSG_RIGHT:
  17.  
  18.                         if (moveRight()) invalidateScreen(RIGHT);
  19.  
  20.                         break;
  21.  
  22.                 case MSG_DOWN:
  23.  
  24.                         if (moveDown()) invalidateScreen(DOWN);
  25.  
  26.                         break;
  27.  
  28.                 case MSG_LEFT:
  29.  
  30.                         if (moveLeft()) invalidateScreen(LEFT);
  31.  
  32.                         break;
  33.  
  34.              }
  35.  
  36.              super.handleMessage(msg);
  37.  
  38.         }
  39.  
  40.         };
  41.  
  42.  
Parsed in 0.033 seconds, using GeSHi 1.0.8.4


regards,
sasuke
sasuke
Experienced Developer
Experienced Developer
 
Posts: 57
Joined: Fri Nov 30, 2007 1:25 pm

Top

Return to Other Coding-Problems

Who is online

Users browsing this forum: Google [Bot] and 10 guests