0

我正在尝试从可绘制资源中加载 11 个图像并延迟显示在 ImageView 中。实际上做类似 AnimationDrawable 的事情,因为创建这样的结果会导致 OutOfMemory 异常和崩溃。它既是重新发明轮子,也是一些实践,但仍然会导致应用程序崩溃。如果我只是初始化 imgView 并imgView.setImageResource(R.drawable.pic1);加载图像。当我设置一个处理程序并尝试像下面那样执行 postDelayed() 时,我只能在应用程序崩溃之前看到布局的背景图像,而 Logcat 中什么也没有。这是代码:

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
        );
    }
}
}

与此代码相关的是 layout/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>

以及来自 anim/alpha.xml 的 imgView 的 alpha 动画:

<?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>

欢迎所有提示和评论。

4

1 回答 1

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);

}
于 2013-02-20T23:36:22.417 回答