Giter VIP home page Giter VIP logo

Comments (21)

tole42 avatar tole42 commented on June 2, 2024

"I was having the memory leak issues using a ViewPager" Me too. Any success / progress ?

from photoview.

kutothe avatar kutothe commented on June 2, 2024

I ended up using some of the code that is currently in the dev branch. In PhotoViewAttacher.java I added:

public final void cleanup() {
        mImageView.getViewTreeObserver().removeGlobalOnLayoutListener(this);

        // Clear listeners too
        mMatrixChangeListener = null;
        mPhotoTapListener = null;
    }

Then my PagerAdapter is done a bit different than the examples here, I'll just include the pertinent parts:

private class ImagePagerAdapter extends PagerAdapter {

        private LayoutInflater inflater;
        private PhotoViewAttacher attacher;
        private int currentPos = -1;

        ImagePagerAdapter() {
            inflater = baseActivity.getLayoutInflater();
        }

        @Override
        public void destroyItem(View container, int position, Object object) {
            ((ViewPager) container).removeView((View) object);
        }

        @Override
        public void setPrimaryItem(ViewGroup container, int position, Object object) {
            super.setPrimaryItem(container, position, object);

            if (position != currentPos) {
                currentPos = position;
                if (attacher != null) attacher.cleanup();

                View view = (View) object;
                ImageView imageView = (ImageView) view.findViewById(R.id.image);
                attacher = new PhotoViewAttacher(imageView);
                attacher.setOnPhotoTapListener(new PhotoTapListener());
            }
        }

        @Override
        public Object instantiateItem(View view, int position) {
            final View imageLayout = inflater.inflate(R.layout.item_pager_image, null);
            final ImageView imageView = (ImageView) imageLayout.findViewById(R.id.image);
            final ProgressBar spinner = (ProgressBar) imageLayout.findViewById(R.id.loading);
            final int pos = position;
            PosterObject one = jsonDataObjects.get(position);

            String imageUrl = getAssetsBaseUrl()+"images/posters/"+one.image;

                        // using UniversalImageLoader to lazy load
            imageLoader.displayImage(imageUrl, imageView, options, new SimpleImageLoadingListener() {
                @Override
                public void onLoadingComplete(Bitmap loadedImage) {
                    spinner.setVisibility(View.GONE);
                    if (pos == currentPos) attacher.update();
                }
            });

            ((ViewPager) view).addView(imageLayout, 0);
            return imageLayout;
        }
    }

from photoview.

tole42 avatar tole42 commented on June 2, 2024

thank you!

from photoview.

seanoshea avatar seanoshea commented on June 2, 2024

seanoshea@7c40837 is one possible solution which seems to solve the memory issues with the ViewPager implementation. See https://github.com/seanoshea/krissytosi-android/commit/b5f928ebb14ac1efe8af7c3628467d07ffb92c1b for bringing this implementation into an app.

from photoview.

kutothe avatar kutothe commented on June 2, 2024

Hey Sean. I scanned your code quickly but don't have time ATM to fully review it. Could you summerize what you did to fix the memory issues? Thank you.

from photoview.

httpdispatch avatar httpdispatch commented on June 2, 2024

@kutothe did you see this? #16

from photoview.

chrisbanes avatar chrisbanes commented on June 2, 2024

Hey, I'm currently on vacation but will get this fixed and released when I get back (in a few days).

from photoview.

seanoshea avatar seanoshea commented on June 2, 2024

@kutothe I just made the mImageView member variable a weak reference. I'd be the first to admit that my implementation makes the code a little messy, but it seems to work. I like your cleanup idea too.

from photoview.

httpdispatch avatar httpdispatch commented on June 2, 2024

@seanoshea dev branch version doesn't have memory leak with viewpager. Are you sure you need weak reference?

from photoview.

seanoshea avatar seanoshea commented on June 2, 2024

@httpdispatch if I switch to the dev branch and leave my code as it stands today, there's still a memory leak.

However, if I switch to using a PhotoView object in my ViewPager instead of a regular ImageView, I get the following:

E/AndroidRuntime(26212): android.view.InflateException: Binary XML file line #7: Error inflating class uk.co.senab.photoview.PhotoView
E/AndroidRuntime(26212): at android.view.LayoutInflater.createView(LayoutInflater.java:518)
E/AndroidRuntime(26212): at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:570)
E/AndroidRuntime(26212): at android.view.LayoutInflater.rInflate(LayoutInflater.java:623)
E/AndroidRuntime(26212): at android.view.LayoutInflater.inflate(LayoutInflater.java:408)
E/AndroidRuntime(26212): at android.view.LayoutInflater.inflate(LayoutInflater.java:320)
E/AndroidRuntime(26212): at android.view.LayoutInflater.inflate(LayoutInflater.java:276)
E/AndroidRuntime(26212): at com.krissytosi.fragments.adapters.ImagePagerAdapter.instantiateItem(ImagePagerAdapter.java:86)
E/AndroidRuntime(26212): at android.support.v4.view.PagerAdapter.instantiateItem(PagerAdapter.java:110)
E/AndroidRuntime(26212): at android.support.v4.view.ViewPager.addNewItem(ViewPager.java:649)
E/AndroidRuntime(26212): at android.support.v4.view.ViewPager.populate(ViewPager.java:783)
E/AndroidRuntime(26212): at android.support.v4.view.ViewPager.setAdapter(ViewPager.java:344)
E/AndroidRuntime(26212): at com.krissytosi.fragments.views.PhotoSetDetailView.buildPage(PhotoSetDetailView.java:71)
E/AndroidRuntime(26212): at com.krissytosi.fragments.PhotoSetsFragment.populatePhotoSet(PhotoSetsFragment.java:203)
E/AndroidRuntime(26212): at com.krissytosi.fragments.PhotoSetsFragment.handleOnItemClick(PhotoSetsFragment.java:191)
E/AndroidRuntime(26212): at com.krissytosi.fragments.PhotoSetsFragment.onItemClick(PhotoSetsFragment.java:181)
E/AndroidRuntime(26212): at android.widget.AdapterView.performItemClick(AdapterView.java:284)
E/AndroidRuntime(26212): at android.widget.AbsListView$PerformClick.run(AbsListView.java:1812)
E/AndroidRuntime(26212): at android.os.Handler.handleCallback(Handler.java:587)
E/AndroidRuntime(26212): at android.os.Handler.dispatchMessage(Handler.java:92)
E/AndroidRuntime(26212): at android.os.Looper.loop(Looper.java:130)
E/AndroidRuntime(26212): at android.app.ActivityThread.main(ActivityThread.java:3683)
E/AndroidRuntime(26212): at java.lang.reflect.Method.invokeNative(Native Method)
E/AndroidRuntime(26212): at java.lang.reflect.Method.invoke(Method.java:507)
E/AndroidRuntime(26212): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:839)
E/AndroidRuntime(26212): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:597)
E/AndroidRuntime(26212): at dalvik.system.NativeStart.main(Native Method)
E/AndroidRuntime(26212): Caused by: java.lang.reflect.InvocationTargetException
E/AndroidRuntime(26212): at java.lang.reflect.Constructor.constructNative(Native Method)
E/AndroidRuntime(26212): at java.lang.reflect.Constructor.newInstance(Constructor.java:415)
E/AndroidRuntime(26212): at android.view.LayoutInflater.createView(LayoutInflater.java:505)
E/AndroidRuntime(26212): ... 25 more
E/AndroidRuntime(26212): Caused by: java.lang.NullPointerException
E/AndroidRuntime(26212): at uk.co.senab.photoview.PhotoView.setScaleType(PhotoView.java:139)
E/AndroidRuntime(26212): at android.widget.ImageView.setAdjustViewBounds(ImageView.java:206)
E/AndroidRuntime(26212): at android.widget.ImageView.(ImageView.java:126)
E/AndroidRuntime(26212): at android.widget.ImageView.(ImageView.java:108)
E/AndroidRuntime(26212): at uk.co.senab.photoview.PhotoView.(PhotoView.java:39)
E/AndroidRuntime(26212): ... 28 more

Looks like the mAttacher member variable isn't instantiated when the PhotoView objects are instantiated from XML files?

from photoview.

httpdispatch avatar httpdispatch commented on June 2, 2024

@seanoshea I'm using it in xml layout without a problems. Remove scale type attribute from the xml layout for the PhotoView object

from photoview.

chrisbanes avatar chrisbanes commented on June 2, 2024

@seanoshea That's a separate issue. I've opened #25 for that.

from photoview.

seanoshea avatar seanoshea commented on June 2, 2024

@chrisbanes Sweet - I'd be happy to switch over to the dev branch & see whether the memory issues are resolved without my weak reference fix after #25 is all squared away.

from photoview.

kutothe avatar kutothe commented on June 2, 2024

@httpdispatch I did see #16. I was working with the code from that, and that's where I ended up getting a workaround.

I was away on vacation for a while, but I just got back. I'll be glad to try and contribute a little more to this in the next few days.

from photoview.

chrisbanes avatar chrisbanes commented on June 2, 2024

de94cd6 should fix the memory issues.

from photoview.

httpdispatch avatar httpdispatch commented on June 2, 2024

@chrisbanes why don't you use same code formatting style accross commits? Really hard to track what exactly was changed with the commit

from photoview.

chrisbanes avatar chrisbanes commented on June 2, 2024

It is the same formatter, but the order of methods has changed due to some being made static.

The basic fix was to wrap the ImageView in a WeakReference.

from photoview.

httpdispatch avatar httpdispatch commented on June 2, 2024

@chrisbanes weird that i can't reproduce memory leak even without weakreference fix. Or it was done for the wrapped image views?

from photoview.

chrisbanes avatar chrisbanes commented on June 2, 2024

I still like the use of WeakReference anyway as PhotoViewAttacher now knows when to clean itself up (a cleanup() should happen automatically now).

from photoview.

seanoshea avatar seanoshea commented on June 2, 2024

Tried out the latest from dev & it looks like the memory issues are gone. Thanks!

However, I get IllegalStateExceptions after flicking through a few photos in a ViewPager (https://github.com/seanoshea/krissytosi-android/blob/master/krissytosi/src/com/krissytosi/fragments/adapters/ImagePagerAdapter.java has details of how I'm using a PhotoViewAttacher). Basically, the mImageView gets deallocated but the attacher seems to hang around.

Do you think I'm mis-using the PhotoView, or should I be manually calling cleanup myself?

from photoview.

seanoshea avatar seanoshea commented on June 2, 2024

FWIW, I'm not throwing the exception in seanoshea@318f5c3 & the memory issues appear to have disappeared too.

from photoview.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.