Shared Element Transition and Fresco not working properly
Asked Answered
V

3

5

I have two Activities, both of which contain an image. I am using Fresco to load the image in one activity and Picasso to load image in another activity. Here are the relevant parts of my code:

Image in first activity

<com.facebook.drawee.view.SimpleDraweeView
                android:id="@+id/imageView102"
                android:transitionName="image"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_gravity="center_horizontal"
                android:layout_marginLeft="9dp"
                android:layout_marginRight="9dp"
                android:layout_marginTop="10dp"
                fresco:actualImageScaleType="centerCrop"
                fresco:placeholderImage="@color/wait_color"
                fresco:placeholderImageScaleType="fitCenter"
                fresco:viewAspectRatio="1.33"
                android:layout_marginBottom="10dp" />

Image in second activity

<uk.co.senab.photoview.PhotoView
        android:id="@+id/zoomable"
        android:transitionName="image"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_centerInParent="true" />

I am using PhotoView in second activity to zoom in and zoom out the image.

First Activity

Uri uri = Uri.parse(photoUrl);
        ImageRequest request = ImageRequestBuilder.newBuilderWithSource(uri)
                .setProgressiveRenderingEnabled(true)
                .build();
        DraweeController controller = Fresco.newDraweeControllerBuilder()
                .setImageRequest(request)
                .setOldController(image.getController())
                .build();
        image.setController(controller);
image.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Intent intent = new Intent(ImageActivity.this, AlternateFullImageActivity.class);
                intent.putExtra("ID", photoId);
                intent.putExtra("photoUrl", photoUrl);
                if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
                    ActivityOptionsCompat options = ActivityOptionsCompat.
                            makeSceneTransitionAnimation(ImageActivity.this, (View)image, "image");
                    startActivity(intent, options.toBundle());
                }
                else {
                    startActivity(intent);
                }
            }
        });

Second Activity

Intent intent = getIntent();
        photoId = intent.getExtras().getString("ID");
        photoUrl = intent.getExtras().getString("photoUrl");

        Picasso.with(AlternateFullImageActivity.this)
                .load(photoUrl)
                .into(image);
        mAttacher = new PhotoViewAttacher(image);

The problem is, the transition is not smooth and very fast. I read here that I need to change the transition to ChangeBounds. How do I change the transition to that and how do I add duration to this transition like say, 1000ms?

Valuable answered 27/1, 2016 at 14:25 Comment(3)
I know its a bit late but why are you loading the other image with Picasso? Also, you can create a simple class that extends TransitionSet and add there transitions like ChangeBounds, you can also add duration and even delay.Wenzel
I now load both images using Fresco. Can you tell me what would be the code to change the shared element transition to ChangeBounds as suggested by facebook.Valuable
I suppose you use Picasso in the second Activity because you need to allow zoom. For future references I will recommend this lib to allow zooming on Fresco: github.com/ongakuer/PhotoDraweeViewGardenia
W
4

You can create a class that extends TransitionSet to specify you transitions, like ChangeBounds. For example...

@TargetApi(Build.VERSION_CODES.LOLLIPOP)
public class DetailTransition extends TransitionSet {
    public DetailsTransition(int duration, int delay) {
        setOrdering(ORDERING_TOGETHER);
        addTransition(new ChangeBounds()).
                addTransition(new ChangeTransform()).
                addTransition(new ChangeImageTransform()).setDuration(duration).setStartDelay(delay).setInterpolator(new AnticipateOvershootInterpolator());
    }
}

and then you set shared element transition onto your fragment or activity / window. For fragments like this

currentFragment.setSharedElementEnterTransition(new DetailsTransition(1000, 400));

or for activity like this

getWindow().setSharedElementEnterTransition(new DetailsTransition(1000, 400));
Wenzel answered 27/5, 2016 at 13:37 Comment(0)
G
9

In new versions of Fresco there is a Custom transition to do it easily.

Look at this example in their own repository:

https://github.com/facebook/fresco/tree/master/samples/transition

Basically, you have to call setSharedElementEnterTransition() with the corresponding transition on the opening activity :

getWindow().setSharedElementEnterTransition(DraweeTransition.createTransitionSet(ScalingUtils.ScaleType.CENTER_CROP,ScalingUtils.ScaleType.FIT_CENTER));
getWindow().setSharedElementReturnTransition(DraweeTransition.createTransitionSet(ScalingUtils.ScaleType.FIT_CENTER,ScalingUtils.ScaleType.CENTER_CROP));
Gardenia answered 21/7, 2016 at 13:26 Comment(0)
W
4

You can create a class that extends TransitionSet to specify you transitions, like ChangeBounds. For example...

@TargetApi(Build.VERSION_CODES.LOLLIPOP)
public class DetailTransition extends TransitionSet {
    public DetailsTransition(int duration, int delay) {
        setOrdering(ORDERING_TOGETHER);
        addTransition(new ChangeBounds()).
                addTransition(new ChangeTransform()).
                addTransition(new ChangeImageTransform()).setDuration(duration).setStartDelay(delay).setInterpolator(new AnticipateOvershootInterpolator());
    }
}

and then you set shared element transition onto your fragment or activity / window. For fragments like this

currentFragment.setSharedElementEnterTransition(new DetailsTransition(1000, 400));

or for activity like this

getWindow().setSharedElementEnterTransition(new DetailsTransition(1000, 400));
Wenzel answered 27/5, 2016 at 13:37 Comment(0)
T
0

Finally I found a solution, By following 3 steps.

1. Add the below class in your project:

import com.facebook.drawee.generic.GenericDraweeHierarchy;
import com.facebook.drawee.view.SimpleDraweeView;

import android.content.Context;
import android.graphics.Matrix;
import android.util.AttributeSet;

public class TranslateDraweeView extends SimpleDraweeView {
    public TranslateDraweeView(Context context) {
        super(context);
    }

    public TranslateDraweeView(Context context, AttributeSet attrs) {
        super(context, attrs);
    }

    public TranslateDraweeView(Context context, AttributeSet attrs, int defStyle) {
        super(context, attrs, defStyle);
    }

    public TranslateDraweeView(Context context, GenericDraweeHierarchy hierarchy) {
        super(context, hierarchy);
    }

    public void animateTransform(Matrix matrix) {
        invalidate();
    }
}

2. Replace SimpleDraweeView in XML layout file with TranslateDraweeView

3. Add the Code in your calling activity:

Intent i = new Intent(FirstActivity.this, SecActivity.class);

    String transitionName = "Detail Transition";

    ActivityOptions transitionActivityOptions;
    if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.LOLLIPOP) {

//******* THIS IS MORE IMPORTANT *******************
        setExitSharedElementCallback(new SharedElementCallback() {

            @Override
            public void onSharedElementEnd(List<String> sharedElementNames,
                                           List<View> sharedElements,
                                           List<View> sharedElementSnapshots) {

                super.onSharedElementEnd(sharedElementNames, sharedElements,
                        sharedElementSnapshots);

                for (View view : sharedElements) {
                    if (view instanceof SimpleDraweeView) {
                        view.setVisibility(View.VISIBLE);
                    }
                }
            }
        });

//***********************************************

        transitionActivityOptions = ActivityOptions.makeSceneTransitionAnimation(this,
                image, transitionName);
        startActivity(i, transitionActivityOptions.toBundle());
    }

Happy Coding...!

Troup answered 15/3, 2019 at 5:5 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.