在 Activity-A 中,我使用 Surfaceview 和 Bitmap 作为背景。当我去 onPause 时,我释放它并将其设置为 null 并进行显式 GC。它工作正常。但是当我回到同一个Activity时——分配了将近3MB的巨大堆来解码位图。这是因为我在 GC 之后解码位图。
我很擅长回收位图和 GC 过程。但我担心堆分配正在增加以处理相同的位图。
当我移动下一个活动时,我需要将其保存在不应在堆中保留任何空间的地方,当我回来时,我应该去挑选图像。知道如何实现这一目标吗?
以下是现有代码
@Override
protected void onPause() {
// TODO Auto-generated method stub
super.onPause();
ourSurfaceView.pause();
Log.i("DragDrop", "In pause drag drop");
backGround.release();
optionSelected.release();
backgoundImage.recycle();
backgoundImage=null;
backGround=optionSelected=null;
if (tts != null) {
Log.i("DragDrop", "In pause drag drop stop");
tts.stop();
tts.shutdown();
}
System.gc();
}
public void run() {
// TODO Auto-generated method stub
// Log.i("Notice", "In run of mybringback");
if(backgoundImage == null){
try {
Log.i("MyBringBack", "In run of mybringback");
backgoundImage = Bitmap.createScaledBitmap(getAssetImage(getApplicationContext(),"backgroundhomepage"), (int) dWidth, (int) dHeight, true);
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
ourHolder = getHolder();
while (isRunning) {
// Log.i("DragDrop", "ourHolder.getSurface().isValid()" + ourHolder.getSurface().isValid() );
if (!ourHolder.getSurface().isValid()){
continue;
}
canvas = ourHolder.lockCanvas();
screenCenterX = dWidth / 2;
screenCenterY = dHeight / 2;
canvas.drawBitmap(backgoundImage, 0, 0, null);
if (imagePublishDone) {
if(!welcomeDone){
message = "Drop your wish to panda";
tts.speak(message, TextToSpeech.QUEUE_FLUSH, null);
welcomeDone=true;
}
moveImageInEllipticalPath();
} else {
initialImagePublish();
}
centreReached = false;
ourHolder.unlockCanvasAndPost(canvas);
}
}
public Bitmap getAssetImage(Context context, String filename) throws IOException {
AssetManager assets = getApplicationContext().getResources().getAssets();
InputStream buffer = null;
try {
buffer = new BufferedInputStream((assets.open("drawable/" + filename + ".png")));
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
BitmapFactory.Options options = new BitmapFactory.Options();
options.inTempStorage = new byte[16*1024];
options.inPurgeable = true;
Bitmap temp = BitmapFactory.decodeStream(buffer, null, options);
Bitmap finalImage = Bitmap.createScaledBitmap(temp, (int) dWidth, (int) dHeight, true);
temp.recycle();
temp=null;
return finalImage;
}