gpt4 book ai didi

java - Android ImageSwitcher 内存不足错误

转载 作者:行者123 更新时间:2023-12-01 13:00:45 25 4
gpt4 key购买 nike

我正在尝试创建一个 ImageSwitcher 来浏览 DCIM 上的相机图像。当我翻阅图像时,出现 OutOfMemory 错误。如果为每个查看的相当大的图像分配内存,那么这是非常有意义的。我研究过其他类似的问题,但我不知道当我切换到新图像时如何释放旧图像的内存。

public class MainActivity extends ActionBarActivity {

private ImageView imageView;
private ImageSwitcher imageSwitcher;
private String imageDir;
private File imageFile;
private String[] imageNames;
private int i;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);

imageSwitcher = (ImageSwitcher)findViewById(R.id.imageSwitcher1);
imageDir = getString(R.string.image_dir);
imageFile = new File(imageDir);
imageNames = imageFile.list();
i = imageNames.length - 1;

imageSwitcher.setFactory(new ViewFactory() {
@TargetApi(Build.VERSION_CODES.HONEYCOMB)
@Override
public View makeView() {
// I tried testing for null here but that doesn't work
imageView = new ImageView(getApplicationContext());
imageView.setScaleType(ImageView.ScaleType.FIT_CENTER);
imageView.setLayoutParams(new
ImageSwitcher.LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT));
return imageView;
}
});
}

public void previous(View view) {
Animation in = AnimationUtils.loadAnimation(this, android.R.anim.slide_out_right);
Animation out = AnimationUtils.loadAnimation(this, android.R.anim.slide_in_left);
imageSwitcher.setInAnimation(out);
imageSwitcher.setOutAnimation(in);
Uri imageUri = Uri.parse(imageDir+"/"+imageNames[i]);
imageSwitcher.setImageURI(imageUri);
decrementImage();
}

public void decrementImage() {
if (i == 0) {
i = imageNames.length - 1;
} else {
i--;
}
}
}

最佳答案

解决方案:

public void previous(View view) {
Animation in = AnimationUtils.loadAnimation(this, android.R.anim.slide_out_right);
Animation out = AnimationUtils.loadAnimation(this, android.R.anim.slide_in_left);
imageSwitcher.setInAnimation(out);
imageSwitcher.setOutAnimation(in);
Uri imageUri = Uri.parse(imageDir+"/"+imageNames[i]);
imageSwitcher.setImageURI(null); // THIS FIXED THE OOM ERROR
imageSwitcher.setImageURI(imageUri);
decrementImage();
}

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

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