-
Notifications
You must be signed in to change notification settings - Fork 6.2k
crossFade messes up aspect ratio for thumbnail/placeholder #363
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
Comments
Can you try overriding the Target and avoiding the SquaringDrawable? |
Based on Is this what you meant? |
Yup that makes sense. Unfortunately if SquaringDrawable isn't involved, then this is an issue with TransitionDrawable which is owned by the framework. I'd love to write our own cross fading Drawable because TransitionDrawable seems to have a number of issues, and this just reinforces that thought. However I can't say it's super likely to happen any time soon if left to just me... |
Just faced this with a simple request with a place holder and crossfade. Changing crossfade to animation solved this, so really sounds like a TransitionDrawable problem. But maybe there should be some kind of notice somewhere because it took me some time to find this issue :( |
@sjudd : Update :( It seems there's a crossfade by default applied so in fact this "problem" is quite important. |
Yeah it is applied by default... Again a pull request would be welcome here if anyone wants to try to take on writing a better TransitionDrawable specifically for cross fades. |
I'm surprised there is no resolution for this issue yet. We've had to disable all crossfades because of this since we use a square placeholder and load images with various aspect ratios. We are using a fade in of the image, but this is not as good as a crossfade since the placeholder just disappears . Anyone else have a better work around? |
Long shot: since you have square placeholder I assume the space for the images isn't much wider, how about creating Drawable with the space's aspect (pad the square) and/or add a transformation for the main image that adds tranparent padding to the loaded Bitmap. This would result in the layers of TransitionDrawable having the same intrinsic size aspect, and hence working as intended. Alternatively, but in a similar fashion: placeholder with same aspect as available space and centerCrop so the loaded Bitmap has exactly the space's size (and aspect). Sadly both of these are hacky workarounds, if you feel like it, you could fix Android's TransitionDrawable to handle different aspects. |
@sjudd @TWiStErRob import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.Canvas;
import android.graphics.ColorFilter;
import android.graphics.Rect;
import android.graphics.drawable.AnimationDrawable;
import android.graphics.drawable.BitmapDrawable;
import android.graphics.drawable.Drawable;
import android.os.Build;
import android.os.SystemClock;
import android.widget.ImageView;
public final class GlideTransitionDrawable extends BitmapDrawable
{
// Only accessed from main thread.
private static final float FADE_DURATION = 800f; //ms
/**
* Create or update the drawable on the target {@link ImageView} to display the supplied bitmap
* image.
*/
public static void setBitmap(ImageView target, Context context, Bitmap bitmap)
{
Drawable placeholder = target.getDrawable();
if (placeholder instanceof AnimationDrawable)
{
((AnimationDrawable) placeholder).stop();
}
GlideTransitionDrawable drawable = new GlideTransitionDrawable(context, bitmap, placeholder);
target.setImageDrawable(drawable);
}
/**
* Create or update the drawable on the target {@link ImageView} to display the supplied
* placeholder image.
*/
public static void setPlaceholder(ImageView target, Drawable placeholderDrawable)
{
target.setImageDrawable(placeholderDrawable);
if (target.getDrawable() instanceof AnimationDrawable)
{
((AnimationDrawable) target.getDrawable()).start();
}
}
Drawable placeholder;
long startTimeMillis;
boolean animating;
int alpha = 0xFF;
GlideTransitionDrawable(Context context, Bitmap bitmap, Drawable placeholder)
{
super(context.getResources(), bitmap);
this.placeholder = placeholder;
animating = true;
startTimeMillis = SystemClock.uptimeMillis();
}
@Override
public void draw(Canvas canvas)
{
if (!animating)
{
super.draw(canvas);
} else
{
float normalized = (SystemClock.uptimeMillis() - startTimeMillis) / FADE_DURATION;
if (normalized >= 1f)
{
animating = false;
placeholder = null;
super.draw(canvas);
} else
{
if (placeholder != null)
{
placeholder.draw(canvas);
}
int partialAlpha = (int) (alpha * normalized);
super.setAlpha(partialAlpha);
super.draw(canvas);
super.setAlpha(alpha);
if (Build.VERSION.SDK_INT <= Build.VERSION_CODES.GINGERBREAD_MR1)
{
invalidateSelf();
}
}
}
}
@Override
public void setAlpha(int alpha)
{
this.alpha = alpha;
if (placeholder != null)
{
placeholder.setAlpha(alpha);
}
super.setAlpha(alpha);
}
@Override
public void setColorFilter(ColorFilter cf)
{
if (placeholder != null)
{
placeholder.setColorFilter(cf);
}
super.setColorFilter(cf);
}
@Override
protected void onBoundsChange(Rect bounds)
{
if (placeholder != null)
{
placeholder.setBounds(bounds);
}
super.onBoundsChange(bounds);
}
} I use this with a custom call at the moment, not ideal but it works. If someone could help me implement this in the Glide code I would be thankful. |
the ideal fix would be not a workaround |
@softvision-mariusduna My code is not a workaround, it should be implemented instead of a TransitionDrawable (which screws up the aspect ratio). I use it in my app and it works great! |
@kevinvanmierlo TransitionDrawable is currently used in Glide here: https://github.com/bumptech/glide/blob/master/library/src/main/java/com/bumptech/glide/request/transition/DrawableCrossFadeTransition.java#L44. A pull request would be welcome! |
@sjudd Ahh thanks! I'll make a pull request when I have the time! Which is probably the next couple days or the beginning of next week. |
@sjudd It's going to take a little while longer. I don't have much time and I can't use BitmapDrawable, so I have to find another solution using Drawable. |
@sjudd I found some time again, sorry that it takes so long. I wanted to test some options, but the library doesn't have |
master is 4.0, which has breaking changes and simplifications .apply(RequestOptions.placeholderOf(drawable)) //=.placeholder(drawable) You don't need GlideDrawable, crossFade has to work between any two Drawables. Sam's link is still valid though. |
@TWiStErRob thanks for the quick response! Didn't know that. Do you perhaps know if the new |
@TWiStErRob This is the answer that i used to overcome this bug. For placeholders that are smaller than the full image. http://stackoverflow.com/a/41459535/3671509 |
I'm still seeing this issue on 3.7.0 and the only workaround for me are either .dontAnimate or .animate() but I would rather have a crossfade. Any suggestions? |
@sheaam30 the issue is not closed; the simplest workaround is to have your placeholder and image aspect ratio match |
I guess this is no longer relevant in Glide v4 since there is no longer any transformation (and it seems fine to me as well) |
@louistsaitszho |
I'm still open to pull requests to improve the cross fade behavior, but I think the limitations are reasonably well understood and documented, so I'm going to remove the bug label. |
above code works fine, trouble starts when we use |
@TWiStErRob how to handle your imageview is inisde ConstraintLayout with
how do I generate the placeholder and image aspect ratio match? |
Using .thumbnail(GlideApp.with(this).load(R.drawable.placeholder))
.transition(withCrossFade()) instead of |
Having the same issue, however I find that even with the fix here: https://stackoverflow.com/a/36944010/253468 it still messes up. I do notice that |
Okay guys, sorry for the delay, but had some time again. I'm going to create a pull request, but before it gets accepted here is a gist with the code you can use in the meantime: https://gist.github.com/kevinvanmierlo/c46f66027e3ae37ebea85a8d2e12aaba. |
+ In the ImageDetailAdapter load the HD URL of the image while showing the thumbnail image that we got while displaying the grid view + Implement the new function in GlideImageLoader - Not enabling transition in the new method because of an existing bug in Glide - bumptech/glide#363 + Enable crossfade for loadImage function. This will be used for the ImageGridFragment, fade out the progressDrawable and fadein the image
Where is this holding? |
I don't know, haven't gotten any response anymore. I created the pull request. But Travis keeps failing and I don't really know why. I fixed all formatting issues I could find, but it still fails. I asked for help, but no response. |
[MOD] Cleaner svg/image loading without thumbnail/placeholder (see bumptech/glide#363)
I ended up using a vector over PNG as the placeholder and haven't seen any stretching |
Glide Version/Integration library (if any): 3.5.2
Device/Android Version: S4/4.4
Issue details/Repro steps: Load a different sized thumbnail and image, or placeholder and image and note the result:
Glide load line:
By removing all the delaying parts, the end result is better, but the now the thumbnail is messed up:
The text was updated successfully, but these errors were encountered: