Canvas won't be refreshed

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

Canvas won't be refreshed

Postby Bevor » Sun Jan 31, 2010 10:56 pm

Hi,
I tried to make a simple animation with a ball which moves up and down. The ball will be paint but the canvas won't be refreshed and I don't know why. Look my example:

Syntax: [ Download ] [ Hide ]
Using java Syntax Highlighting
  1.  
  2.     @Override
  3.  
  4.     public void run() {
  5.  
  6.         Canvas c;
  7.  
  8.         timestamp = System.currentTimeMillis();
  9.  
  10.         while (isRunning)   {
  11.  
  12.                 c = null;
  13.  
  14.                 try {
  15.  
  16.                                 c = surfaceHolder.lockCanvas(null);
  17.  
  18.                                 synchronized (surfaceHolder) {
  19.  
  20.                                         sleep(50);
  21.  
  22.                                         if (System.currentTimeMillis() - timestamp >= 200)  {
  23.  
  24.                                                 timestamp = System.currentTimeMillis();
  25.  
  26.                                                 ballJump();
  27.  
  28.                                                 doDraw(c);
  29.  
  30.                                         }
  31.  
  32.                                 }
  33.  
  34.                 } catch (InterruptedException e) { //because of sleep method
  35.  
  36.                         if (c != null)  {   //surface in an inconsistent state
  37.  
  38.                                 surfaceHolder.unlockCanvasAndPost(c);
  39.  
  40.                         }
  41.  
  42.                                 e.printStackTrace();
  43.  
  44.                         }  finally  { //do this in a finally so that if an exception is thrown during the above we don't leave the
  45.  
  46.                         if (c != null)  {   //surface in an inconsistent state
  47.  
  48.                                 surfaceHolder.unlockCanvasAndPost(c);
  49.  
  50.                         }
  51.  
  52.                 }
  53.  
  54.         }
  55.  
  56.     }
  57.  
  58.     /* Callback invoked when the surface dimensions change. */
  59.  
  60.     public void setSurfaceSize(int width, int height) {
  61.  
  62.         // synchronized to make sure these all change atomically
  63.  
  64.         synchronized (surfaceHolder) {
  65.  
  66.             canvasWidth = width;
  67.  
  68.             canvasHeight = height;
  69.  
  70.         }
  71.  
  72.     }
  73.  
  74.    
  75.  
  76.     private void doDraw(Canvas c)  {
  77.  
  78.         c.drawColor(0);
  79.  
  80.         ball.draw(c);
  81.  
  82.  
  83.  
  84.     }
  85.  
  86.    
  87.  
  88.     public void setRunning(boolean run)  {
  89.  
  90.         isRunning = run;
  91.  
  92.     }
  93.  
  94.    
  95.  
  96.     private void moveBall(int x, int y)   {
  97.  
  98.         ball.setBounds(x, y, ballWidth + x, ballHeight + y);
  99.  
  100.     }
  101.  
  102.    
  103.  
  104.     private void ballJump()   {
  105.  
  106.         if (jumpDirection == false)  {//If ball falls
  107.  
  108.                 currentHeight-=2;
  109.  
  110.                 if (currentHeight <= 0 + ballHeight)
  111.  
  112.                         jumpDirection = true;
  113.  
  114.         }
  115.  
  116.         else  {
  117.  
  118.                 currentHeight+=2;
  119.  
  120.                 if (currentHeight >= jumpHeight)  
  121.  
  122.                         jumpDirection = false;
  123.  
  124.                
  125.  
  126.         }              
  127.  
  128.         moveBall(canvasWidth/2-ballWidth/2, canvasHeight-ballHeight-currentHeight);    
  129.  
  130.     }
  131.  
  132.  
Parsed in 0.038 seconds, using GeSHi 1.0.8.4


The problem is, that old paintings of the ball stay painted, although I execute a c.drawColor(0);.
Any ideas why this is so?

Regards.
Bevor
Junior Developer
Junior Developer
 
Posts: 16
Joined: Sat Jan 23, 2010 6:28 pm

Top

Postby BUKsAPPs » Mon Feb 01, 2010 12:10 am

Try changing "c.drawColor(0);" to "c.drawColor(0xFF000000);"

I think that using "0" for your color has an alpha of 0, so you are redrawing the background with a totally transparent black color, which basically does nothing. In what I suggested, the hex value 0xFF000000 will use a fully opaque color of black.

Look here to see more about the Android Color object.

Let me know if this works for you.
BUKsAPPs
Claustrum Lite - Area capture game
R:P:S Academy - play Rock/Paper/Scissors online
BUKsAPPs
Experienced Developer
Experienced Developer
 
Posts: 71
Joined: Sat Jan 16, 2010 9:01 pm
Location: USA

Postby Bevor » Mon Feb 01, 2010 7:57 am

Hello,

thanks for your answer. I will try it when I'm at home tonight.
Bevor
Junior Developer
Junior Developer
 
Posts: 16
Joined: Sat Jan 23, 2010 6:28 pm

Postby Bevor » Mon Feb 01, 2010 7:59 pm

Hmmm, this doensn't work. i don't understand why.
Bevor
Junior Developer
Junior Developer
 
Posts: 16
Joined: Sat Jan 23, 2010 6:28 pm

Postby Bevor » Mon Feb 01, 2010 9:53 pm

When I use a black background Bitmap then it works.

Is the non functioning repainting with the back color a bug?
Bevor
Junior Developer
Junior Developer
 
Posts: 16
Joined: Sat Jan 23, 2010 6:28 pm

Top

Return to View, Layout & Resource Problems

Who is online

Users browsing this forum: Google Feedfetcher and 4 guests