This is what i currently have in my MainActivity.java.
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
MyBall ball = new MyBall(this);
setContentView(ball);
}
}
and this is my canvas class
public class MyBall extends View {
Bitmap myBall;
int gap = 10;
String direction = "down";
float xPos = 0;
float yPos = 0;
public MyBall(Context context) {
super(context);
myBall = BitmapFactory.decodeResource(getResources(), R.drawable.ball3);
}
protected void onDraw(Canvas canvas){
super.onDraw(canvas);
canvas.drawColor(Color.WHITE);
canvas.drawBitmap(myBall, xPos, yPos, null);
invalidate();
}
}
I want the move from the upper left, to the middle right, middle left, to the lower right and then cycle back. I am also having problems when i change the orientation to landscape because the width of the canvas changes and everything is ruined after that.
[EDIT]
protected void onDraw(Canvas canvas){
super.onDraw(canvas);
canvas.drawColor(Color.WHITE);
if(direction.equals("down"))
{
if(yPos < canvas.getHeight()/2 - myBall.getWidth()){
xPos+=gap;
yPos+=gap;
}
else {
direction = "up";
Log.d("direction", xPos + "help");
}
}
if(direction.equals("up")){
if (xPos > myBall.getWidth()) {
xPos-=gap;
yPos+=gap;
}
}
canvas.drawBitmap(myBall, xPos, yPos, null);
invalidate();
}
This is where i am stuck. The ball goes to the middle left, but when the orientation and changed to landscape the ball goes down and doesn't go up.