Skip to content
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

Change format for specific loads and still get transition drawable #840

Closed
eicm opened this issue Dec 25, 2015 · 5 comments · Fixed by #1324
Closed

Change format for specific loads and still get transition drawable #840

eicm opened this issue Dec 25, 2015 · 5 comments · Fixed by #1324
Milestone

Comments

@eicm
Copy link

eicm commented Dec 25, 2015

Is it possible to change the load format between ARGB and RGB_565 for specific loads and still load a drawable to get the transition effect between thumbnail and actual image?

@TWiStErRob
Copy link
Collaborator

You're essentially asking to do .asBitmap().crossFade(), right?

Remember that you can always do your own animation with .animate() or .listener(). I found a way to do something like this a few weeks ago for v4, I'll check tomorrow off I can backport it.

Update: here's the v4 version I was talking about:

.transition(new GenericTransitionOptions<PaletteBitmap>().transition(CROSS_FADE_FACTORY))

private static final TransitionFactory<PaletteBitmap> CROSS_FADE_FACTORY = new TransitionFactory<PaletteBitmap>() {
    private final TransitionFactory<Drawable> realFactory = new DrawableCrossFadeFactory();

    @Override public Transition<PaletteBitmap> build(DataSource dataSource, boolean isFirstResource) {
        final Transition<Drawable> transition = realFactory.build(dataSource, isFirstResource);
        return new Transition<PaletteBitmap>() {
            @Override
            public boolean transition(PaletteBitmap current, ViewAdapter adapter) {
                Resources resources = adapter.getView().getResources();
                Drawable currentBitmap = new BitmapDrawable(resources, current.bitmap);
                return transition.transition(currentBitmap, adapter);
            }
        };
    }
};

I made it for PaletteBitmap, but it's easy to adapt it to any custom transcoded type that has a Bitmap or Drawable.

@TWiStErRob
Copy link
Collaborator

This response is outdated, since 3.8.0 it's possible to do .asBitmap().crossFade(), see #1007.

The heart of all is a custom factory:

private static final GlideAnimationFactory<Bitmap> CROSS_FADE_FACTORY = new GlideAnimationFactory<Bitmap>() {
    private final GlideAnimationFactory<Drawable> realFactory = new DrawableCrossFadeFactory<>();
    @Override public GlideAnimation<Bitmap> build(boolean isFromMemoryCache, boolean isFirstResource) {
        final GlideAnimation<Drawable> transition = realFactory.build(isFromMemoryCache, isFirstResource);
        return new GlideAnimation<Bitmap>() {
            @Override public boolean animate(Bitmap current, ViewAdapter adapter) {
                Resources resources = adapter.getView().getResources();
                Drawable currentBitmap = new BitmapDrawable(resources, current);
                return transition.animate(currentBitmap, adapter);
            }
        };
    }
};

This can be exploded into several classes and realFactory can be anything, most notably DrawableCrossFadeFactory has several constructor overloads.

Assuming you have something like this (you'll see later why I extracted the local variable):

BitmapRequestBuilder<String, Bitmap> request = Glide
    .with(context)
    .load(fullUrl)
    .asBitmap()
    .format(DecodeFormat.PREFER_ARGB_8888)
    .thumbnail(Glide
        .with(context)
        .load(thumbUrl)
        .asBitmap()
    )
;

you have two options, both of them are weird because GenericRequestBuilder.animate(GlideAnimationFactory) is package private.
@sjudd I think it could be made public in 3.7.0 if ever released since v4 already made it public. Also let's use this issue to track the addition of built-in crossFade for asBitmap().

The proper

... yet ugly: I don't like listeners to do stuff, though they were designed that way.

request
    .dontAnimate() // turn off defaults since we're handling it
    .listener(new BitmapCrossfadeListener<>())
    .into(imageView)
;
public class BitmapCrossfadeListener<ModelType> implements RequestListener<ModelType, Bitmap> {
    // CROSS_FADE_FACTORY here
    @Override public boolean onException(Exception e, ModelType model, Target<Bitmap> target, boolean isFirstResource) {
        return false;
    }
    @Override public boolean onResourceReady(Bitmap resource, ModelType model, Target<Bitmap> target, boolean isFromMemoryCache, boolean isFirstResource) {
        return CROSS_FADE_FACTORY
            .build(isFromMemoryCache, isFirstResource)
            .animate(resource, (ViewAdapter)target)
        ;
    }
}

The hacky

but would-be nicer: equivalent to calling .animate(CROSS_FADE_FACTORY).into(imageView) if it was public, see #1007

GlideRequestBuilderAccessor.animate(request, CROSS_FADE_FACTORY);
request.into(imageView);
package com.bumptech.glide;
public class GlideRequestBuilderAccessor {
    public static <T> void animate(GenericRequestBuilder<?, ?, ?, T> request, GlideAnimationFactory<T> factory) {
        request.animate(factory);
    }
}

or, depending on your preferred style, an alternative:

GlideRequestBuilderAccessor.animate(request, CROSS_FADE_FACTORY).into(imageView);

public static <ModelType, DataType, ResourceType, TranscodeType>
GenericRequestBuilder<ModelType, DataType, ResourceType, TranscodeType> animate(
        GenericRequestBuilder<ModelType, DataType, ResourceType, TranscodeType> request,
        GlideAnimationFactory<TranscodeType> factory
    ) {
    return request.animate(factory);
}

@TWiStErRob TWiStErRob added this to the 4.0 milestone Dec 26, 2015
@TWiStErRob
Copy link
Collaborator

Just noticed that #605 is the same and has a simpler solution, which is essentially the total inlining of the core of CROSS_FADE_FACTORY.

@eicm
Copy link
Author

eicm commented Dec 27, 2015

Thanks for the reply.

@sjudd
Copy link
Collaborator

sjudd commented Jan 28, 2016

Agree, pull requests welcome :)

@sjudd sjudd removed their assignment Jan 28, 2016
TWiStErRob added a commit to TWiStErRob/glide that referenced this issue Jul 10, 2016
… to .asBitmap().transition(withCrossFade())

Fixes bumptech#840
Also introduce some missing convenience methods and fix some JavaDoc typos.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants