在android中显示许多图像
我需要在imageview中显示大约125幅图像,就像gif动画一样,只需3-5秒。在android中显示许多图像,android,image,imageview,Android,Image,Imageview,我需要在imageview中显示大约125幅图像,就像gif动画一样,只需3-5秒。 我尝试了一个timertask,但看起来很慢,我该怎么办?考虑到这一点,该资源应该非常有用 您也可以尝试:我坚持认为您应该尝试使用AnimationDrawable。将所有图像保存在drawable文件夹中,您可以在特定的持续时间后逐个设置动画 假设您在drawable文件夹中有图像,如abc1、abc2、abc3等,然后只拍摄一个ImageView并使用动画迭代该ImageView中的所有图像 这样试试看,
我尝试了一个timertask,但看起来很慢,我该怎么办?考虑到这一点,该资源应该非常有用
您也可以尝试:我坚持认为您应该尝试使用
AnimationDrawable
。将所有图像保存在drawable文件夹中,您可以在特定的持续时间后逐个设置动画
假设您在drawable文件夹中有图像,如abc1、abc2、abc3等,然后只拍摄一个ImageView并使用动画迭代该ImageView中的所有图像
这样试试看,这里我只考虑5张图片,如果你有125张,那么循环应该移动125次
我的xml-main.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:gravity="center_vertical|center_horizontal"
>
<ImageView android:id="@+id/imgView"
android:layout_width="fill_parent"
android:layout_height="fill_parent" />
</LinearLayout>
这是一个很好的解决方案,但我尝试过了,应用程序崩溃了,分配的空间太多了:12-14 11:19:15.762:E/AndroidRuntime(14259):java.lang.OutOfMemoryError:循环中每20到30个图像,位图大小就会超过VM budgettry System.gc(),问题是,如果要将帧添加到AnimationDrawable,则无法调用System.gc();我可以在TimerTask上调用它,但是脚本每秒应该显示2-3个图像
public class mainAct extends Activity {
AnimationDrawable anim = new AnimationDrawable();
Handler handler = new Handler();
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
ImageView imageView = (ImageView) findViewById(R.id.imgView);
for (int i = 1; i <= 5; i++) {
anim.addFrame(getResources().getDrawable(getResources().getIdentifier("abc"+i, "drawable", getPackageName())), 1000);
}
imageView.setBackgroundDrawable(anim);
imageView.post(new Runnable() {
@Override
public void run() {
anim.start();
handler.postDelayed(new Runnable() {
@Override
public void run() {
mainAct.this.finish();
}
},getTotalAnimationDuration());
}
});
}
private int getTotalAnimationDuration() {
int mDuration = 0;
for (int i = 0; i < anim.getNumberOfFrames(); i++) {
mDuration = mDuration + anim.getDuration(i);
}
return mDuration;
}
@Override
protected void onDestroy() {
super.onDestroy();
anim.stop();
}
}