2013-07-10 45 views
0

我正在開發一個android遊戲。它幾乎已經完成並在10.1平板電腦,nexus 7,nexus 4,HTC senstation上嘗試過,它可以在所有這些平臺上運行。然而,當我在Galaxy s3上試用它時,我在幀動畫xml中調用圖片時出現了OutofMemory錯誤。現在我通過調整xhdpi文件夾中的圖片來解決這個問題,但結果並不令人滿意。有沒有足夠的方法可以用來重新縮放在動畫中顯示的圖片以進行幀動畫。OutOfMemory錯誤android

下面是代碼:

public class Logo extends Activity { 

    private ImageView iv; 
    Thread timer; 

    @Override 
    protected void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 

     setContentView(R.layout.activity_logo); 

     iv = (ImageView) findViewById(R.id.imageView1); 
     iv.setBackgroundResource(R.anim.anime); 

     // thread for animating the logo and moving to next activity after 
     // display. 
     timer = new Thread() { 

      @Override 
      public void run() { 
       try { 
        sleep(7000); 
       } catch (InterruptedException e) { 
        e.printStackTrace(); 
       } finally { 
        //Intent i = new Intent(); 
        //i.setClass(Logo.this, StartPoint.class); 
        //startActivity(i); 
       } 
      } 
     }; 
    } 

    @Override 
    public void onWindowFocusChanged(boolean hasFocus) { 
     // TODO Auto-generated method stub 
     super.onWindowFocusChanged(hasFocus); 
     AnimationDrawable anime = (AnimationDrawable) iv.getBackground(); 
     if (hasFocus) { 
      anime.start(); 
      timer.start(); 
     } else { 
      anime.stop(); 
     } 
    } 

    @Override 
    public boolean onCreateOptionsMenu(Menu menu) { 
     // Inflate the menu; this adds items to the action bar if it is present. 
     getMenuInflater().inflate(R.menu.logo, menu); 
     return true; 
    } 

    @Override 
    protected void onStop() { 
     // TODO Auto-generated method stub 
     super.onStop(); 
     finish(); // to remove this activity. 
    } 
} 

這裏是anime.xml:

<?xml version="1.0" encoding="utf-8"?> 
<animation-list xmlns:android="http://schemas.android.com/apk/res/android 
android:oneshot="true"> 
<item android:drawable="@drawable/a1" android:duration="60" /> 
<item android:drawable="@drawable/a2" android:duration="60" /> 
<item android:drawable="@drawable/a3" android:duration="60" /> 
<item android:drawable="@drawable/a4" android:duration="60" /> 
<item android:drawable="@drawable/a5" android:duration="60" /> 
<item android:drawable="@drawable/a6" android:duration="60" /> 
<item android:drawable="@drawable/a7" android:duration="60" /> 
<item android:drawable="@drawable/a8" android:duration="60" /> 
<item android:drawable="@drawable/a9" android:duration="60" /> 
<item android:drawable="@drawable/a10" android:duration="60" /> 
<item android:drawable="@drawable/a11" android:duration="60" /> 
<item android:drawable="@drawable/a12" android:duration="60" /> 
<item android:drawable="@drawable/a13" android:duration="60" /> 
<item android:drawable="@drawable/a14" android:duration="60" /> 
<item android:drawable="@drawable/a15" android:duration="60" /> 
<item android:drawable="@drawable/a16" android:duration="60" /> 
<item android:drawable="@drawable/a17" android:duration="60" /> 
<item android:drawable="@drawable/a18" android:duration="60" /> 
<item android:drawable="@drawable/a19" android:duration="60" /> 
<item android:drawable="@drawable/a20" android:duration="60" /> 
<item android:drawable="@drawable/a21" android:duration="60" /> 
<item android:drawable="@drawable/a22" android:duration="60" /> 
<item android:drawable="@drawable/a23" android:duration="60" /> 
<item android:drawable="@drawable/a24" android:duration="60" /> 
<item android:drawable="@drawable/a25" android:duration="60" /> 
<item android:drawable="@drawable/a26" android:duration="60" /> 
<item android:drawable="@drawable/a27" android:duration="60" /> 

</animation-list> 

現在我看到了很多答案,說明bitmapFactory.options 但我怎麼能應用在XML這些選項文件,如果它不可能。有沒有其他方法可以使用,而不是調整圖片的大小。 預先感謝您。

這裏我試圖programmaticaly應用的代碼,仍然得到了同樣的錯誤:

//onCreate and thread then 
public void onWindowFocusChanged(boolean hasFocus) { 
    // TODO Auto-generated method stub 
    super.onWindowFocusChanged(hasFocus); 

    BitmapDrawable frame1 = (BitmapDrawable)getResources().getDrawable(R.drawable.a1); 
    BitmapDrawable frame2 = (BitmapDrawable)getResources().getDrawable(R.drawable.a2); 
    BitmapDrawable frame3 = (BitmapDrawable)getResources().getDrawable(R.drawable.a3); 
    BitmapDrawable frame4 = (BitmapDrawable)getResources().getDrawable(R.drawable.a4); 
    BitmapDrawable frame5 = (BitmapDrawable)getResources().getDrawable(R.drawable.a5); 
    BitmapDrawable frame6 = (BitmapDrawable)getResources().getDrawable(R.drawable.a6); 
    BitmapDrawable frame7 = (BitmapDrawable)getResources().getDrawable(R.drawable.a7); 
    BitmapDrawable frame8 = (BitmapDrawable)getResources().getDrawable(R.drawable.a8); 
    BitmapDrawable frame9 = (BitmapDrawable)getResources().getDrawable(R.drawable.a9); 
    BitmapDrawable frame10 = (BitmapDrawable)getResources().getDrawable(R.drawable.a10); 
    BitmapDrawable frame11 = (BitmapDrawable)getResources().getDrawable(R.drawable.a11); 
    BitmapDrawable frame12 = (BitmapDrawable)getResources().getDrawable(R.drawable.a12); 
    BitmapDrawable frame13 = (BitmapDrawable)getResources().getDrawable(R.drawable.a13); 
    BitmapDrawable frame14 = (BitmapDrawable)getResources().getDrawable(R.drawable.a14); 
    BitmapDrawable frame15 = (BitmapDrawable)getResources().getDrawable(R.drawable.a15); 
    BitmapDrawable frame16 = (BitmapDrawable)getResources().getDrawable(R.drawable.a16); 
    BitmapDrawable frame17 = (BitmapDrawable)getResources().getDrawable(R.drawable.a17); 
    BitmapDrawable frame18 = (BitmapDrawable)getResources().getDrawable(R.drawable.a18); 
    BitmapDrawable frame19 = (BitmapDrawable)getResources().getDrawable(R.drawable.a19); 
    BitmapDrawable frame20 = (BitmapDrawable)getResources().getDrawable(R.drawable.a20); 
    BitmapDrawable frame21 = (BitmapDrawable)getResources().getDrawable(R.drawable.a21); 
    BitmapDrawable frame22 = (BitmapDrawable)getResources().getDrawable(R.drawable.a22); 
    BitmapDrawable frame23 = (BitmapDrawable)getResources().getDrawable(R.drawable.a23); 
    BitmapDrawable frame24 = (BitmapDrawable)getResources().getDrawable(R.drawable.a24); 
    BitmapDrawable frame25 = (BitmapDrawable)getResources().getDrawable(R.drawable.a25); 
    BitmapDrawable frame26 = (BitmapDrawable)getResources().getDrawable(R.drawable.a26); 
    BitmapDrawable frame27 = (BitmapDrawable)getResources().getDrawable(R.drawable.a27); 

    final AnimationDrawable anime = new AnimationDrawable(); 
    anime.setOneShot(true); 
    anime.addFrame(frame1, 60); 
    anime.addFrame(frame2, 60); 
    anime.addFrame(frame3, 60); 
    anime.addFrame(frame4, 60); 
    anime.addFrame(frame5, 60); 
    anime.addFrame(frame6, 60); 
    anime.addFrame(frame7, 60); 
    anime.addFrame(frame8, 60); 
    anime.addFrame(frame9, 60); 
    anime.addFrame(frame10, 60); 
    anime.addFrame(frame11, 60); 
    anime.addFrame(frame12, 60); 
    anime.addFrame(frame13, 60); 
    anime.addFrame(frame14, 60); 
    anime.addFrame(frame15, 60); 
    anime.addFrame(frame16, 60); 
    anime.addFrame(frame17, 60); 
    anime.addFrame(frame18, 60); 
    anime.addFrame(frame19, 60); 
    anime.addFrame(frame20, 60); 
    anime.addFrame(frame21, 60); 
    anime.addFrame(frame22, 60); 
    anime.addFrame(frame23, 60); 
    anime.addFrame(frame24, 60); 
    anime.addFrame(frame25, 60); 
    anime.addFrame(frame26, 60); 
    anime.addFrame(frame27, 60); 


    iv.setBackgroundDrawable(anime); 
    if (hasFocus) { 
     anime.start(); 
     timer.start(); 
    } 

    else 
     anime.stop(); 
} 
+0

您是否試圖以編程方式創建動畫? – sandrstar

+0

不,我不認爲這是可能的,即時通訊將閱讀有關它,所以你說如果我以編程方式實現它,我可以在圖片上添加bitmapfactory的選項? – Coderji

+1

以及事情是你永遠不知道哪個設備可以支持這個以及哪個設備cannot ..檢查DDMS視圖中的堆內存你有設備..還有sandrstar建議你可以動畫編程..它會解決問題經常 – user632905

回答

2

銀河S3實際上具有相對小的默認堆大小(32MB)爲它的規格。

您可以臨時解決此問題,請求更大的堆並檢查此問題是否解決了您的問題。

<application android:largeHeap="true"> 
     . . . 
</application> 

然後我會研究幀資源的大小和類型。

乾杯。

+0

即使添加它,我仍然遇到了錯誤,這是否意味着我使用的內存太多了?我想我只是調整照片。 – Coderji

+0

你能給我更詳細的資源分辨率大小和格式嗎? 也許你試圖完成什麼類型​​的動畫(整個屏幕,只是它的一部分)。 –

+0

我有27張照片,每張照片都有1000 * 600的尺寸,水平和垂直均爲300dpi。每個圖片的大小是55.7 KB。 我使用圖片來顯示遊戲的啓動畫面,雖然在前面提到的Nexus 4和其他設備上工作的格式相同 – Coderji