开发者

Android loading and showing a lot of images in ImageView(frame by frame animation) hangs in certain moments

I've created an application that show around 250 images in ImageView. Images are loaded one after another, 15-30 ima开发者_开发百科ges per second. Basically the whole thing gives an illusion of a rotating 3D object, at least it should. The problem is next, app hangs when loading certain images(i.e. I see a few seconds of fluid animation and then animation hangs, jump 10-15 frames(images) ahead and continues. It always happens at the same places in animation cycle. I though that Android might not have enough resources to handle something like this, so I've resized images to half their size, but it did't help. I've tried buffering images but that did't help either(actually, maybe a little, I think that animation looks a little bit smoother). And now the weirdest thing. I use the touch screen to allow users to "rotate" the 3D object on those images, and while rotating I again experience those hangs at exactly the same places as with the animation.

All images are in .png format and their size vary from 15kB to 40kB.

I use the following code for the animation:

new Thread(new Runnable() {

   @Override
   public void run() {
    while (!stopStartupAnimation && li < images_360.length) {
     final int fli = li;
     handler.post(new Runnable() {

      @Override
      public void run() {
       //Bitmap b = BitmapFactory.decodeResource(getResources(), R.drawable.icon);
       //imageCanvas.setImageResource(images_360[fli]);
       imageCanvas.setImageBitmap(imageStackNext.pop());
       System.out.println("rawX = " + fli);
      }
     });
     int ti = fli +25;
     if(ti > images_360.length-1){
      ti = ti - images_360.length;
     }
     imageStackNext.push(BitmapFactory.decodeResource(getResources(), images_360[ti]));
     synchronized (this) {
      try {
       wait(1000 / 25);
      } catch (InterruptedException e) {
       e.printStackTrace();
      }
     }
     li++;
     li++;
     if (li >= images_360.length) {
      li = 0;
     }
    }
   }
  }).start();


First, 15-40KB is their compressed form. Uncompressed, as Bitmaps, they are probably substantially larger. 250 of them may be using many MB of RAM, which is not a good idea.

Second, given a choice between using OpenGL for 3D (which is its purpose), or the 2D drawing primitives on the Canvas, or using ImageView, you chose the worst-performing option.

Third, postRunnable() does not take effect immediately, but rather puts things on a message queue for the main application thread to process when it gets a chance. If it gets tied up -- say, handling touch events -- it may well skip over some seemingly redundant ImageView redraws, or have them go by so fast they appear to not happen. All your 40ms wait() does is ensure that you are only raising events every 40ms, not that they will paint every 40ms. Besides, you could have more easily just used postDelayed() for your 40ms timing.


Bitmaps should be loaded efficiently. Refer example on official page: https://developer.android.com/training/displaying-bitmaps/index.html

0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜