两个界面的动画效果,可以使用共享元素。
代码如下:
首先看下工具类:
import android.app.Activity;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.support.v4.util.Pair;
import android.view.View;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
public class TransitionHelper {
/**
* 创建所需的活动过渡期间避免与系统UI小过渡的参与者。
*
* @param activity The activity used as start for the transition.
* @param includeStatusBar 如果是错误的,状态栏将不会被添加为过渡参与者
* @return All transition participants.
*/
public static Pair<View, String>[] createSafeTransitionParticipants(@NonNull Activity activity, boolean
includeStatusBar, @Nullable Pair... otherParticipants) {
// Avoid system UI glitches as described here:
// https://plus.google.com/+AlexLockwood/posts/RPtwZ5nNebb
View decor = activity.getWindow().getDecorView();
View statusBar = null;
if (includeStatusBar) {
statusBar = decor.findViewById(android.R.id.statusBarBackground);
}
View navBar = decor.findViewById(android.R.id.navigationBarBackground);
// 创建一对过渡参与者。
List<Pair> participants = new ArrayList<>(3);
addNonNullViewToTransitionParticipants(statusBar, participants);
addNonNullViewToTransitionParticipants(navBar, participants);
// only add transition participants if there's at least one none-null element
// 只有添加过渡参与者,如果至少有一个非空元素
if (otherParticipants != null && !(otherParticipants.length == 1 && otherParticipants[0] == null)) {
participants.addAll(Arrays.asList(otherParticipants));
}
return participants.toArray(new Pair[participants.size()]);
}
private static void addNonNullViewToTransitionParticipants(View view, List<Pair> participants) {
if (view == null) {
return;
}
participants.add(new Pair<>(view, view.getTransitionName()));
}
}
再看下动画开始前的界面设置:
private void transitionToActivity(TextView tS) {
final Pair<View, String>[] pairs = TransitionHelper.createSafeTransitionParticipants(this, false, new Pair<>(tS, "tS"));
Intent i = new Intent(this, SharedElementsActivity.class);
ActivityOptionsCompat transitionActivityOptions = ActivityOptionsCompat.makeSceneTransitionAnimation
(this, pairs);
startActivity(i, transitionActivityOptions.toBundle());
}
这里的TextView为动画开始时的控件。字符串tS为下个界面的TextView的transitionName值:
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="shared Elements"
android:transitionName="tS"/>
调用transitionToActivity方法动画自动完成。