内存耗尽照片 - 更新

时间:2017-04-25 04:18:54

标签: android

我是Android新手并制作图片应用程序,但我遇到的问题是我的内存不足。

主要活动是一个按钮网格,用于启动新活动,这些活动具有查看寻呼机照片滑块,显示5张照片。它目前正在运行,但在返回主屏幕并选择一个新按钮后,它会启动一个新的照片活动,它会比崩溃变慢。

观察监视器后,每次活动开始时都会分配内存,但按下后退按钮时内存不会被释放。

我知道有人建议使用滑动库,但我为每个活动运行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");
}

1 个答案:

答案 0 :(得分:0)

OutOfMemory问题是由于几个问题引起的。对于你的情况,我猜它是因为图像的大小和图像的数量。您可以通过执行以下操作来解决此问题。

  • 如果这些drawables是图标?使用矢量drawable而不是普通的png。
  • 在添加到可绘制文件夹之前压缩图像。
  • 将您的png或jpg图片转换为webP格式。由于Android现在支持webP WebP。它会大大减少你的图像尺寸。
  • 最重要的是,不要使用普通的setImageDrawable或任何方法设置图像drawable。使用Image加载库(如Picasso,Glide,Fresco等)将图像加载到ImageView。它肯定会避免outOfMemory问题。

希望这能帮助您克服困难。如果您还有问题,请告诉我。 :)