Android 如何停止动画
我已经创建了一个反弹球的动画(通过下面的代码) 我想知道如何在特定条件下停止此动画,例如10秒后或球到达特定坐标时 守则:Android 如何停止动画,android,animation,Android,Animation,我已经创建了一个反弹球的动画(通过下面的代码) 我想知道如何在特定条件下停止此动画,例如10秒后或球到达特定坐标时 守则: public class MyDemoView extends ImageView{ private Context mContext; int x = -1; int y = -1; private int xVelocity = 10; private int yVelocity = 5; private Handler
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);
}
}
公共类MyDemoView扩展了ImageView{
私有上下文;
int x=-1;
int y=-1;
私有int xVelocity=10;
私人住宅单位面积=5;
私有处理器h;
专用最终整数帧_率=30;
公共MyDemoView(上下文、属性集属性){
超级(上下文,attrs);
mContext=上下文;
h=新处理程序();
}
private Runnable r=new Runnable(){
@凌驾
公开募捐{
使无效();
}
};
受保护的void onDraw(画布c){
BitmapDrawable ball=(BitmapDrawable)mContext.getResources().getDrawable(R.drawable.ball);
如果(x this.getHeight()-ball.getBitmap().getHeight())| |(y<0)){
yVelocity=yVelocity*-1;
}
}
c、 drawBitmap(ball.getBitmap(),x2,y2,null);
h、 后延迟(r,帧速率);
}
}
根据您的代码,只需调用h.removeCallbacks(r)
要停止动画,请使用以下代码:
object.clearAnimation();
或
后者可能不适用于2.1,我不记得了
animation.cancel();