使用毕加索的活动之间的共享过渡

时间:2019-02-15 18:22:53

标签: android android-animation shared-element-transition

使用Picasso异步加载图像时,共享转换存在问题。 如果我要使用带有可绘制对象的简单ImageView,它将很好用。

但是,当我使用毕加索时,返回的动画无法正确缩放图像。

您可以更好地看到我在github存储库中描述的问题: https://github.com/brunoperezm/SharedTransitionsAsync

调用和被调用活动的代码也可以在我的仓库中看到,如下所示:

MainActivity.java

package com.example.sharedtransitionsasync;

import ...

public class MainActivity extends AppCompatActivity {
    ImageView imageView;
    @Override
    protected void onCreate (Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

//      getWindow().setSharedElementEnterTransition(new DetailsTransition());
        getWindow().setSharedElementReturnTransition(new DetailsTransition());


        imageView = findViewById(R.id.my_image_view1);
        Picasso.get()
                .load("https://i.redd.it/jb1okqafolg21.jpg")
                .fit()
                .into(imageView, new Callback() {
                    @Override
                    public void onSuccess () {
                        startPostponedEnterTransition();
                    }

                    @Override
                    public void onError (Exception e) {
                        startPostponedEnterTransition();
                    }
                });

    }

    public void gotoActivity2 (View view) {
        Intent intent = new Intent(this, Main2Activity.class);
        ActivityOptionsCompat optionsCompat = ActivityOptionsCompat.makeSceneTransitionAnimation(this, imageView, "profile");
        startActivity(intent, optionsCompat.toBundle());
    }

    public class DetailsTransition extends TransitionSet {
        public DetailsTransition() {
            setOrdering(ORDERING_TOGETHER);
            addTransition(new ChangeBounds()).
                    addTransition(new ChangeTransform()).
                    addTransition(new ChangeImageTransform());
        }
    }

}

Main2Activity.java

package com.example.sharedtransitionsasync;

import ...

public class Main2Activity extends AppCompatActivity {
    ImageView imageView;
    @Override
    protected void onCreate (Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main2);


        getWindow().setSharedElementReturnTransition(new DetailsTransition());


        postponeEnterTransition();

        imageView = findViewById(R.id.my_image_view2);
        Picasso.get()
                .load("https://i.redd.it/jb1okqafolg21.jpg")
                .fit()
                .centerCrop()
                .into(imageView, new Callback() {
                    @Override
                    public void onSuccess () {
                        scheduleStartPostponedTransition(imageView);
                    }

                    @Override
                    public void onError (Exception e) {

                    }
                });
    }

    private void scheduleStartPostponedTransition(final View sharedElement) {
        sharedElement.getViewTreeObserver().addOnPreDrawListener(
                new ViewTreeObserver.OnPreDrawListener() {
                    @Override
                    public boolean onPreDraw () {
                        sharedElement.getViewTreeObserver().removeOnPreDrawListener(this);
                        startPostponedEnterTransition();
                        return true;
                    }
                });
    }

    @Override
    public void onBackPressed () {
        super.onBackPressed();
        supportFinishAfterTransition();
    }
    public class DetailsTransition extends TransitionSet {
        public DetailsTransition() {
            setOrdering(ORDERING_TOGETHER);
            addTransition(new ChangeBounds()).
            addTransition(new ChangeTransform()).
            addTransition(new ChangeImageTransform()).
            setDuration(5000);
        }
    }

}

1 个答案:

答案 0 :(得分:0)

在您的两个xml中

在图像视图上都使用此scaleType

android:scaleType="centerCrop"

然后在毕加索上调用这两个活动,删除.fit()和.centerCrop()

现在在您的git repo上效果很好