我正在对Android L中的共享元素转换做一些基本的探索。我设置的简单示例有一个图像视图,在活动转换期间从屏幕顶部转换到屏幕底部,我已经扩展过渡时间让我看到事情有效。到目前为止,我遇到了两个问题,试图了解共享元素转换的工作原理。
1)仅使用Enter / Return转换(Exit / Reenter设置为null)。输入转换很好,但是当按下后退按钮时,视图会动画一段时间,停止,然后重新出现在最终位置。似乎与this question类似,但我将所有Exist / Reenter过渡设置为null,因此不确定它为什么会发生。
2)仅使用退出/重新转换时(Enter / Return设置为null)。没有任何事情发生,视图在屏幕上向下转换,如下面的默认输入转换(300毫秒持续时间),当按下后退时,视图会弹回原来的位置。
如何使用退出/重新转换?
这是我的代码:
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;
}
}
答案 0 :(得分:12)
您不应该在finishAfterTransition()
中致电onBackPressed()
。 Activity
超类已经为你做了这个。
您应在requestFeature()
之前致电super.onCreate()
。如果您使用Window.FEATURE_ACTIVITY_TRANSITIONS
主题(或类似主题),则无需请求Theme.Material
。
此处呼叫setAllowEnterTransitionOverlap(false)
和setAllowReturnTransitionOverlap(false)
是多余的。这些决定了活动的窗口内容转换重叠......它们根本不会影响活动的共享元素内容转换。
很少需要设置退出和重新输入共享元素转换。您几乎总是希望使用输入和返回转换。如果仅设置退出并重新输入共享元素转换并将enter和return共享元素转换为null,则被调用的活动将无法知道如何在转换开始时为共享元素设置动画,并且动画将显示为已损坏
答案 1 :(得分:9)
我记得,L中存在一个错误,如果它比重新进入转换持续时间更长,则会导致共享元素返回转换被中断。如果您调整重新设置转换持续时间(在调用活动上),那么应该修复中断问题,直到在MR1中修复错误。
退出和重新进入转换用于在允许共享元素转换之前执行内容。例如,如果要在传输共享元素之前解除共享元素,则可以在共享元素退出转换中完成。重新输入将用于执行相反的操作 - 将视图转移回后将其删除。大多数应用程序都不需要它,但它适用于罕见的应用程序。