2012-04-17 15 views
0

我開發一個機器人應用,其中我使用的GIF圖像用下面的代碼在表面視圖中使用Gif圖像?

private static byte[] streamToBytes(InputStream is) { 
    ByteArrayOutputStream os = new ByteArrayOutputStream(1024); 
    byte[] buffer = new byte[1024]; 
    int len; 
    try { 
     while ((len = is.read(buffer)) >= 0) { 
      os.write(buffer, 0, len); 
     } 
    } catch (java.io.IOException e) { 
    } 
    return os.toByteArray(); 
} 

//在通過從樣活性ontouch我出口類

is = this.context.getResources().openRawResource(R.drawable.fondo_juego_nexus); 

      if (DECODE_STREAM) { 
        System.out.println("in if DECODE_STREAM"); 
       mMovie = Movie.decodeStream(is); 
      } 

      else { 
       byte[] array = streamToBytes(is); 
       System.out.println("in else DECODE_STREAM"); 
       mMovie = Movie.decodeByteArray(array, 0, array.length); 
      } 



// In On Draw 

    long now = android.os.SystemClock.uptimeMillis(); 

     if (mMovieStart == 0) { // first time 
      mMovieStart = now; 
     } 
     if (mMovie != null) { 
      System.out.println("in if (mMovie != null) " + mMovie.duration()); 
      int dur = mMovie.duration(); 
      if (dur == 0) 
      { 
       dur = 1000; 
       System.out.println("in if movie if"); 
      } 
      System.out.println("duration is "+ dur); 
      int relTime = (int)((now - mMovieStart) % dur); 
      mMovie.setTime(relTime); 
      System.out.println("in if displaying syd"); 
      mMovie.draw(canvas,120,100); 

     } 

的構造和

else if(_y<=60 && _x<=60) 
     { 


      sp.play(mySound1, volume, volume, 0, 0, 1); 
      sp.release(); 
      playr.stop(); 
      tme.cancel(); 
      act.finish(); 

} 

卜當我用上面的方法退出活動並返回到以前的活動, 又一次來到活動WH我使用gif圖像它不出現,在設備星系s2,2.3.3,但在一個相同的大小爲2.2的模擬器是好的

是否有任何問題,這種方法或我可以用什麼方式來顯示GIF圖像

我應該怎麼做才能消除這種錯誤

回答

1

我得到了我的問題的解決方案,我不知道它有多好, 但它的工作對我來說我最早是使用在構造函數中的代碼

if (DECODE_STREAM) { 
       mMovie = Movie.decodeStream(is); 
      } else { 
       byte[] array = streamToBytes(is); 
       mMovie = Movie.decodeByteArray(array, 0, array.length); 
      } 

     } 

有人workign˚F第一次,但我認爲在第二次它是內存不足,因爲 太多的垃圾收集器,這是隱式運行 和我的圖像初始化也一次又一次地運行,這是造成的 糟糕的性能遊戲, 我所做的就是我剛纔添加 手動垃圾收集與

System.gc(); 

並在此之後初始化所有圖像,也 放在下面的代碼

if (DECODE_STREAM) { 
       mMovie = Movie.decodeStream(is); 
      } else { 
       byte[] array = streamToBytes(is); 
       mMovie = Movie.decodeByteArray(array, 0, array.length); 
      } 

     } 

在此之後 這就避免了垃圾收集器的所述隱式運行

現在GIF圖像工作正常