andkulikov / Transitions-Everywhere

Set of extra Transitions on top of Jetpack Transitions Library
Apache License 2.0
4.83k stars 486 forks source link

Transition animation dosen't seem to work on Pre-Lollipop #59

Closed ClaudeHangui closed 7 years ago

ClaudeHangui commented 7 years ago

From this sample app I tried to implement fragment transition with shared elements. Here is a snippet from the sample :

` import android.annotation.TargetApi; import android.content.Context; import android.os.Build; import android.transition.ChangeBounds; import android.transition.ChangeImageTransform; import android.transition.ChangeTransform; import android.transition.TransitionSet; import android.util.AttributeSet;

@TargetApi(Build.VERSION_CODES.LOLLIPOP) public class DetailsTransition extends TransitionSet { public DetailsTransition() { init(); }

/**
 * This constructor allows us to use this transition in XML
 */
public DetailsTransition(Context context, AttributeSet attrs) {
    super(context, attrs);
    init();
}

private void init() {
    setOrdering(ORDERING_TOGETHER);
    addTransition(new ChangeBounds()).
            addTransition(new ChangeTransform()).
            addTransition(new ChangeImageTransform());
}

} `

This is my version using your library (for pre-Lollipop): ` import com.transitionseverywhere.ChangeBounds; import com.transitionseverywhere.ChangeImageTransform; import com.transitionseverywhere.ChangeTransform; import com.transitionseverywhere.TransitionSet;

public class DetailsBackPortTransition extends TransitionSet {

public DetailsBackPortTransition(){
    init();
}

private void init()
{
    setOrdering(ORDERING_TOGETHER);
    addTransition(new ChangeBounds())
            .addTransition(new ChangeTransform())
            .addTransition(new ChangeImageTransform());
}

}`

Here is the fragment class (assume there's an adapter, a listener interface, and a Details fragment. A click from an item in the list opens up the DetailsFragment in an animated fashion. Like I said, I tested your library for animation on pre-Lollipop (I tested it on a samsung having KitKat) but it doesn't seem to work. It opens without any animation:

`public class GridFragment extends Fragment implements KittenClickListener {

@Nullable
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
    return inflater.inflate(R.layout.fragment_grid, container, false);
}

@Override
public void onViewCreated(View view, @Nullable Bundle savedInstanceState) {
    super.onViewCreated(view, savedInstanceState);

    RecyclerView recyclerView = (RecyclerView) view.findViewById(R.id.recyclerview);
    recyclerView.setAdapter(new KittenGridAdapter(6, this));
    recyclerView.setLayoutManager(new GridLayoutManager(getContext(), 2));
}

@Override
public void onKittenClicked(KittenViewHolder holder, int position) {
    int kittenNumber = (position % 6) + 1;

    DetailsFragment kittenDetails = DetailsFragment.newInstance(kittenNumber);

    // Note that we need the API version check here because the actual transition classes (e.g. Fade)
    // are not in the support library and are only available in API 21+. The methods we are calling on the Fragment
    // ARE available in the support library (though they don't do anything on API < 21)

    if (Build.VERSION.SDK_INT < Build.VERSION_CODES.LOLLIPOP) {
        kittenDetails.setSharedElementEnterTransition(new DetailsBackPortTransition());
        kittenDetails.setEnterTransition(new com.transitionseverywhere.Fade());
        setExitTransition(new com.transitionseverywhere.Fade());
        kittenDetails.setSharedElementReturnTransition(new DetailsBackPortTransition());
    }
    else         
    {
        kittenDetails.setSharedElementEnterTransition(new DetailsTransition());
        kittenDetails.setEnterTransition(new Fade());
        setExitTransition(new Fade());
        kittenDetails.setSharedElementReturnTransition(new DetailsTransition());
    }

    getActivity().getSupportFragmentManager()
            .beginTransaction()
            .addSharedElement(holder.image, "kittenImage")
            .replace(R.id.container, kittenDetails)
            .addToBackStack(null)
            .commit();
}

} `

andkulikov commented 7 years ago

Hello. Fragment transitions is not backported. You cannot provide a class from com.transitionseverywhere into setExitTransition. And also ChangeImageTransform in not backported as well. Please, check out the article https://medium.com/@andkulikov/animate-all-the-things-transitions-in-android-914af5477d50#.a70zesphv

ClaudeHangui commented 7 years ago

Yeah, it's actually your article that brought me here. But I thought your library supports Animations backport to Android 4.0+. Moreover I did not have crash when I called ChangeImageTransform on the samsung device (android v19).. Besides I quote you "Activity Transitions can’t be backported, sorry (sadpanda). A lot of logic is hidden in the Activity. The same applies to Fragment transitions. We should create our own Fragment to change transitions logic."

From your tutorial with horizontal progressbar, you created a custom transition .....Does this mean I have to do the same ???

andkulikov commented 7 years ago

Yes it not crashed. It just don't have any action for pre-lollipop. By "We should create our own Fragment to change transitions logic." I meant that we can't change sources of support fragments library that why there is no way to implement it. But it could be implemented with Scenes inside one activity(fragment). Please check scenes sample. If I will find some time in a future I create more advanced sample of something like shared element transition via scenes.