I have this code that should move an object down but it stands still can someone say what is wrong??
First code
The view class were I display the object code.
import android.app.Activity;
import android.os.Bundle;
public class Game extends Activity {
Play View;
@Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
View = new Play(this);
setContentView(View);
}
}
Secound code
This is the code were the object should move.
import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.Color;
import android.view.View;
public class Play extends View {
int Y=0;
Bitmap object;
public Play(Context context) {
super(context);
// TODO Auto-generated constructor stub object = BitmapFactory.decodeResource(getResources(), R.drawable.brid);
}
@Override
protected void onDraw(Canvas canvas) {
// TODO Auto-generated method stub
super.onDraw(canvas);
canvas.drawColor(Color.GRAY);
canvas.drawBitmap(object, (canvas.getWidth()/2), Y, null);
if (Y < canvas.getHeight()){
Y +=10;
}else {
Y = 0;
}
}
}
First off don’t call your
Playobject “View” since that refers to an Android class. Name it something like “playView”…To answer your question, a
Viewdoesn’t redraw itself when theonDraw()completes. To make it do that you should callinvalidate()at the end ofonDraw().To make the drawing smooth I would recommend putting this into a
Threadand make a game loop to run the drawing. Look intoSurfaceViewas that is faster than drawing straight to aCanvasfrom aView.