Comments (21)
"I was having the memory leak issues using a ViewPager" Me too. Any success / progress ?
from photoview.
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.
thank you!
from photoview.
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.
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.
@kutothe did you see this? #16
from photoview.
Hey, I'm currently on vacation but will get this fixed and released when I get back (in a few days).
from photoview.
@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.
@seanoshea dev branch version doesn't have memory leak with viewpager. Are you sure you need weak reference?
from photoview.
@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.
@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.
@seanoshea That's a separate issue. I've opened #25 for that.
from photoview.
@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.
@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.
de94cd6 should fix the memory issues.
from photoview.
@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.
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.
@chrisbanes weird that i can't reproduce memory leak even without weakreference fix. Or it was done for the wrapped image views?
from photoview.
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.
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.
FWIW, I'm not throwing the exception in seanoshea@318f5c3 & the memory issues appear to have disappeared too.
from photoview.
Related Issues (20)
- Error implementing dependency HOT 1
- Not working on Android Studio Bumblebee HOT 3
- Dragging gesture is consumed when used in scrollable composable HOT 5
- Exceptions occur when setZoomable(false) and setScaleType(FIT_XY) are used together HOT 1
- Canvas: trying to draw too large(432000000bytes) bitmap. HOT 1
- Profile
- Way to get location of tap outside view?
- Openharmony application
- Is there a Version Update and Release Plan after 2.3.0 HOT 1
- Picture
- Could not resolve all artifacts HOT 4
- Could not resolve all artifacts - com.github.chrisbanes:PhotoView:2.3.0 HOT 5
- Could not resolve com.github.chrisbanes:PhotoView:2.3.0 HOT 1
- Photo HOT 1
- It seems that the Viewpage that does not support sliding up and down? HOT 1
- Scrolling behavior in ViewPager HOT 1
- Why not Migrate to AndroidX
- 图片放大后 比较模糊 请问如何处理 谢谢 HOT 1
- The picture is blurred after enlarging. How to deal with it HOT 1
- How to use PhotoViewAttacher?
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
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.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from photoview.