我想從可繪製資源中加載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>
歡迎所有的提示和意見。
也許這可能是一些使用 http://stackoverflow.com/questions/5880559/how-to-make-smooth-frame-animation-in-android – Jacob 2013-02-21 01:17:09