Apps Refresh view problems

tibo-k3

Lurker
Hello everyone!

I am actually programming a video game with android, and I a have a little problem. I am using a timer to increase two integers, which are the position of my picture on a view. However, when I am using this timer, the integers are increased but the view does not refresh, even if I use the method myview.Invalidate().

This my code:

package SpaceInvader.pack;

import java.util.TimerTask;

import android.graphics.Canvas;

public class timertask extends TimerTask{

int abscisse;
int ordonnee;
MyView m;
Canvas c;

public timertask(int x, int y, MyView myview, Canvas canvas ) {
abscisse=x;
ordonnee=y;
m = myview;
c=canvas;
}

@Override
public void run() {
c.drawBitmap(m.bLogo, abscisse, ordonnee, m.mPaint);
abscisse++;
ordonnee++;

m.invalidate();
//m.refreshDrawableState();
//m.postInvalidate();
//m.forceLayout();
//m.loadLogo();
//m.requestLayout();
//m.setFocusable(true);
}


}

Thanks for your help ;)!
 

markb

Well-Known Member
I think the problem might be that you created the GUI elements in one thread but are updating them in another thread. I believe you can only update GUI elements from the thread that created them. One way around that would be for your TimerTask to communicate back to the main GUI thread, asking it to refresh them.

Mark
 

Boogs

Member
tibo-k3,

I think markb is right - you should try sending a message back to your main thread to update/draw the widgets on screen. Doing that sort of thing from another thread usually causes problems like you're describing. If you need help on how to do that, then let us know and someone will help you out.

Also, as a tip, please enclose any source code inside the "code" tags in your post. It makes it much easier to read. E.g.:
Code:
public static final String USING_CODE_TAGS = "A Good Idea";
TextView important = (TextView)findViewById(R.id.important);
important.setText("Using code tags is " + USING_CODE_TAGS);
 
Try something like this:
Code:
public class MyGame extends Activity {
	int abscisse;
	int ordonnee;
	Paint paint = new Paint();
	Bitmap bLogo;

	class MyView extends SurfaceView {

		public MyView(Context context, AttributeSet attrs) {
			super(context, attrs);
			loop();
		}

		private void loop() {
			new Thread(new Runnable() {

				@Override
				public void run() {
					while (true) {
						Canvas c = null;
						SurfaceHolder holder = getHolder();
						try {
							c = holder.lockCanvas(null);
							if (c != null) {
								synchronized (holder) {
									doUpdate();
									doDraw(c);
								}
							}
						} catch (Exception e) {
							e.printStackTrace();
						} finally {
							if (c != null) {
								holder.unlockCanvasAndPost(c);
							}
						}
					}
				}
			}).start();
		}
	}

	public void doUpdate() {
		abscisse++;
		ordonnee++;
	}

	public void doDraw(Canvas c) {
		c.drawBitmap(bLogo, abscisse, ordonnee, paint);
	}

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		MyView my = new MyView(getApplicationContext(), null);
		setContentView(my);
		// load bLogo here
	}
}
This is a basic code to you program a game. You can improve it a lot.
 
Top