0

这是我的代码,我正在我的图像视图中动态更改一些图像。公共类 LoadingScreen 扩展 Activity{

public static Integer[] imageList={R.drawable.food_pics1,R.drawable.food_pics2,R.drawable.food_pics3,
    R.drawable.food_pics4,R.drawable.food_pics5,R.drawable.food_pics6,R.drawable.food_pics7,
    R.drawable.food_pics8,R.drawable.food_pics9};
Thread thread;
ImageView foodImageView;
final Handler myHandler=new Handler();
public int currentImageIndex=0;

@Override
protected void onCreate(Bundle savedInstanceState) {
    // TODO Auto-generated method stub
    super.onCreate(savedInstanceState);
    requestWindowFeature(Window.FEATURE_NO_TITLE);
    setContentView(R.layout.load_xml);
    foodImageView=(ImageView)findViewById(R.id.imageView_food);
    //      final int i=0;

    final Runnable runnable = new Runnable() {
        @Override
        public void run() {
            // TODO Auto-generated method stub
            animateImages();
        }


    };

    final int delay=500;            
    final long period=1000;
    Timer timer=new Timer();

    timer.scheduleAtFixedRate(new TimerTask() {

        @Override
        public void run() {
            // TODO Auto-generated method stub
            myHandler.post(runnable);
        }
    }, delay, period);
}

private void animateImages() {
    // TODO Auto-generated method stub
        foodImageView.setImageResource(imageList[currentImageIndex%imageList.length]);
        currentImageIndex++;
        foodImageView.getAnimation();
}

我想停止计时器并在 20 秒后完成此活动。我该怎么做。

4

3 回答 3

1

试试用这个,

 View v = new View(this);
        v.postDelayed(new Runnable(){
            public void run() {
                // TODO Auto-generated method stub
                //cancel your animation and finish the Activity here.
                finish();
            }
         }, (long) 20000.0);
于 2012-04-09T11:01:02.633 回答
0

你可以这样说:

timer.cancel();
timer = null;
于 2012-04-09T10:23:56.783 回答
0

将 TimerTask 实例保存在类中并在其上调用取消

这是一个我发现在不使用 Java 的 Timer 的情况下非常有用的提示:尝试在 UI 线程中同步事物,特别是如果你想做 UI 任务。例子:

... onCreate() {
getUiThreadHandler().post(new Runnable(){
  if (canceled()) {
    return;
  }

  // Actual work
  ...

  // Invoke again after delay
  getUiThreadHandler().postDelayed(this, 500);
});

祝你好运

于 2012-04-09T10:35:16.317 回答