Android Studio 内存不足错误 ViewFlipper

Android Studio Out Of Memory Error ViewFlipper

首先我想说请不要将此标记为重复,因为这个问题与大多数其他问题不同。我正在尝试创建各种人工屏幕保护程序。我试图通过创建一个带有 ViewFlipper 的 activity 来实现这一点,在其中放置一些图片,然后设置 flipInterval 以使事情顺利进行。当我尝试 运行 这个 activity 时,我得到一个 OutOfMemory 错误。大多数建议说减小图片的大小,但这是不可能的,因为我已经包含的图片是最小尺寸。它们的总文件大小也从 20kb-70kb 不等。我现在总共有六张图片,但是我为其他活动加载到我的应用程序中的图片越多,我似乎能够在我的屏幕保护程序中播放的图片就越少。有什么方法可以增加图片数量吗?也许在其他活动中调用“.finish()”?我显然不确定。任何解决方案?

谢谢大家!

希望这可能有用

public class AnimateBackgroundActivity extends Activity{

protected int[] backgroundImages;
protected ImageView mainBackground;
protected boolean isRunning;

protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    this.requestWindowFeature(Window.FEATURE_NO_TITLE);
    isRunning = true;
    backgroundImages = new int[6];
    backgroundImages[0] = R.drawable.background0;
    backgroundImages[1] = R.drawable.background1;
    backgroundImages[2] = R.drawable.background2;
    backgroundImages[3] = R.drawable.background3;
    backgroundImages[4] = R.drawable.background4;
    backgroundImages[5] = R.drawable.background5;
}

protected void animateBackground()
{
    Runnable animate = new Runnable() {

        public void run() {
            for (int i = 0; isRunning; i = (++i % backgroundImages.length)) {
                final int j = i;
                runOnUiThread(new Runnable() {
                    public void run() {
                        mainBackground.
                        setImageResource(
                                backgroundImages[j]);
                    }
                });

                try {
                    Thread.sleep(5000);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }finally {

                }
            }
        }
    };

    Thread animBackgroundThread = new Thread(animate);
    animBackgroundThread.start();
}

@Override
protected void onResume() {
    isRunning = true;
    animateBackground();
    super.onResume();
}

@Override
protected void onDestroy() {
    isRunning = false;
    super.onDestroy();
}

@Override
protected void onPause() {
    isRunning = false;
    super.onPause();
}
}


public class InitScreen extends AnimateBackgroundActivity {


private void initImageLoader() {
    Context context = getApplicationContext();

    DisplayImageOptions options = new DisplayImageOptions.Builder().cacheInMemory().cacheOnDisc().build();

    ImageLoaderConfiguration config = new ImageLoaderConfiguration.Builder(context).memoryCacheExtraOptions(480, 800)
            // default
            // =
            // device
            // screen
            // dimensions
            .discCacheExtraOptions(480, 800, CompressFormat.JPEG, 100).memoryCache(new LruMemoryCache(4 * 1024 * 1024)).memoryCacheSize(4 * 1024 * 1024).discCacheSize(50 * 1024 * 1024).discCacheFileCount(1000).defaultDisplayImageOptions(options).build();

    ImageLoader.getInstance().init(config);

}

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);


    initImageLoader();

    setContentView(R.layout.inital);
    mainBackground = (ImageView) findViewById(R.id.init_screen_background);
    animateBackground();

}