【问题标题】:makeSceneTransitionAnimation when opening an activity from an intent from a button within a fragmentmakeSceneTransitionAnimation 从片段内按钮的意图打开活动时
【发布时间】:2019-04-19 02:29:42
【问题描述】:

我通过单击片段按钮打开活动屏幕。我正在尝试通过 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());

    } }); }

【问题讨论】:

    标签: android


    【解决方案1】:

    如果您看到文档:

    /**
     * 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());
    

    【讨论】:

      【解决方案2】:

      @杰森,

      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>
      

      ma​​nfiest.xml 中,转到活动并使用如下主题;

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

      【讨论】:

        猜你喜欢
        • 2023-03-28
        • 2015-08-04
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2014-06-28
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多