我向用户展示了一系列问题。在每个问题中,我将drawables设置为6个图像视图。唯一的问题是,当我进入下一个屏幕时,ram的使用量会增加大约60MB。
我不明白为什么会这样,因为我只是用不同的图片替换图片?大约5个屏幕后,由于内存分配失败,应用程序崩溃。我怎么会在它之前释放屏幕的内存,所以它不会累积。
提前致谢。
public class Test extends ActionBarActivity {
ImageButton a1;
ImageButton a2;
ImageButton a3;
ImageButton a4;
Intent result;
String correctOrWrong = "";
int question;
int amountOfC = 0;
int numCorrect;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_test);
getSupportActionBar().hide();
ImageView qMain = (ImageView) findViewById(R.id.qMain);
a1 = (ImageButton) findViewById(R.id.a1);
a2 = (ImageButton) findViewById(R.id.a2);
a3 = (ImageButton) findViewById(R.id.a3);
a4 = (ImageButton) findViewById(R.id.a4);
ImageView qTitle = (ImageView) findViewById(R.id.qTitle);
a1.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
nextQ(1);
}
});
a2.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
nextQ(2);
}
});
a3.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
nextQ(3);
}
});
a4.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
nextQ(4);
}
});
Intent qGet = getIntent();
question = qGet.getIntExtra("QNUMBER", 1);
numCorrect = qGet.getIntExtra("NUMCORRECT", 1);
Log.d("lel", String.valueOf(question));
if (question == 1) {
qMain.setBackgroundResource(R.drawable.q1);
a1.setBackgroundResource(R.drawable.a13);
a2.setBackgroundResource(R.drawable.a12);
a3.setBackgroundResource(R.drawable.a11);
a4.setBackgroundResource(R.drawable.ca1);
qTitle.setBackgroundResource(R.drawable.q1t);
} else if (question == 2) {
qMain.setBackgroundResource(R.drawable.q2);
a1.setBackgroundResource(R.drawable.q23);
a2.setBackgroundResource(R.drawable.q2a);
a3.setBackgroundResource(R.drawable.q22);
a4.setBackgroundResource(R.drawable.q21);
qTitle.setBackgroundResource(R.drawable.q2t);
} else if (question == 3) {
qMain.setBackgroundResource(R.drawable.q3);
a1.setBackgroundResource(R.drawable.q31);
a2.setBackgroundResource(R.drawable.q32);
a3.setBackgroundResource(R.drawable.q33);
a4.setBackgroundResource(R.drawable.q3a);
qTitle.setBackgroundResource(R.drawable.q3t);
} else if (question == 4) {
qMain.setBackgroundResource(R.drawable.q4);
a1.setBackgroundResource(R.drawable.q4a);
a2.setBackgroundResource(R.drawable.q43);
a3.setBackgroundResource(R.drawable.q42);
a4.setBackgroundResource(R.drawable.q41);
qTitle.setBackgroundResource(R.drawable.q4t);
} else if (question == 5) {
qMain.setBackgroundResource(R.drawable.q5);
a1.setBackgroundResource(R.drawable.q53);
a2.setBackgroundResource(R.drawable.q5a);
a3.setBackgroundResource(R.drawable.q52);
a4.setBackgroundResource(R.drawable.q51);
qTitle.setBackgroundResource(R.drawable.q5t);
答案 0 :(得分:0)
不要自己处理图像内存/缓存,而是使用滑动等库 - https://github.com/bumptech/glide
或
尝试清除活动的imageView onDestroy()
a1.setImageDrawable(null);
a2.setImageDrawable(null);
a3.setImageDrawable(null);
a4.setImageDrawable(null);
答案 1 :(得分:0)
你需要在使用它们之后为每个位图调用recycle(),如下所示:
a1.setImageDrawable(null);
a2.setImageDrawable(null);
a3.setImageDrawable(null);
a4.setImageDrawable(null);
a1.recycle();
a2.recycle();
a3.recycle();
a4.recycle();