|
需求:
在ViewFlipper中读入十几张图片,并可以滑动切换。
问题:
1, 读入的过程花时间长的话,大家有没有好点的处理办法呢?
2, 读入完成以后,要及时释放内存,怎么处理呢?
大概代码如下:
public voidonCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main3);
Relative = (RelativeLayout) findViewById(R.id.lin_Id);
params = new LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.FILL_PARENT);
flipper = new ViewFlipper(this);
this.mainProcessing();//调用线程
} privateHandler handler= newHandler(); privatevoidmainProcessing(){ Thread thread = new Thread(null, runnable, "Background"); thread.start(); } privateRunnable runnable= newRunnable() {
@Override
public void run() {
//动态加载ViewFlipper
for(i = 0; i<imageArray.length;i++){
flipper.addView(addImageById(imageArray),params);
}
handler.post(doUpdateGUI);
}
};
public View addImageById(int id){
iv = newImageView(ThirdActivity.this);
iv.setImageResource(id);
return iv;
}
//更新GUI,动态加载ViewFlipper
private Runnable doUpdateGUI = new Runnable() {
@Override
public void run() {
Relative.removeAllViews();
Relative.addView(flipper, params);
Toast.makeText(getApplicationContext(),"左右滑动切换图片", Toast.LENGTH_LONG).show();
}
};
//按下返回键时,及时的释放内存
@Override
public void onBackPressed() {
iv = null;
flipper = null;
Relative = null;
handler.removeCallbacks(doUpdateGUI);
java.lang.System.gc();
super.onBackPressed();
}
我在网上搜了一下,自己的处理方法是这样的:1, 开一个线程,线程里面读入图片,GUI动态加载ViewFlipper
2, 即使释放内存的那几条语句是在网上搜的,不知道合适不?
现在的问题是:重复打开几次,或者重复打开这个Activity的频率高点,就会出现OutOfMemory的错误,大家看怎么解决啊? |
|