简体   繁体   English

使用共享元素的Android片段转换

[英]Android fragment transition with shared element

everyone! 大家! I have 2 fragments. 我有2个片段。 In one fragment i have a little image in card. 在一个片段中,我在卡片中有一个小图像。 In 2d fragment i have the same image in the header of my layout. 在2d片段中,我的布局标题中有相同​​的图像。 I want to make shared element transition of this image from one fragment to another like in this example http://1.bp.blogspot.com/-Vv4SxVSI2DY/VEqQxAf3PWI/AAAAAAAAA7c/mfq7XBrIGgo/s1600/activity_transitions%2B(1).gif 我希望将此图像的共享元素从一个片段转换到另一个片段,如本例所示http://1.bp.blogspot.com/-Vv4SxVSI2DY/VEqQxAf3PWI/AAAAAAAAA7c/mfq7XBrIGgo/s1600/activity_transitions%2B(1).gif

2d fragment: 2d片段:

<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/main"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#f2f4f7">
<CustomScrollView
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/scroll_view"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    android:background="#f2f4f7">
    <LinearLayout
        android:id="@+id/content_wrapper"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="vertical"
        android:divider="@drawable/vertical_divider"
        android:showDividers="middle">
        <FrameLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content">
            <RelativeLayout
                android:id="@+id/header_wrapper"
                android:layout_width="match_parent"
                android:layout_height="166.7dp">
                <ImageView
                    android:id="@+id/share_image"
                    android:transitionName="imageTransition"
                    android:layout_width="match_parent"
                    android:layout_height="166.7dp"
                    android:src="@drawable/friday"/>

1st fragment: 第一片段:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:gravity="center_horizontal"
android:layout_marginTop="56dp"
android:background="#ff1e1e23">
<ImageView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_marginTop="25dp"
    android:src="@drawable/menuicon1g"/>
<TextView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_marginTop="25dp"
    android:gravity="center"
    android:textColor="#fff"
    android:textSize="22sp"/>
<android.support.v7.widget.CardView
    xmlns:card_view="http://schemas.android.com/apk/res-auto"
    android:id="@+id/card_view"
    android:layout_gravity="center"
    android:layout_width="200dp"
    android:layout_height="300dp"
    android:layout_marginTop="25dp"
    card_view:cardCornerRadius="4dp"
    card_view:cardBackgroundColor="#fff"
    card_view:cardElevation="2dp">
    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="vertical"
        android:gravity="center_horizontal">
        <ImageView
            android:id="@+id/share_image"
            android:transitionName="imageTransition"
            android:layout_width="match_parent"
            android:layout_height="92.5dp"
            android:src="@drawable/friday"/>
        <ImageView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginTop="20dp"
            android:src="@drawable/demo_calendar"/>
        <com.zagum.work.virtualfitness.CustomTextView
            xmlns:com.zagum.work.virtualfitness="http://schemas.android.com/apk/res/com.zagum.work.virtualfitness"
            android:id="@+id/date"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:textSize="18sp"
            android:layout_marginTop="10dp"
            com.zagum.work.virtualfitness:typeface="fonts/Roboto-Light.ttf"/>
        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginTop="15dp"
            android:layout_marginBottom="40dp"
            android:gravity="center"
            android:paddingLeft="40dp"
            android:paddingRight="40dp"
            android:textSize="18sp"
            android:textStyle="bold"/>
    </LinearLayout>
</android.support.v7.widget.CardView>

Transaction: 交易:

if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
                View image = mRootView.findViewById(R.id.share_image);

                CurrentWorkoutFragment secondFragment = CurrentWorkoutFragment.newInstance();
                secondFragment.setSharedElementEnterTransition(TransitionInflater.from(getActivity()).inflateTransition(R.transition.transition_move));

                FragmentTransaction trans = getFragmentManager().beginTransaction();
                trans.replace(R.id.container, secondFragment);
                trans.addToBackStack(null);
                trans.addSharedElement(image, image.getTransitionName());
                trans.commit();
            }

transition_move.xml transition_move.xml

<transitionSet xmlns:android="http://schemas.android.com/apk/res/android">
   <changeImageTransform />
   <changeBounds/>
</transitionSet>

But no image animation happens. 但是没有图像动画发生。 It appears in the top left corner an resizes. 它出现在左上角的调整大小。

Check out this answer. 看看这个答案。

Because the view I'm sharing between 2 fragments is a child of another view (RelativeLayout) in the 2nd fragment, you need to add the ChangeTransform transition to your TransitionSet. 因为我在2个片段之间共享的视图是第二个片段中另一个视图(RelativeLayout)的子视图,所以需要将ChangeTransform过渡添加到TransitionSet。

Basically what you need is to add a <changeTransform/> in your transition_move.xml 基本上你需要的是在transition_move.xml中添加一个<changeTransform/>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 Android:RecyclerView和Fragment之间没有共享元素转换 - Android: No shared element transition between RecyclerView and Fragment Android fragment 共享元素过渡实现 - Android fragment shared element transition implementation Android共享元素在带有片段的活动与带有片段的另一个活动之间转换 - Android shared element transition between an activity with a fragment to another activity with a fragment 共享元素片段过渡不起作用 - Shared element fragment transition not working 具有片段共享元素过渡的WindowInset - WindowInset with Fragment Shared Element Transition Android共享元素片段过渡:返回过渡不起作用 - Android shared element fragment transitions: return transition not working Android共享元素转换 - Android shared element transition 如何使用android.support.transition.Transition / TransitionSet作为片段共享元素进入/返回转换? - How to use android.support.transition.Transition/TransitionSet as a fragment shared element enter/return transition? 如何共享元素从片段到活动的转换 - how to Shared element transition from a fragment to an activity 片段共享元素转换使用 add() 而不是 replace()? - Fragment shared element transition with add() instead of replace()?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM