Android 我能';不要用定时器播放动画
当我尝试从计时器中设置图像动画时,我不能这样做,但是会执行Toast。动画工作正常,因为我从一个按钮测试。这是罕见的,只有土司运行,并没有失败的应用程序。 有人可以帮我Android 我能';不要用定时器播放动画,android,timer,android-animation,android-imageview,timertask,Android,Timer,Android Animation,Android Imageview,Timertask,当我尝试从计时器中设置图像动画时,我不能这样做,但是会执行Toast。动画工作正常,因为我从一个按钮测试。这是罕见的,只有土司运行,并没有失败的应用程序。 有人可以帮我 public class Ayuda extends Activity { ImageView imagen_toro, imagen_vaca; Animation rotar; Timer timer; TimerTask timerTask;
public class Ayuda extends Activity {
ImageView imagen_toro, imagen_vaca;
Animation rotar;
Timer timer;
TimerTask timerTask;
final Handler handler = new Handler();
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_ayuda);
imagen_toro = (ImageView) findViewById(R.id.imagen_toro_ayuda);
imagen_vaca = (ImageView) findViewById(R.id.imagen_vaca_ayuda);
rotar = AnimationUtils.loadAnimation(this, R.animator.animar_toro);
timer = new Timer();
timerTask = new TimerTask() {
public void run() {
runOnUiThread(new Runnable() {
public void run() {
rotar.reset();
imagen_toro.setAnimation(rotar);
rotar.reset();
imagen_vaca.setAnimation(rotar);
int duration = Toast.LENGTH_SHORT;
Toast toast = Toast.makeText(getApplicationContext(), "test", duration);
toast.show();
}
});
}
};
timer.schedule(timerTask, 3000, 3000); //
}
}
首先,您应该使用PostDealyed而不是计时器。 此外,还需要调用“开始动画”
imageView.postDelayed(new Runnable() {
@Override
public void run() {
imagen_toro.startAnimation(animation)
}
}, 3000);
一般来说,使用定时器被认为是不好的做法。即使是Android文档也建议在运行定期任务的一般情况下使用
ScheduledThreadPoolExecutor
。此外,Android动画应该使用特定于动画的API来运行,以避免错误
我建议使用rotar.setAnimationListener()
使用imagen\u vaca.startAnimation(rotar)重新启动动画代码>
你也应该考虑使用新的.< /P>