gpt4 book ai didi

javascript - Android Studio 内存不足错误 ViewFlipper

转载 作者:搜寻专家 更新时间:2023-11-01 08:34:52 25 4
gpt4 key购买 nike

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

}

关于javascript - Android Studio 内存不足错误 ViewFlipper,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37627542/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com