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

Apps Refresh view problems

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 ;)!
 
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);
 
Upvote 0
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.
 
Upvote 0

BEST TECH IN 2023

We've been tracking upcoming products and ranking the best tech since 2007. Thanks for trusting our opinion: we get rewarded through affiliate links that earn us a commission and we invite you to learn more about us.

Smartphones