1. Download our Official Android App: Forums for Android!

Apps using Handler() to display a series of images

Discussion in 'Android Development' started by goshdarnit, Jun 28, 2011.

  1. goshdarnit

    goshdarnit Lurker
    Thread Starter
    Rank:
    None
    Points:
    5
    Posts:
    7
    Joined:
    Jun 21, 2011

    Jun 21, 2011
    7
    0
    5
    I am trying to put an image on the screen and change it every five seconds. This is not in an Activity class. Sorry if is a stupid question. This is a huge learning curve for me.

    Code (Text):
    1. public class Panel extends SurfaceView implements SurfaceHolder.Callback {
    2.   private Handler mHandler = new Handler();
    3.   //stuff
    4.  
    5.   public void doDraw(Canvas canvas) {
    6.         int counter = 0;
    7.         canvas.drawColor(Color.BLACK);
    8.         dot1.doDraw(getResources(), canvas, counter);
    9.         mHandler.removeCallbacks(panelDraw);
    10.         mHandler.postDelayed(panelDraw, 5000);
    11. }
    12.  
    13. private Runnable panelDraw = new Runnable() {
    14.        public void run() {
    15.  
    16.        }
    17. };
    doDraw is a function that draws the image. My problem is that I can't put dot1.doDraw(getResources(), canvas, counter) under public void run() because I cannot pass the parameters to it. (Eclipse gives me errors.) Why doesn't it work if I do the draw statement and then the delay where the called function does nothing? :thinking:
     

    Advertisement

  2. goshdarnit

    goshdarnit Lurker
    Thread Starter
    Rank:
    None
    Points:
    5
    Posts:
    7
    Joined:
    Jun 21, 2011

    Jun 21, 2011
    7
    0
    5
    I moved the Handler code lines to my ViewThread.java page.

    Code (Text):
    1.  
    2. import android.graphics.Canvas;
    3. import android.graphics.Color;
    4. import android.view.SurfaceHolder;
    5. import android.util.Log;
    6. import android.os.Handler;
    7.  
    8.  
    Code (Text):
    1. public class ViewThread extends Thread {
    2.     private Panel mPanel;
    3.     private SurfaceHolder mHolder;
    4.     private Handler mHandler = new Handler();
    5.     private Canvas canvas1;
    6.     private static final String TAG = "KITTENS";
    7.    
    8.     public ViewThread(Panel panel) {
    9.         mPanel = panel;
    10.         mHolder = mPanel.getHolder();
    11.     }
    12.    
    13.    
    14.    
    15.     @Override
    16.     public void run() {
    17.             canvas1 = mHolder.lockCanvas();
    18.             if (canvas1 != null) {
    19.                 mHandler.removeCallbacks(panelDraw);
    20.                 canvas1.drawColor(Color.YELLOW);
    21.                 mHandler.postDelayed(panelDraw, 5000);
    22.                 Log.w(TAG, "ViewThread");
    23.                 mHolder.unlockCanvasAndPost(canvas1);
    24.             }
    25.     }
    26.    
    27.     final private Runnable panelDraw = new Runnable() {
    28.         public void run(){
    29.             canvas1.drawColor(Color.RED);
    30.                         Log.w(TAG, "ViewThread2");
    31.         }
    32.     };
    33. }
    34.  
    I removed a lot of stuff in order to test it. The screen turns yellow and doesn't change. It does reach panelDraw because ViewThread2 prints. I have no idea what's wrong. Any help?
     

Share This Page

Loading...