0
我想創建一個ImageSwitcher來瀏覽我的DCIM上的相機圖像。當我翻閱圖像時,出現OutOfMemory錯誤。如果將內存分配給每個相當大的圖像,這是非常有意義的。我研究過其他類似的問題,但我無法弄清楚如何在切換到新圖像時釋放舊圖像的內存。Android ImageSwitcher OutOfMemoryError
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--;
}
}
}
看看這個庫:https://github.com/nostra13/Android-Universal-Image-Loader – Kakarot