-
Notifications
You must be signed in to change notification settings - Fork 6.1k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
CenterCrop is not always working #613
Comments
I found a hack that fixes this issue: Glide.with(itemView.getContext()).load(url).asBitmap().centerCrop().diskCacheStrategy(DiskCacheStrategy.ALL).into(new BitmapImageViewTarget(imageView) {
@Override
public void onResourceReady(Bitmap bitmap, GlideAnimation anim) {
super.onResourceReady(bitmap, anim);
Glide.with(itemView.getContext()).load(url).centerCrop().into(imageView);
}
}); EDIT: this fix prevents |
Didn't run your code, but one thing worth trying is changing that wrap_content to match_parent or 0dp on the custom ImageView. Also try it without the custom ImageView altogether, to see if Glide is compatible with your ImageView. In case it works with stock ImageView you could try to generalize the aspect ratio thing into a custom layout (ViewGroup) and have a fill-fill ImageView inside that. |
hello @TWiStErRob thanks for the reply. Actually the code in my project is using a std ImageView, I accidentally added a custom imageview to that sample app . Also I have given the image a set height (150dp) and width (300dp) . Just update the github project to reflect these things. |
Is it possible that the images you load ( |
I'm sorry for guessing, I don't have computer access to compile and debug atm. |
I don't thikn so but lets see. the imageview itself is set to 300 * 150 dp , the image that's being loaded is 615 * 425 px (maybe not on the imgur link, but it is with the real data source). On a htc one m8 the width is 1080 pixels with a densityDPI of 480 which means the the ImageView is actually anyways, if that there was the case, then why is it applying it after the ImageView comes back in the viewport ? Hope this is helpful. I think if you follow the repo steps you'll see what I mean. |
This also happens to me. I will go into my activity and the images will load. If I hit back and then go back into the activity again, they scale correctly the second time around? Also @yrizk I tried your hack and it seems to work but then it no longer animates. EDIT: Moving super.onResourceReady after setting my imagedrawable keeps the animation. |
Also from playing around if I set my src and have it set to centerCrop on the imageview, it seems glide will display the new image the same exact size on the initial load, if you go leave and go back so it displays it again, it displays correctly. Maybe layout issue of some sort or how the image takes cropping? EDIT: One new note, if you set the imageview resource on SetResource that is when it happens, it seems to work fine using SetResourceReady.
|
Sooo, I ran your example app. Sorry for the delay, I was AFK. Here's my investigation: First run: buggy, symptoms are: first screen of images load correctly, along with partially visible list items. The next row below that doesn't seem to apply centerCrop, but rather fitCenter, neither the next one, but after that centerCrop is correctly applied again, implication: the problem has to do with recycling the item views. The layout and is too complex to debug, simplify and gather more info:
What's up with that Test image: http://i.imgur.com/jMvECu2.jpg, original size: 604x453
So, indeed the bitmap is loaded with bigger than the view size, fitting applies. Why is Glide loading a 900x450 image into a clearly 476x450 size ImageView? imageView.setTag(R.id.image, imgurImage.getLink()); this helps to identify which hits to the
App startup: no hits as expected, scroll down, breakpoint hit.
Aah, makes sense, since the XML says TL;DR and summary Then that item scrolls off screen and gets reused. When scrolled back the same image is bound to a recycled item which means it was already laid out and now Solutions
I still suggest to move <ImageView
...
android:layout_width="match_parent" <!-- this makes Glide wait -->
android:layout_height="150dp"
android:scaleType="centerCrop"
/> it also simplifies the loading to: Glide.with(itemView.getContext()).load(imgurImage.getLink()).into(imageView); and all the benefits of resource directories are unleashed, for example you can use |
This is the second solution I described, it just hides the issue more, please review your layout and try to make it more dynamic. |
Thanks twister. So for now just do these workarounds, will this possibly be fixed in the future? |
It cannot be fixed as I see it. Reading the |
Thanks, understood. I currently use match_parent but I also use match_parent for my height because I want aspect ratio and I measure on imageview which creates a perfect square. Not sure if this is bad practice though. |
I use a simple layout around the ImageView to make it square: https://gist.github.com/TWiStErRob/f8190f1f55ec1e7777fc. It plays real nice with Glide so far. |
Thanks, that is how mine is setup and is working great with glide. Was just checking to make sure what I was doing was ok. |
This also looks like a duplicate of #291. |
Yes I agree. |
@TWiStErRob thank you so much for that very detailed investigation. Seriously, I'm impressed. That |
That's what you get for providing a sample project... ;) @yrizk the centerCrop disappears because the upscaled centerCropped image is fitCentered (even if you say
When you have Feel free to ask more if this doesn't clear it up! |
yes I understand exactly what you mean now. and wow you keep raising the bar!! Seriously thank you again this is all very illuminating. I'd buy you a drink , but I just looked into int'l fund transfer and its discouraging. if you've had success with paypal lmk :) Glide takes the cake for best support in my book |
when I change placeholder image size, it's ok. OMG!!!!!! |
My ImageView has a |
@SarvagyaVaish What happens when you don't use match_parent. can you show us the issue from a screenshot? |
After further testing I found out that the problem was with setting a placeholder image. I was setting a square placeholder image. Once Glide fetched the actual rectangular image it replaced the placeholder with it, but ended up forcing it to be square. Thanks! |
I still suggest to move centerCrop() into XML, because it's a view thing, not logic:
it also simplifies the loading to: Glide.with(itemView.getContext()).load(imgurImage.getLink()).into(imageView); this fix my problem ! |
i just removed placheldor from glide and it's worked for me :) |
fix #1797 Apparently, we ran into a bug similar to the one described in bumptech/glide#613 (comment) as we cropped twice (in addition to the cropping I removed, we also set `android:scaleType="centerCrop"` in `thumbnail_view.xml`)
fix #1797 Apparently, we ran into a bug similar to the one described in bumptech/glide#613 (comment) as we cropped twice (in addition to the cropping I removed, we also set `android:scaleType="centerCrop"` in `thumbnail_view.xml`)
this bug is still in version 4.13.0 |
Glide Version/Integration library (if any): 3.6.0
Device/Android Version: any
Issue details/Repro steps/Use case background:
How to repo:
I have a demo app with this bug here: https://github.com/yrizk/glide-centercrop-bug
but essentially...
no scale type declared in the xml
centerCrop() when loading image into an imageview.
make sure the app is cleared from memory
The image will start w/o the center crop, then if you scroll past or above it, then sometimes it comes back with the correct scale type.
on a separate note, the library is really good! thank you guys for building and supporting this
The text was updated successfully, but these errors were encountered: