2013-02-20 74 views
0

我想從可繪製資源中加載11個圖像,並將它們延遲顯示在ImageView中。實際上做一些像AnimationDrawable,因爲在OutOfMemory異常和崩潰中創建這樣的結果。這既是重新發明輪子,也是一些練習,但仍會導致應用程序崩潰。如果我只是初始化imgView並執行imgView.setImageResource(R.drawable.pic1);圖像加載。當我設置一個Handler並嘗試像下面這樣做postDelayed()時,我只能在應用程序崩潰之前看到佈局的背景圖像,而Logcat中沒有任何內容。下面是代碼:如何從可繪製資源創建ImageView動畫序列?

package com.forms.test; 

import android.app.Activity; 
import android.os.Bundle; 
import android.os.Handler; 
import android.view.animation.AnimationUtils; 
import android.widget.ImageView; 

public class Animacia extends Activity { 

final static int[] imgGalleryResources = { 
    R.drawable.pic1, R.drawable.pic2, R.drawable.pic3, 
    R.drawable.pic4, R.drawable.pic5, R.drawable.pic6, 
    R.drawable.pic7, R.drawable.pic8, R.drawable.pic9, 
    R.drawable.pic10, R.drawable.pic11 
}; 

ImageView imgView; 
Handler handlerTimer = new Handler(); 

public void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.animation); 

    initGUI(); 
} 

private void initGUI() { 
    int i; 
    imgView = (ImageView)findViewById(R.id.imgView); 
    imgView.setAnimation(AnimationUtils.loadAnimation(this, R.anim.alpha)); 

    for(i = 0; i < imgGalleryResources.length ;i++) { 
     final int res = imgGalleryResources[i]; 
     handlerTimer.postDelayed(
       new Runnable() { 
        public void run() { 
         imgView.setImageResource(res); 
        } 
       }, 
       5000 
     ); 
    } 
} 
} 

與此相關的代碼是從佈局/ animation.xml的imgView:

<?xml version="1.0" encoding="utf-8"?> 
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:orientation="vertical" 
    android:layout_width="fill_parent" 
    android:layout_height="fill_parent" 
    android:background="@drawable/heart" 
    android:id="@+id/frameLayout1"> 

    <ImageView 
     android:id="@+id/imgView" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" /> 

</FrameLayout> 

而從動畫/ alpha.xml的imgView的α動畫:

<?xml version="1.0" encoding="utf-8"?> 
<alpha> 
    <alpha 
    xmlns:android="http://schemas.android.com/apk/res/android" 
    android:fromAlpha="0.3" 
android:toAlpha="0.9" 
android:duration="2000" /> 
</alpha> 

歡迎所有的提示和意見。

+0

也許這可能是一些使用 http://stackoverflow.com/questions/5880559/how-to-make-smooth-frame-animation-in-android – Jacob 2013-02-21 01:17:09

回答

1

你試過這種方法嗎?

private int pos = 0; 
private Timer timer; 
private static final int UPDATE_PICTURE = 5; 
private void initGUI() { 
    imgView = (ImageView)findViewById(R.id.imgView); 
    imgView.setAnimation(AnimationUtils.loadAnimation(this, R.anim.alpha)); 

    handlerTimer = new Handler() { 
     public void handleMessage(Message msg) { 
      if(msg.what == UPDATE_PICTURE) 
      { 
       //start animation here 
       imgView.setImageResource(msg.arg1); 
       imgView.invalidate(); 
      } 
     } 
    }; 

    timer = new Timer(); 
    timer.scheduleAtFixedRate(new TimerTask() { 

     public void run() { 
      Message msg = new Message(); 
      msg.what = UPDATE_PICTURE; 
      msg.arg1 = imgGalleryResources[pos++]; 
      handlerTimer.sendMessage(msg); 
      if(pos>=imgGalleryResources.length) 
       timer.cancel(); 
     } 
    }, 0, 2000); 

} 
+0

好可惜這也導致的在崩潰中......爲了清楚起見:我只是試圖從幾張可繪製的圖像中創建一個圖像幻燈片,並在它們之間延遲一段時間,然後使用一個alpha動畫淡化下一張圖像。就是這樣。一般來說,它會很好的與setRepeatMode(Animation.RESTART)具有相同的效果;和setRepeatCount(Animation.INFINITE);爲了從圖像1重新開始,一旦圖像11被顯示,但是如果我讓它運行一次,這應該不是一個挑戰:) – freemind 2013-02-21 00:43:38

+0

這段代碼和我一起工作。你會得到類型爲「內存不足」的錯誤,我認爲這是一個圖像大小的問題。你的照片的分辨率是多少?你有沒有嘗試過與其他圖片(較小的分辨率)? – 2013-02-21 08:51:07

+0

加載的唯一圖像(R.drawable.pic1)的尺寸爲W:680px H:453px。所有其他人都高於此。我認爲這可能是問題,但一方面,setImageResource()方法本身會對圖像進行一些重新縮放。此外,我環顧四周,實現了兩種方法(不在這裏發佈)嘗試手動重新調整圖像並使用setImageBitmap()將它們設置爲ImageView,但它可能是錯誤的,因爲在每種情況下,我都會遇到「內存不足」錯誤。 LANDSCAPE/PORTRAIT的最大可能尺寸是什麼?其他工具如何輸出更大的圖像? – freemind 2013-02-21 18:02:44