android - Animating bitmap ball - hit the wall and change direction effect -


i trying animate bitmap. have ball, , move it. when reaches boundaries of screen, want change direction mirror effect, mean this, think of ball dots below, comes down, hits wall , changes direction.

.          .  .       .   .    . ____._._______ 

here code:

public class drawshapes extends view{  bitmap ball; int x,y;  public drawshapes(context context) {     super(context);     ball=bitmapfactory.decoderesource(getresources(),r.drawable.ball);     x=0;     y=0; }  @override protected void ondraw(canvas canvas) {           super.ondraw(canvas);             if(x<canvas.getwidth())         x+=5;     else         x-=5;     if(y<canvas.getheight())         y+=5;     else         y-=5;     canvas.drawbitmap(ball, x, y, new paint());     invalidate();  } 

the problem is, after ball reaches boundary, keeps going , goes out of screen , never comes back. can me this?

thanks

hmmm...if canvas larger screen, explain why "goes out of screen."

and wouldn't come because logic there, ball should bounce , forth between same spot. here's see:

canvas.width == 10  iteration1 x == 5 x += 5 x == 10  iteration2 x == 10 x -= 5 x == 5  iteration3 x == 5 x += 5 x == 10 

Comments

Popular posts from this blog

java - Run a .jar on Heroku -

java - Jtable duplicate Rows -

validation - How to pass paramaters like unix into windows batch file -