Shared Element Transition on CardView with radius

后端 未结 3 1516
春和景丽
春和景丽 2021-02-01 07:18

I\'ve been working on this problem for weeks and I\'m still unable to solve this problem.

So, I have a CardView that contains a LinearLayout with an ImageView.

3条回答
  •  故里飘歌
    2021-02-01 07:38

    I finally able to solve it. For those who are interested, here's how:

    Why it remove radius before starting transition? Because the target ImageView doesn't have any radius.

    activity_detail.xml

    
    

    When I use CardView without radius, it's not noticable, but it's actually turned into target Shared View.

    1. To achieve radius to no-radius transition you have to set the target Shared View to be rounded. I'm simply wrap it using a Card View (with radius).

    activity_detail.xml

    
    
        
    
    
    
    1. Be sure to change your makeSceneTransition to use "card" instead of "animalImage"

    ListActivity.class

    ActivityOptionsCompat option = ActivityOptionsCompat
    .makeSceneTransitionAnimation(ListActivity.this, cardView, "card");
    
    startActivity(intent, option.toBundle());
    
    1. In the DetailActivity, you can start a radius animation when the transition start.

    DetailActivity.java

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        getWindow().getSharedElementEnterTransition()
            .addListener(new Transition.TransitionListener() {
                @Override
                public void onTransitionStart(Transition transition) {
                    ObjectAnimator animator = ObjectAnimator
                        .ofFloat(activityDetailBinding.card, "radius", 0);
                    animator.setDuration(250);
                    animator.start();
                }
            });
    }
    
    1. Enjoy the smooth transition

    Note: gist for layout and activities

提交回复
热议问题