• After 15+ years, we've made a big change: Android Forums is now Early Bird Club. Learn more here.

Apps using Handler() to display a series of images

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:
public class Panel extends SurfaceView implements SurfaceHolder.Callback {
  private Handler mHandler = new Handler();
  //stuff

  public void doDraw(Canvas canvas) {
        int counter = 0;
        canvas.drawColor(Color.BLACK);
        dot1.doDraw(getResources(), canvas, counter);
        mHandler.removeCallbacks(panelDraw);
        mHandler.postDelayed(panelDraw, 5000);
}

private Runnable panelDraw = new Runnable() {
       public void run() {

       }
};

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? :confused:
 
I moved the Handler code lines to my ViewThread.java page.

Code:
import android.graphics.Canvas;
import android.graphics.Color;
import android.view.SurfaceHolder;
import android.util.Log;
import android.os.Handler;
Code:
public class ViewThread extends Thread {
    private Panel mPanel;
    private SurfaceHolder mHolder;
    private Handler mHandler = new Handler();
    private Canvas canvas1;
	private static final String TAG = "KITTENS";
    
    public ViewThread(Panel panel) {
        mPanel = panel;
        mHolder = mPanel.getHolder();
    }
    
    
    
    @Override
    public void run() {
        	canvas1 = mHolder.lockCanvas();
            if (canvas1 != null) {
            	mHandler.removeCallbacks(panelDraw);
            	canvas1.drawColor(Color.YELLOW);
                mHandler.postDelayed(panelDraw, 5000);
                Log.w(TAG, "ViewThread");
                mHolder.unlockCanvasAndPost(canvas1);
            }
    }
   
	final private Runnable panelDraw = new Runnable() {
		public void run(){
			canvas1.drawColor(Color.RED);
                        Log.w(TAG, "ViewThread2");
		}
	};
}

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?
 
Back
Top Bottom