makeSceneTransitionAnimation,当从fragment中的按钮打开intent中的activit



我正在打开一个片段按钮的点击活动屏幕。我试图通过makeSceneTransitionAnimation使按钮从我的片段屏幕移动到活动屏幕。但这不是动画。我怎么能打开一个活动从意图从片段,并有动画去。我非常感谢你的帮助。

public class ListFragment extends Fragment {
    FloatingActionButton fab;
    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
                R.layout.fragment, container, false);
    }

    @Override
    public void onActivityCreated(@Nullable Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);

 fab.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Intent i = new Intent(getActivity(), MainActivity.class);
             i.putExtra("title",  "title");
                ActivityOptionsCompat options = ActivityOptionsCompat.makeSceneTransitionAnimation(getActivity(), fab, "image");
                ActivityCompat.startActivity(getActivity(), i, options.toBundle());
    } }); }

如果你看到文档:

/**
 * Create an ActivityOptions to transition between Activities using cross-Activity scene
 * animations. This method carries the position of multiple shared elements to the started
 * Activity. The position of the first element in sharedElements
 * will be used as the epicenter for the exit Transition. The position of the associated
 * shared element in the launched Activity will be the epicenter of its entering Transition.
 *
 * <p>This requires {@link android.view.Window#FEATURE_CONTENT_TRANSITIONS} to be
 * enabled on the calling Activity to cause an exit transition. The same must be in
 * the called Activity to get an entering transition.</p>
 * @param activity The Activity whose window contains the shared elements.
 * @param sharedElements The names of the shared elements to transfer to the called
 *                       Activity and their associated Views. The Views must each have
 *                       a unique shared element name.
 * @return Returns a new ActivityOptions object that you can use to
 *         supply these options as the options Bundle when starting an activity.
 */
public static ActivityOptionsCompat makeSceneTransitionAnimation(Activity activity,
        Pair<View, String>... sharedElements) {

所以你可能需要输入

ActivityOptionsCompat options = ActivityOptionsCompat.makeSceneTransitionAnimation(getActivity(), new Pair<>(fav, "image"));
ActivityCompat.startActivity(getActivity(), i, options.toBundle());

@jason,

styles.xml文件

中添加以下代码
<style name="TransitionTheme" parent="Theme.AppCompat.Light.DarkActionBar">
<!-- enable window content transitions -->
<item name="android:windowActivityTransitions">true</item>
<!-- specify enter and exit transitions -->
<item name="android:windowEnterTransition">@android:transition/slide_right</item>
<item name="android:windowExitTransition">@android:transition/slide_left</item>
</style>

manfiest.xml中,进入活动并使用如下主题;

<activity
        android:name=".YourActivityName"
        android:theme="@style/TransitionTheme"/>

最新更新