开发者

Setting images with a timer

I am trying to set images with a 600ms delay using a countdown timer as follows:

protected void onCreate(Bundle savedInstanceState) {
int order[3];
index=0;
  image1.setOnClickListener(this);

   @Override
   public void onClick(View arg0) 
   {
   order={2,1,3};

   do_switches();
   }

private void do_switches() 
{
   new CountdownTimer(3*600, 600) {

 public void onTick(long millisUntilFinished) {
     switch(order[index]){

    case 1:

        image1.setImageResource(R.drawable.one);
                break;

    case 2:

        image2.setImageResource(R.drawable.two);
                         break;

    case 3:
   开发者_JAVA百科     image3.setImageResource(R.drawable.three);
                    break;

    }
    index++;

 }

 public void onFinish() {

 }
  }.start();

}

}

This is an example but in my actual code, I use larger arrays (about 50 in size). My problem is, the countdown timer does not always go all the way to the last spot in the array. Sometimes works but other times it stops before reaching the last point of my array. However if I increase the interval time to be 2000ms it always works. Its definitely some processing going on that delays this but I don't expect a countdown to not work just because I have reduced the interval. Can someone tell me what I can do to make sure the countdown timer does not stop before going through my whole array or is there a better way to do this.

I also tried using a timer with scheduleAtFixedRate but I got an error saying I cannot touch views in a thread because they were not constructed in the thread

Please help Thanks


I'm sure there are other details involved and this may not cure everything, but for the last bit ("...cannot touch views in a thread..."), you need to pass a Runnable to Activity.runOnUiThread. In that Runnable, do your setting of image resources and anything else that will affect the screen.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜