了解退出/重新输入共享单元转换 [英] Understanding exit/reenter shared element transitions

查看:446
本文介绍了了解退出/重新输入共享单元转换的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在做Android的L.一些基本的勘探共享单元转换的简单的例子,我设置了一个图像视图从屏幕到屏幕中的活动转变底部顶​​部翻译,我已经延长过渡时间,所以我能看到的东西的工作。我已经打了两个问题,到目前为止试图了解共享的元素转换是如何工作的。

1)当只使用输入/返回转换(退出/重新输入设置为null)。在进入过渡是好的,但是当后退按钮是pressed视图动画的时间,停止,然后重新出现在最后的位置。似乎类似于<一href="http://stackoverflow.com/questions/26567822/hiccups-in-activity-transitions-with-shared-elements">this问题但我已经将所有的存在/重新输入转换为空,所以不知道为什么会发生。

2)当只使用退出/重新输入转换(输入/返回设置为null)。什么也没有发生,则视图过渡​​下来像其以下默认进入过渡(300毫秒的持续时间)的屏幕上,和当后面是pressed视图弹回到它的初始位置。

我如何使用退出/重新输入转换?

下面是我的code:

activity_main.xml

 &LT; RelativeLayout的的xmlns:机器人=htt​​p://schemas.android.com/apk/res/android
    的xmlns:工具=htt​​p://schemas.android.com/tool​​s机器人:layout_width =match_parent
    机器人:layout_height =match_parent机器人:以下属性来=@扪/ activity_horizo​​ntal_margin
    机器人:paddingRight =@扪/ activity_horizo​​ntal_margin
    机器人:paddingTop =@扪/ activity_vertical_margin
    机器人:paddingBottom会=@扪/ activity_vertical_margin工具:上下文=MainActivity。&GT;

    &LT; ImageView的
        机器人:layout_width =WRAP_CONTENT
        机器人:layout_height =WRAP_CONTENT
        机器人:ID =@ + ID / ImageView的
        机器人:SRC =@可绘制/ ic_launcher
        机器人:layout_alignParentTop =真
        机器人:layout_centerHorizo​​ntal =真/&GT;

    &LT;按钮
        机器人:layout_width =WRAP_CONTENT
        机器人:layout_height =WRAP_CONTENT
        机器人:文本=动画!
        机器人:ID =@ + ID /按钮
        机器人:layout_centerVertical =真
        机器人:layout_alignParentStart =真/&GT;

&LT; / RelativeLayout的&GT;
 

activity_second.xml

 &LT; ImageView的
    机器人:layout_width =WRAP_CONTENT
    机器人:layout_height =WRAP_CONTENT
    机器人:ID =@ + ID / imageView2
    机器人:SRC =@可绘制/ ic_launcher
    机器人:layout_alignParentBottom =真
    机器人:layout_centerHorizo​​ntal =真/&GT;
 

MainActivity.java

 公共类MainActivity延伸活动{

    @覆盖
    保护无效的onCreate(包savedInstanceState){
        super.onCreate(savedInstanceState);
        。getWindow()requestFeature(Window.FEATURE_CONTENT_TRANSITIONS);
        。getWindow()requestFeature(Window.FEATURE_ACTIVITY_TRANSITIONS);
        。getWindow()setAllowEnterTransitionOverlap(假);
        。getWindow()setAllowReturnTransitionOverlap(假);


        。getWindow()setSharedElementExitTransition(exitTransition());
        getWindow()setSharedElementReenterTransition(reenterTransition())。
        //getWindow().setSharedElementExitTransition(null);
        //getWindow().setSharedElementReenterTransition(null);


        的setContentView(R.layout.activity_main);

        最后查看的iView = findViewById(R.id.imageView);
        iView.setTransitionName(图像);

        最后Button按钮=(按钮)findViewById(R.id.button);
        button.setOnClickListener(新View.OnClickListener(){
            @覆盖
            公共无效的onClick(视图v){
                意向意图=新的意图(MainActivity.this,SecondActivity.class);
                ActivityOptions选项= ActivityOptions
                        .makeSceneTransitionAnimation(MainActivity.this,iView中,图像);
                startActivity(意向,options.toBundle());
            }
        });
    }

    私人过渡exitTransition(){
        ChangeBounds边界=新ChangeBounds();
        bounds.setInterpolator(新BounceInterpolator());
        bounds.setDuration(2000);

        返回界限;
    }

    私人过渡reenterTransition(){
        ChangeBounds边界=新ChangeBounds();
        bounds.setInterpolator(新OvershootInterpolator());
        bounds.setDuration(2000);

        返回界限;
    }
}
 

SecondActivity.java

 公共类SecondActivity延伸活动{
    @覆盖
    保护无效的onCreate(包savedInstanceState){
        super.onCreate(savedInstanceState);
        。getWindow()requestFeature(Window.FEATURE_CONTENT_TRANSITIONS);
        。getWindow()requestFeature(Window.FEATURE_ACTIVITY_TRANSITIONS);
        。getWindow()setAllowEnterTransitionOverlap(假);
        。getWindow()setAllowReturnTransitionOverlap(假);


        // getWindow()setSharedElementEnterTransition(enterTransition());
        // getWindow()setSharedElementReturnTransition(returnTransition());
        。getWindow()setSharedElementEnterTransition(空);
        。getWindow()setSharedElementReturnTransition(空);


        的setContentView(R.layout.activity_second);

        最后查看的iView = findViewById(R.id.imageView2);
        iView.setTransitionName(图像);
    }

    @覆盖
    公共无效onBack pressed(){
        super.onBack pressed();
        finishAfterTransition();
    }

    私人过渡enterTransition(){
        ChangeBounds边界=新ChangeBounds();
        bounds.setDuration(2000);

        返回界限;
    }

    私人过渡returnTransition(){
        ChangeBounds边界=新ChangeBounds();
        bounds.setInterpolator(新DecelerateInterpolator());
        bounds.setDuration(2000);

        返回界限;
    }
}
 

解决方案

我记得,有导致,如果它需要更长的时间比重新输入转换时间共享的元素返回变换被打断L中的一个错误。如果你调整你重新输入转换时间(在调用的Activity),这应该修复中断的问题,直到错误在MR1。

退出并重新进入过渡是共享的元素之前执行的东西是允许的过渡。例如,如果你想转接前解除您共享的元素,这将是在共享单元退出转换完成。再输入将被用来做相反的 - 砸后,被调回的​​看法。大多数应用程序并不需要它,但它是有罕见的一个没有。

I'm doing some rudimentary exploration of Shared Element Transitions in Android L. The simple example I've setup has an image view translating from the top of the screen to the bottom of the screen during activity transitions and I've extended the transition duration so I can see things working. I've hit two problems so far trying to understand how Shared Element Transitions works.

1)When using only Enter/Return transitions (Exit/Reenter set to null). The enter transition is fine, but when the back button is pressed the view animates for a time, stops, then reappear in the final position. Seems similar to this question but I've set all the Exist/Reenter transitions to null so not sure why it happens.

2)When using only Exit/Reenter transitions (Enter/Return set to null). Nothing is happening, the view transitions down the screen like its following a default enter transition (300ms duration), and when back is pressed the view pops back to its original position.

How do I use Exit/Reenter transitions?

Here is my code:

activity_main.xml

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent"
    android:layout_height="match_parent" android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    android:paddingBottom="@dimen/activity_vertical_margin" tools:context=".MainActivity">

    <ImageView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:id="@+id/imageView"
        android:src="@drawable/ic_launcher"
        android:layout_alignParentTop="true"
        android:layout_centerHorizontal="true" />

    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Animate!"
        android:id="@+id/button"
        android:layout_centerVertical="true"
        android:layout_alignParentStart="true" />

</RelativeLayout>

activity_second.xml

<ImageView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:id="@+id/imageView2"
    android:src="@drawable/ic_launcher"
    android:layout_alignParentBottom="true"
    android:layout_centerHorizontal="true" />

MainActivity.java

public class MainActivity extends Activity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        getWindow().requestFeature(Window.FEATURE_CONTENT_TRANSITIONS);
        getWindow().requestFeature(Window.FEATURE_ACTIVITY_TRANSITIONS);
        getWindow().setAllowEnterTransitionOverlap(false);
        getWindow().setAllowReturnTransitionOverlap(false);


        getWindow().setSharedElementExitTransition(exitTransition());
        getWindow().setSharedElementReenterTransition(reenterTransition());
        //getWindow().setSharedElementExitTransition(null);
        //getWindow().setSharedElementReenterTransition(null);


        setContentView(R.layout.activity_main);

        final View iView = findViewById(R.id.imageView);
        iView.setTransitionName("image");

        final Button button = (Button)findViewById(R.id.button);
        button.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(MainActivity.this, SecondActivity.class);
                ActivityOptions options = ActivityOptions
                        .makeSceneTransitionAnimation(MainActivity.this, iView, "image");
                startActivity(intent, options.toBundle());
            }
        });
    }

    private Transition exitTransition() {
        ChangeBounds bounds = new ChangeBounds();
        bounds.setInterpolator(new BounceInterpolator());
        bounds.setDuration(2000);

        return bounds;
    }

    private Transition reenterTransition() {
        ChangeBounds bounds = new ChangeBounds();
        bounds.setInterpolator(new OvershootInterpolator());
        bounds.setDuration(2000);

        return bounds;
    }
}

SecondActivity.java

public class SecondActivity extends Activity {
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        getWindow().requestFeature(Window.FEATURE_CONTENT_TRANSITIONS);
        getWindow().requestFeature(Window.FEATURE_ACTIVITY_TRANSITIONS);
        getWindow().setAllowEnterTransitionOverlap(false);
        getWindow().setAllowReturnTransitionOverlap(false);


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


        setContentView(R.layout.activity_second);

        final View iView = findViewById(R.id.imageView2);
        iView.setTransitionName("image");
    }

    @Override
    public void onBackPressed() {
        super.onBackPressed();
        finishAfterTransition();
    }

    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;
    }
}

解决方案

As I recall, there is a bug in L that causes the shared element return transition to be interrupted if it takes longer than the reenter transition duration. If you adjust your reenter transition duration (on the calling Activity), that should fix the interruption problem until the bug is fixed in MR1.

The exit and reenter transitions are for executing stuff before the shared element is allowed to transition. For example, if you want to lift your shared element before transferring it, that would be done in the shared element exit transition. The reenter would be used to do the opposite -- drop the view after it was transferred back. Most apps don't need it, but it is there for the rare one that does.

这篇关于了解退出/重新输入共享单元转换的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆