Android:加快活动之间的共享元素转换

2022-09-02 09:11:57

我在两个活动之间有一个共享元素转换,其工作方式如下:

Intent someintent = new Intent(this, someclass.class);

        if (Build.VERSION.SDK_INT >= 21) {

            ActivityOptions options = ActivityOptions.makeSceneTransitionAnimation(this
                    , new Pair<>(viewClicked.findViewById(R.id.someimage), "someimage")
                    , new Pair<>(viewClicked.findViewById(R.id.someicon), "someicon")
            );
            startActivity(someintent, options.toBundle());
        }
        else {
            startActivity(someintent);
        }

这工作正常,但过渡非常缓慢。当第一次单击图像时,它似乎在过渡发生之前停滞了一两秒钟。这是由于正在加载的活动的“权重”还是延迟可配置?


答案 1

您是否尝试过更改 持续时间 和 :enterTransitionreturntransition

    private Transition enterTransition() {
        ChangeBounds bounds = new ChangeBounds();
        bounds.setDuration(2000);

        return bounds;
    }

    private Transition returnTransition() {
        ChangeBounds bounds = new ChangeBounds();
        bounds.setInterpolator(new DecelerateInterpolator());
        bounds.setDuration(2000);

        return bounds;
    }

在 :onCreate

getWindow().setSharedElementEnterTransition(enterTransition());
getWindow().setSharedElementReturnTransition(returnTransition());

答案 2

另一种方法可能有助于一些已经在其样式中设置了过渡的方法:

if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
    getWindow().getSharedElementEnterTransition().setDuration(2000);
    getWindow().getSharedElementReturnTransition().setDuration(2000)
        .setInterpolator(new DecelerateInterpolator());
}

推荐