使用Picasso的内存不足

时间:2020-12-21 09:19:25

I need to show images in view pager. To show image in image view I use Picasso library. Also I resize image using screen size.
So in my Activity in OnCreate I got view pager:

我需要在view pager中显示图像。要在图像视图中显示图像,我使用Picasso库。我还使用屏幕大小调整图像大小。所以在OnCreate的Activity中我得到了一个view pager:

mPager=(ViewPager)findViewById(R.id.pager); mPagerAdapter = new ScreenSlidePagerAdapter(); mPager.setAdapter(mPagerAdapter);

mPager =(ViewPager)findViewById(R.id.pager); mPagerAdapter = new ScreenSlidePagerAdapter(); mPager.setAdapter(mPagerAdapter);

Then get device size:

然后获取设备大小:

Display display = getWindowManager().getDefaultDisplay();
    Point size = new Point();
    display.getSize(size);
    screenWidth = size.x;
    screenHeight = size.y;

And here is my Page Adapter:

这是我的页面适配器:

 private class ScreenSlidePagerAdapter extends  PagerAdapter {
    PhotoViewAttacher attacher;

    @Override
    public void destroyItem(View collection, int position, Object o) {
        View view = (View) o;
        ((ViewPager) collection).removeView(view);
        view = null;
    }

    @Override
    public void finishUpdate(View arg0) {
        // TODO Auto-generated method stub

    }

    @Override
    public int getCount() {
        return Constants.imageUrls.size();
    }

    @Override
    public Object instantiateItem(View context, int position) {

        LayoutInflater ltInflater = getLayoutInflater();
        View view = ltInflater.inflate(R.layout.page_fragment, null, false);
        final ImageView imageView = (ImageView)view.findViewById(R.id.image);
        attacher = new PhotoViewAttacher(imageView);
        attacher.setZoomable(true);

        Picasso.with(MainActivity.this).setIndicatorsEnabled(true);
        Picasso.with(MainActivity.this)
                .load(Constants.imageUrls.get(position))
                .resize(screenWidth, screenHeight).centerInside()
                .into(imageView, new com.squareup.picasso.Callback() {
                    @Override
                    public void onSuccess() {
                        if (attacher != null) {
                            attacher.update();
                        } else {
                            attacher = new PhotoViewAttacher(imageView);
                        }
                    }

                    @Override
                    public void onError() {
                        // TODO Auto-generated method stub
                        int i = 0;
                    }
                });

                ((ViewPager) context).addView(view);
        return view;
    }

    @Override
    public boolean isViewFromObject(View view, Object object) {
        return view == ((View) object);
    }

    @Override
    public void restoreState(Parcelable arg0, ClassLoader arg1) {
        // TODO Auto-generated method stub
    }

    @Override
    public Parcelable saveState() {
        // TODO Auto-generated method stub
        return null;
    }

    @Override
    public void startUpdate(View arg0) {
        // TODO Auto-generated method stub

    }

}

The problem is that when I scroll 3 times from first image to last then images not showing anymore and I have out of memory in logs. Please help me!!!

问题是,当我从第一张图像滚动3次到最后一次然后图像不再显示时,我的日志内存不足。请帮我!!!

1 个解决方案

#1


0  

I found the problem. It was because every time I created new view.

我发现了问题。这是因为每次我创建新视图。

So now I do next:

所以现在我做下一个:

 list= createListOfImages();
    mPager=(ViewPager)findViewById(R.id.pager);
    mPagerAdapter = new ScreenSlidePagerAdapter(list);
    mPager.setAdapter(mPagerAdapter);

And my adapter:

我的适配器:

   private class ScreenSlidePagerAdapter extends  PagerAdapter {
    List<View> pages;

    public ScreenSlidePagerAdapter(List<View> pages){
        this.pages=pages;
    }

    @Override
    public void destroyItem(ViewGroup container, int position, Object object) {
        container.removeView(pages.get(position));
    }

    @Override
    public int getCount() {
        return Constants.imageUrls.size();
    }

    @Override
    public Object instantiateItem(ViewGroup container, int position) {
        Log.i(TAG,"show image "+position);
        View v = pages.get(position);
        container.addView(v);
        return v;
    }

    @Override
    public boolean isViewFromObject(View view, Object object) {
        return view == ((View) object);
    }

    @Override
    public void restoreState(Parcelable arg0, ClassLoader arg1) {
        // TODO Auto-generated method stub
    }

    @Override
    public Parcelable saveState() {
        // TODO Auto-generated method stub
        return null;
    }
}

And of cource for image I do the next :

和图像的cource我做下一个:

 @Override
protected void onDestroy() {
    super.onDestroy();
    for (int i=0;i<Constants.imageUrls.size();i++){
        ImageView imageView = (ImageView)list.get(i).findViewById(R.id.image);
        imageView.setImageDrawable(null);
    }
}

#1


0  

I found the problem. It was because every time I created new view.

我发现了问题。这是因为每次我创建新视图。

So now I do next:

所以现在我做下一个:

 list= createListOfImages();
    mPager=(ViewPager)findViewById(R.id.pager);
    mPagerAdapter = new ScreenSlidePagerAdapter(list);
    mPager.setAdapter(mPagerAdapter);

And my adapter:

我的适配器:

   private class ScreenSlidePagerAdapter extends  PagerAdapter {
    List<View> pages;

    public ScreenSlidePagerAdapter(List<View> pages){
        this.pages=pages;
    }

    @Override
    public void destroyItem(ViewGroup container, int position, Object object) {
        container.removeView(pages.get(position));
    }

    @Override
    public int getCount() {
        return Constants.imageUrls.size();
    }

    @Override
    public Object instantiateItem(ViewGroup container, int position) {
        Log.i(TAG,"show image "+position);
        View v = pages.get(position);
        container.addView(v);
        return v;
    }

    @Override
    public boolean isViewFromObject(View view, Object object) {
        return view == ((View) object);
    }

    @Override
    public void restoreState(Parcelable arg0, ClassLoader arg1) {
        // TODO Auto-generated method stub
    }

    @Override
    public Parcelable saveState() {
        // TODO Auto-generated method stub
        return null;
    }
}

And of cource for image I do the next :

和图像的cource我做下一个:

 @Override
protected void onDestroy() {
    super.onDestroy();
    for (int i=0;i<Constants.imageUrls.size();i++){
        ImageView imageView = (ImageView)list.get(i).findViewById(R.id.image);
        imageView.setImageDrawable(null);
    }
}