我是新來的Android和拍圖片應用程序,但我在那裏我跑出來的內存問題。暗戰與照片內存 - 更新
主要活性是啓動其具有視圖尋呼機相片滑塊新的活動,表現出5ish照片按鈕的網格。目前它正在運行,但是在返回到主屏幕並選擇一個新按鈕(該按鈕開始一個新的照片活動)之後,它變得比崩潰慢。
看完監視器後,每次活動開始時都分配內存,但按下後退按鈕時內存不會被釋放。
我知道人們建議使用滑行庫,但我爲每個活動運行5個小尺寸照片,並且如果可以在不再需要活動時釋放內存,那麼我當前的設置應該可以工作。
我知道我這樣做的方式並不是最好的方法,但我只是想盡快讓它工作,然後我可以回去優化它。
哪裏是內存泄漏或我的錯誤?
建議?
在此先感謝。
public class MainActivity extends AppCompatActivity {
static int picSet = 1; //the set of pictures i want
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
public void startActiv()
{
startActivity(new Intent(MainActivity.this, PagerSetUp.class));
}
//Button/Image
public void firstMet(View view) {
picSet = 1;
startActiv();
}
//Button/Image
public void wedding (View view) {
picSet = 2;
startActiv();
}
//Button/Image
public void vacation(View view) {
picSet = 3;
startActiv();
}
//Button/Image
public void pups (View view) {
picSet = 4;
startActiv();
}
//Button/Image
public void ourHomes(View view) {
picSet = 5;
startActiv();
}
//Button/Image
public void age (View view) {
picSet = 6;
startActiv();
}
//Button/Image
public void family(View view) {
picSet = 7;
startActiv();
}
//Button/Image
public void friends (View view) {
picSet = 8;
startActiv();
}
}
public class PagerSetUp extends AppCompatActivity {
//Create an instance of the pager adapter class
CustomPagerAdapter mCustomPagerAdapter; //Creates content for each page
//Create an instance of the view pager class
ViewPager mViewPager;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.collections_main); //XML File
//Setting up the ViewPager
mCustomPagerAdapter = new CustomPagerAdapter(this);
mViewPager = (ViewPager) findViewById(R.id.pager);
mViewPager.setAdapter(mCustomPagerAdapter);
}
}
public class CustomPagerAdapter extends PagerAdapter {
public int[] firstMetPics = {
R.drawable.fm1,
R.drawable.fm2,
R.drawable.fm3,
R.drawable.fm4,
R.drawable.fm5
};
public int[] weddingPics = {
R.drawable.w1,
R.drawable.w2,
R.drawable.w3,
R.drawable.w4,
R.drawable.w5
};
public int[] vacationPics = {
R.drawable.fm1,
R.drawable.fm2,
R.drawable.fm3,
R.drawable.fm4,
R.drawable.fm5
};
public int[] pupsPics = {
R.drawable.fm1,
R.drawable.fm2,
R.drawable.fm3,
R.drawable.fm4,
R.drawable.fm5
};
public int[] homesPics = {
R.drawable.fm1,
R.drawable.fm2,
R.drawable.fm3,
R.drawable.fm4,
R.drawable.fm5
};
public int[] agePics = {
R.drawable.fm1,
R.drawable.fm2,
R.drawable.fm3,
R.drawable.fm4,
R.drawable.fm5
};
public int[] familyPics = {
R.drawable.fm1,
R.drawable.fm2,
R.drawable.fm3,
R.drawable.fm4,
R.drawable.fm5
};
public int[] friendsPics = {
R.drawable.fm1,
R.drawable.fm2,
R.drawable.fm3,
R.drawable.fm4,
R.drawable.fm5
};
protected Context mContext;
LayoutInflater mLayoutInflater;
public CustomPagerAdapter(Context context) {
mContext = context;
mLayoutInflater = (LayoutInflater) mContext.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
}
//Specifies how many views to show in the ViewPager
@Override
public int getCount() {
int picArrayLength = 0;
switch (MainActivity.picSet) {
case 1: picArrayLength = firstMetPics.length;
break;
case 2: picArrayLength = weddingPics.length;
break;
case 3: picArrayLength = vacationPics.length;
break;
case 4: picArrayLength = pupsPics.length;
break;
case 5: picArrayLength = homesPics.length;
break;
case 6: picArrayLength = agePics.length;
break;
case 7: picArrayLength = familyPics.length;
break;
case 8: picArrayLength = friendsPics.length;
break;
default: picArrayLength = firstMetPics.length;
break;
}
return picArrayLength;
}
@Override
public boolean isViewFromObject(View view, Object object) {
return view ==(object);
}
@Override
public Object instantiateItem(ViewGroup container, int position) {
View itemView = mLayoutInflater.inflate(R.layout.pager_item, container, false);
ImageView imageView = (ImageView) itemView.findViewById(R.id.imageView);
switch (MainActivity.picSet) {
case 1: imageView.setImageResource(firstMetPics[position]);
break;
case 2: imageView.setImageResource(weddingPics[position]);
break;
case 3: imageView.setImageResource(vacationPics[position]);
break;
case 4: imageView.setImageResource(pupsPics[position]);
break;
case 5: imageView.setImageResource(homesPics[position]);
break;
case 6: imageView.setImageResource(agePics[position]);
break;
case 7: imageView.setImageResource(familyPics[position]);
break;
case 8: imageView.setImageResource(friendsPics[position]);
break;
default: imageView.setImageResource(firstMetPics[position]);
break;
}
container.addView(itemView);
return itemView;
}
@Override
public void destroyItem(ViewGroup container, int position, Object object) {
container.removeView((LinearLayout) object);
Log.i("NOTE", "destroyed view");
}
首先你不應該使用這樣的靜態對象。靜態對象是內存泄漏的主要來源。只有在真正需要時才應該採用靜態對象。 –
將FragmentStatePagerAdapter與ViewPager一起使用,以避免內存不足問題,並使用Glide加載圖像 – faruk
更適合免費的強大的圖書館,如Glide。教程http://www.androidhive.info/2016/04/android-glide-image-library-building-image-gallery-app/ – Stallion