我正在尝试从可绘制资源加载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>
欢迎提供所有提示和评论。
答案 0 :(得分: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);
}