android - How to stop an animation -
i have created animation (by below code) of bouncing ball.
i wanted know how stop animation on particular condition, after 10 seconds or when ball reaches @ particular coordinates.
the code:
public class mydemoview extends imageview{ private context mcontext; int x = -1; int y = -1; private int xvelocity = 10; private int yvelocity = 5; private handler h; private final int frame_rate = 30; public mydemoview(context context, attributeset attrs) { super(context, attrs); mcontext = context; h = new handler(); } private runnable r = new runnable() { @override public void run() { invalidate(); } }; protected void ondraw(canvas c) { bitmapdrawable ball = (bitmapdrawable) mcontext.getresources().getdrawable(r.drawable.ball); if (x<0 && y <0) { x = this.getwidth()/2; y = this.getheight()/2; } else { x += xvelocity; y += yvelocity; if ((x > this.getwidth() - ball.getbitmap().getwidth()) || (x < 0)) { xvelocity = xvelocity*-1; } if ((y > this.getheight() - ball.getbitmap().getheight()) || (y < 0)) { yvelocity = yvelocity*-1; } } c.drawbitmap(ball.getbitmap(), x2, y2, null); h.postdelayed(r, frame_rate); } }
to stop animation use code below:
object.clearanimation();
or
animation.cancel();
the latter may not work 2.1, can't remember.
Comments
Post a Comment