dynamically change bitmap in imageView, android


Ask by : Junfei Wang November 03, 2012 23:17

All,

I have a problem related to imageView, android.
I have array which contains of 10 bitmap objects, called bm.
I have a imageView, called im.

Now I wanna show the bitmaps in the array in im one by one, so I did the following:

new Thread(new Runnable() {
       public void run() {
         for(int j=0;j<10;j++){
             im.setImageBitmap(bm[j]);
         }
       }
      }).start();

But the result only shows the last bitmap in the array.

Can someone tell me what to do with this issue?

Millions of thanks!

View original question

Answer by : DeeVNovember 03, 2012 23:05

  1. Don't ever change a UI element in a thread. Most of the time this results in an exception. I don't know why it didn't here.

  2. You're blasting through all ten images in a mere milliseconds. That's why the last one is the only one you see. You need to slow it down.

  3. The easiest way would be the Handler/Runnable implementation like so:

    im.post(new Runnable() {   
       int j = 0;      
       @Override   
       public void run() {
          im.setImageBitmap(bm[j]);
          if(j++ < 10){
             im.postDelayed(this, 1000);
          }
       }
    });
    
View original answer